#!/bin/bash function MIOERR(){ echo "输入参数格式错误!!!" echo "格式要像这样 ./ask_mysql_bak.sh MIO_HOST MIO_PORT MIO_DATABASE MIO_USER MIO_PSW MIO_DELDAY MIO_SAVEPATH" echo "host 数据库IP port 数据库端口 database 要备份的库名 user 用户名 psw 密码 delday删除N天之前的备份数据 path 保存目录" exit 2 } function MIOLOG(){ echo "$1">>"${MIO_SAVEPATH}run.log" } MIO_HOST=$1 MIO_PORT=$2 MIO_DATABASE=$3 MIO_USER=$4 MIO_PSW=$5 MIO_DELDAY=$6 MIO_SAVEPATH=$7 if [ $# != 7 ]; then MIOERR else MIOLOG "备份开始 `date "+%Y-%m-%d %H:%M:%S"`" fi now_time=`date "+%Y-%m-%d"` del_timeswap=`date -d "${now_time}" +%s` del_timwswap=$[${del_timeswap}-(86400*$MIO_DELDAY)] del_time=`date -d @${del_timwswap} "+%Y-%m-%d"` del_file="${MIO_SAVEPATH}${del_time}.sql" if [ -f "${del_file}" ]; then rm -f "${del_file}" fi bak_name="${MIO_SAVEPATH}${now_time}.sql" `mysqldump -h "${MIO_HOST}" -P "${MIO_PORT}" -u"${MIO_USER}" -p"${MIO_PSW}" "${MIO_DATABASE}" > "${bak_name}"` if [ $? == 0 ];then MIOLOG "备份成功 `date "+%Y-%m-%d %H:%M:%S"`" exit 0 else MIOLOG "备份失败 `date "+%Y-%m-%d %H:%M:%S"`" exit 1 fi