场景:备份Server (S) 的数据到 Local(L)
On Local:1.为了无密码使用rsync
ssk-keygenssh-copy-id user@Server.com
2.保留最近3,5天的备份数据(数据库,code),磁盘数据不做多重备份backup_local.sh
#!/bin/shfind /data/backup/db_code/db* -mtime +5 -exec rm {} \;find /data/backup/db_code/htdocs* -mtime +3 -exec rm {} \;find /data/backup/db_code/rdb* -mtime +5 -exec rm {} \;
3.编辑crontabcrontab -e
6 2 * * * rsync -avz root@Server.com:/data/serverdata /data/backup/6 3 * * * rsync -avz root@Server.com:/var/backups/* /data/backup/db_code/6 5 * * * bash /data/backup/backup_local.sh
On Server1.打包数据
#!/bin/shTHEDB="dbname"THEDBUSER="root"THEDBPW="xxxxxx"THEDATE=`date +%Y-%m-%d-%H-%M-%s`rm /var/backups/*tar czf /var/backups/rdb_backup_${THEDATE}.tar /data/redis/data/dump.rdb && gzip -f /var/backups/rdb_backup_${THEDATE}.tartar czf /var/backups/htdocs_backup_${THEDATE}.tar /data/hq_htdocs && gzip -f /var/backups/htdocs_backup_${THEDATE}.tarmysqldump -u $THEDBUSER -p${THEDBPW} $THEDB | gzip > /var/backups/db_backup_${THEDB}_${THEDATE}.bak.gz
2.编辑crontab
6 1 * * * bash /var/backup_server.sh