双VPS增量备份实践

一、背景介绍

    本人有两个vps,一个linode,一个阿里云,其中linode是本博客所在主机,很多资料需要定期备份。恰好阿里云的磁盘够大,整个linode主机磁盘拷贝进去也有富余,于是简单做个增量备份的shell脚本。 在这里献丑了。

二、配置过程

1  配置两个vps的ssh免密码登录

2  两个vps均安装rsync
    yum install -y rsync

3  编写备份脚本

备份脚本的内容如下,

#!/bin/bash
 
#  this script is made to automatically backup data of host linode
#  Author:Attaboy
#  Modify history:
#       2017.11.11
#       2017.11.14  add local backup function
#       2017.11.17  do not print debug info to log file when backup DB
#       2017.11.22  add mailx report
#       2017.11.25  if initial backup size is too low ,then mail to report.

source /etc/profile

switch="0"
src="/data /srv /etc /mnt /opt /media /var/www /usr /lib /lib64 /bin /root /home /sbin /boot /sys"
bak_node1="leniency@peloo.cn:/data/backup/linode"
bak_node2="/data/backup"
bak_node1_ip="peloo.cn"
bak_node1_port="34782"
bak_log="/var/log/linode-backup.log"
dst_mail="458946985@qq.com"
mail_content_01="\n\nYour aliyun vps sshd service is offline when running backup!!!\n\n"
mail_content_02="\n\nYour disk size is lower than 1500MB after cleaning some files, please release some space !!!\n\n$(df -mT)\n\n"
mail_title_01="Linode Backup Report"
mail_title_02="Linode Running Health Report"
free_disk_size="$(df -m | awk '/sda/{ print $4 }')"
required_disk_size="1500"

pre_do(){

#clean some backup files first.
find ${bak_node2} -type f -ctime +5 -execdir rm -f {} \;

#  check the integtity of important paths and remote host alive.
if [ ${UID} -ne 0 ] ; then
        echo -e '\n…… Current user is not root, please switch user using "su" .\n' >>${bak_log}
        exit -1
elif [ ${free_disk_size} -lt ${required_disk_size} ] ; then
        echo -e "\n\nYour disk size is lower than ${required_disk_size}MB after cleaning some files,Please release some space !\n\n" >>${bak_log} 2>&1
        echo -e "${mail_content_02}" | mailx -v -s "${mail_title_02}" "${dst_mail}" >>${bak_log} 2>&1
        exit -4
else
        echo -e "\n\nYour disk size is adiquate !!!\n\n"
fi

}

backup_node1(){

#backup local file to remote node1–aliyun vps
nc -zv ${bak_node1_ip} ${bak_node1_port}
if [ $? -eq 0 -a ${switch} -eq 0 ] ; then
        echo -e "\ntime:$(date)…… remote backup node1 begin,just wait .\n" >>${bak_log}
        for i in ${src} ; do
                echo ${i}
                rsync -azvP ${i} -e "ssh -p ${bak_node1_port}" ${bak_node1} >>${bak_log} 2>&1
        done
        echo -e "\ntime:$(date)…… remote backup node1  end.\n" >>${bak_log}
else
        echo -e "\n…… HOST ${bak_node1_ip} is offline, script will halt with exit code -2" >>${bak_log}
        echo -e "${mail_content_01}" | mailx -v -s "${mail_title_01}" "${dst_mail}" >>${bak_log} 2>&1
        exit -2
fi

}

backup_node2(){

#backup local file to local disk
if [ ${switch} -eq 0 ] ; then
        tar zcvf ${bak_node2}/wordpress/wordpress-$(date +%Y%m%d)-bak.tar.gz /var/www/wordpress >>${bak_log} 2>&1
        tar zcvf ${bak_node2}/nginx/nginx-$(date +%Y%m%d)-bak.tar.gz /usr/local/openresty >>${bak_log} 2>&1
        mysqldump -u root -pv0b2uEvrVmjg17mMzw2M –all-databases > ${bak_node2}/mysql/all_database-$(date +%Y%m%d).bak 1>/dev/null
        mysqldump -u root -pv0b2uEvrVmjg17mMzw2M owemeawe_hail > ${bak_node2}/mysql/owemeawe_hail-$(date +%Y%m%d).bak 1>/dev/null
fi

}

post_do(){

find ${bak_node2} -type f -ctime +5 -execdir rm -f {} \;
if [ ${free_disk_size} -lt 500 ] ; then
        echo -e "\n\nYour disk size is lower than 500MB after cleaning some files,Please release some space !\n\n" >>${bak_log} 2>&1
        echo -e "${mail_content_02}" | mailx -v -s "${mail_title_02}" "${dst_mail}" >>${bak_log} 2>&1
else
        echo -e "\n\nYour disk size is adiquate !!!\n\n"
fi

}

pre_do
backup_node2
backup_node1
post_do

exit 0

 

注解:脚本里主要有两个函数,对应备份的两个过程,pre_do函数用来在备份前检查当前运行脚本的用户是否为root,以及判断日志文件是否超过限定并及时清空日志文件,bak_node1函数是用来实现整个远程备份过程的,核心是用了rsync的ssh加密通道进行增量备份,bak_node2函数用来备份本机重要的程序文件和数据库。


4  添加crontab任务计划

在/etc/crontab文件添加每7天备份一次的任务计划

echo "* * */7 * * root /bin/bash /srv/sh/auto-backup.sh">>/etc/crontab

 

发表评论

电子邮件地址不会被公开。