标签:需要 dump type exec 第一步 def time 服务器 bash
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/expect
set user root
set password 123456
set dir /home/wwwroot/default/
set ip xxx.xxx.xxx
set 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>&1
30 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
标签:需要 dump type exec 第一步 def time 服务器 bash
原文地址:https://www.cnblogs.com/jiqing9006/p/12405506.html