Linux 环境自动备份文件
1、先安装rsync
yum install rsync
2、写一个shell脚本rsync_file.sh
#!/usr/bin/env bash
function rsync_file(){
pwds='123456'
datestr=`date`
echo -e "\033[32m =========== 开始备份 ========== \033[0m"
# \033[32m的解释见链接: https://blog.csdn.net/panpan639944806/article/details/23930553
echo -e "\033[31m [${datestr}] \033[0m"
/usr/bin/expect<<-EOF
set timeout 443200
spawn bash -c "rsync -avzu --progress /home/cj/* root@180.80.*.*:/opt/"
expect {
"*assword*" {send "$pwds\r"}
}
expect "total size is*"
send "exit\r"
expect eof
EOF
datestr2=`date`
echo -e "\033[32m =========== 备份结束 ========== \033[0m"
echo -e "\033[31m [${datestr2}] \033[0m"
}
rsync_file
3、设置定时任务
crontab -e
00 00 * * * sh /opt/rsync_file.sh >> /home/logs/log.log