# 配置mysql备份
00 03 * * * sh /home/cron/back_mysql_db.sh >> /tmp/back_mysql_db.log 2>&1
30 03 * * * /home/cron/scp_back_to_test.sh /home/wwwroot/default/mysqlbackups/ >> /tmp/scp_back_to_test.log 2>&1
-----------------------------------------------------------执行备份数据库脚本-------------------------------------------------------------
[root@c118c178c255c134 cron]# cat back_mysql_db.sh
#!/bin/bash
# 没有则创建
if [ ! -d "/home/wwwroot/default/mysqlbackups" ];then
mkdir -p "/home/wwwroot/default/mysqlbackups"
fi
# 备份数据库
mysqldump -uroot -p**** daping > /home/wwwroot/default/mysqlbackups/daping_$(date + %Y%m%d%H%M%S).sql
# 只保留5天的数据库
find /home/wwwroot/default/mysqlbackups/ -type f -ctime +3 -exec rm -rf {} ;
----------------------------------------------移动备份数据库到备份数据库-----------------------------------------------------------
crontab -e
30 3 * * * sh /home/sh/clear_old_mysql_db.sh >> /tmp/clear_old_mysql_db.log 2 >&1
[root@c118c178c255c134 cron]# cat scp_back_to_test.sh
#!/usr/bin/expect
set user root
set password ********
set dir /home/wwwroot/default/
set ip 218.93.***.**
set files [lrange $argv 0 0]
spawn scp -r ${files} ${user}@${ip}:${dir}
expect {
"*yes/no*" { send "yes
"; exp_continue}
"*password:*" { send "$password
"; exp_continue }
}
interact
-------------------------------备份所在服务器,定期删除多余备份数据库脚本!!!------------------------------
30 3 * * * sh /home/sh/clear_old_mysql_db.sh >> /tmp/clear_old_mysql_db.log 2 >&1
[root@caomall mysqlbackups]# cat /home/sh/clear_old_mysql_db.sh
#!/bin/bash
# 清理超过3天的数据
find /home/wwwroot/default/mysqlbackups/ -type f -ctime +3 -exec rm -rf {} ;
echo "清理成功";