1、配置免密登录
ssh-copy-id xxx@111.111.111.111
xxx用户名,111.111.111.111示例IP
2、编写备份拷贝到备份机脚本
#! /bin/sh
tarName="$(date +%Y%m%d)"
ssh xxx@111.111.111.111 << remotessh
mkdir /home/xxxBacxxxkupTemp
mkdir /home/xxxBackup
cp -r /var/xxx /home/xxxBackupTemp
cd /home/wikiBackup
tar -zcPvf xxxBackup$tarName.tar.gz /home/xxxBackupTemp
/bin/rm -rf /home/xxxBackupTemp
# 删除7天以前的备份
find /home/xxxBackup/ -mtime +7 -name "*.tar.gz" -exec /bin/rm -rf {} \;
exit
remotessh
cd ~/xxxBackup
# 将远端机器打包好的数据拷贝到本备份机
scp xxx@111.111.111.111:/home/xxxBackup/xxxBackup$tarName.tar.gz .
# 删除30天以前的备份
find ./ -mtime +30 -name "*.tar.gz" -exec /bin/rm -rf {} \;
exit
3、设置定时任务
crontab -e
添加定时执行脚本
# 每天执行一次
0 0 */1 * * sh /home/xxx/backup.sh
# 以下为测试 每两分钟执行一次
*/2 * * * * sh /home/xxx/backup.sh
保存退出
通过crontab -l
命令查看定时任务