back_mysql_db.sh
#!/bin/bash# 没有则创建if [ ! -d "/home/wwwroot/default/mysqlbackups" ];then mkdir -p "/home/wwwroot/default/mysqlbackups"fi# 备份数据库mysqldump -uroot -p123456 dbname > /home/wwwroot/default/mysqlbackups/dbname_$(date +%Y%m%dH%M%S).sql# 只保留5天的数据库find /home/wwwroot/default/mysqlbackups/ -type f -ctime +5 -exec rm -rf {} \;
scp_back_to_test.sh
需要安装一下expect
yum install expect
#!/usr/bin/expectset user rootset password 123456set dir /home/wwwroot/default/set ip xxx.xxx.xxxset files [lrange $argv 0 0]spawn scp -r ${files} ${user}@${ip}:${dir}expect { "*yes/no*" { send "yes\r"; exp_continue} "*password:*" { send "$password\r"; exp_continue }}interact
30 03 * * * sh /home/cron/back_mysql_db.sh >> /tmp/back_mysql_db.log 2>&130 03 * * * /home/cron/scp_back_to_test.sh /home/wwwroot/default/mysqlbackups/ >> /tmp/scp_back_to_test.log 2>&1
每天夜里3点半
clear_old_mysql_db.sh
#!/bin/bash# 只保留5天的数据库find /home/wwwroot/default/mysqlbackups/ -type f -ctime +5 -exec rm -rf {} \;
30 3 * * * sh /home/sh/clear_old_mysql_db.sh >> /tmp/clear_old_mysql_db.log 2 >&1