定时备份脚本

backuplist 备份清单

vim /server/scripts/backuplist

/server/scripts
/etc
/var/spool/cron

 backup_to_rsync.sh 备份脚本

vim /server/scripts/backup_to_rsync.sh

#!/bin/bash
#Source function library
. /etc/init.d/functions

rsync_host=rsync01.test.cn

#Define variables
IP=$(ifconfig eth1 | awk -F '[ :]+' 'NR==2 {print $4}')
Path="/backup/$IP"
TIME=`/bin/date +%F`
BackupFile=/server/scripts/backuplist

#Judge the existence of variables
[ ! -d $Path ] && mkdir -p $Path
[ ! -f $BackupFile ] && {
     echo "Please give me $BackupFile"
     exit 1
}

#Define result function
function Msg(){
     if [ $? -eq 0 ];then
          action "$*" /bin/true
     else
          action "$*" /bin/false
     fi
}

#Backup config files
tar czfh $Path/conf_${TIME}.tar.gz `cat $BackupFile` &>/dev/null
md5sum $Path/conf_${TIME}.tar.gz > $Path/flag_$TIME
Msg 'Backup config files'

#Send backup to backup server
rsync -az $Path rsync_backup@${rsync_host}::backup --password-file=/etc/rsync.password &>/dev/null
Msg 'Send backup to backup server'

#Delete backup a week ago
find $Path -type f -name "*.tar.gz" -mtime +7 | xargs rm -f &>/dev/null
Msg 'Delete backup a week ago'

 set_backup_script.sh 设置定时任务

vim /server/scripts/set_backup_script.sh

#!/bin/bash
echo "123456" > /etc/rsync.password &&\
chmod 600 /etc/rsync.password
function crond_backup(){
     [ `crontab -l | grep "backup data" | wc -l` -eq 0 ] && {
     echo -e "#backup data\n00 00 * * * /bin/sh /server/scripts/backup_to_rsync.sh >/dev/null 2>&1" >> /var/spool/cron/root
     crontab -l
     sleep 2
     } || {
          echo "backup cron is exist,no config."
     }
}
crond_backup

sh /server/scripts/set_backup_script.sh
posted @ 2017-09-13 23:50  Peterer~王勇  阅读(325)  评论(0编辑  收藏  举报