创建备份文件存放的目录 mkdir /usr/local/dbbak 脚本:vi /usr/local/mysqlback.sh # /bin/bash DB_NAME="dsideal_db" DB_USER="root" DB_PASS="123456" BACK_DIR="/usr/local/dbbak" DATE="$DB_NAME-`date +'%Y-%m-%d=%H:%M:%S'`" LogFile="$BACK_DIR"/dbbakup.log #日志记录保存的目录 BackNewFile=$DATE.sql mysqldump --opt --force -u$DB_USER -p$DB_PASS $DB_NAME > $BACK_DIR/$DATE.sql echo -----------------------"$(date +"%y-%m-%d %H:%M:%S")"----------------------- >> $LogFile echo createFile:"$BackNewFile" >> $LogFile find "/usr/local/dbbak/" -mtime +7 -type f -name "*.sql" -print > deleted.txt echo -e "delete files:\n" >> $LogFile #循环删除匹配到的文件 cat deleted.txt | while read LINE do rm -rf $LINE echo $LINE>> $LogFile done echo "---------------------------------------------------------------" >> $LogFile 给sh脚本增加权限
chmod 777 /usr/local/mysqlback.sh
利用crontab,每天凌晨3点定时执行 crontab -e 0 03 * * * /usr/local/mysqlback.sh > /dev/null 2>&1
【备注】简单的备份 mysqldump --databases test --flush-logs> /usr/local/backup-`date +%F-%H-%M`.sql (责任编辑:IT) |