自用PC增量备份Shell脚本

Table of Contents

最近自己的PC硬盘里丢了个重要文件,应该是我误删除的,所以提醒大家千万别乱删文件,就算删除也要备份一俩月。于是一怒之下写了个shell脚本,专门用于备份重要文件的,主要是通过rsync这个强大的同步工具实现,脚本里也使用了函数创建和调用,具体内容如下,

#!/bin/bash
 
#  this script is made to automatically backup data of host S5
#  Author:Attaboy
#  history:
#       2016.06.28
#       2016.07.03
#       2016.07.14
#       2017.09.02
#       2017.09.04
#       2017.09.14
#       2017.09.17
#       2017.11.04  anotate bak_node_3 function
#       2017.11.07  allow bak_node_3 function
#       2017.11.11  allow bak_node_4 function
#       2017.11.12  add code to start DELL server
#       2017.11.14  add backup node 6 function
#       2017.11.18  modify backup function 6
#       2017.11.19
#       2017.11.25  add variable end_hour
#       2017.12.30  shutdown virtual machine before running backup task
#       2017.12.30  modify notification and anotate shutdown machine code
#       2018.02.20  add function of absolutely synchronizing in some backup nodes.
#       2018.02.21  modify error code, ban backup_node_1, backup_node_5 and backup_node_6

switch="0"
bak_node3_port="22"
bak_node4_port="22"
mnt_dir="mnt/2T|mnt/1T_0|mnt/3T|dev/sda|mnt/1T_1"
src="/mnt/2T"
bak_node_1="/mnt/1T_0" #local disk
bak_node_2="/mnt/1T_1" #local disk
bak_node_3="[email protected]:/media/pi/Seagate_disk/backup" #Raspberry pi
bak_node_4="[email protected]:/mnt/data/backup" #DELL SERVER
bak_node_5="/mnt/3T" #local disk
bak_node_6="/mnt/4T/backup/2T"
bak_node3_ip="172.17.1.176"
bak_node4_ip="172.17.1.170"
remote_boot_ip="172.17.1.171"
remote_boot_user="pc"
remote_boot_pwd="pc"
bak_log="/var/log/S5-backup.log"
end_hour="07"

pre_do(){

#  check the integtity of important paths and remote host alive.
if [ ! ${UID} -eq 500 ] ; then
        echo -e '\n...... Current user is not pc, please switch user using "su" .' >>${bak_log} 2>&1
        exit -1
elif [ $(df -h 2>/dev/null | grep -Eic ${mnt_dir}) -lt 5 ] ; then
        echo -e '\n...... number of PATHs is NOT FOUR ,script WILL HALT with exit code -2.' >>${bak_log} 2>&1
        exit -2
elif [ $(ping ${bak_node3_ip} -c 2 | grep -c icmp) -eq 0 ] ; then
        echo -e "\n...... Host ${bak_node3_ip} is offline" >>${bak_log} 2>&1
        #exit -3
elif [ ${switch} -eq 0 ] ; then
        nc -zv ${bak_node4_ip} ${bak_node4_port}
        if [ $? -eq 0 ] ; then
        echo -e "skip start DELL server ."
        else
        ipmitool -U ${remote_boot_user} -P ${remote_boot_pwd} -H ${remote_boot_ip} power on >>${bak_log} 2>&1
        #ping -c 400 127.1 2>/dev/null 1>&2
        fi
elif [ ! -d ${bak_node_1} -o ! -d ${bak_node_5} -o ! -d ${src} ] ; then
        echo -e "\n...... folder ${bak_node_1}, ${bak_node_5} or ${src} is not available, script will halt with exit code -4" >>${bak_log} 2>&1
        exit -4
#if log file size is up to 100MB,then empty it.
elif [ $(du -sm ${bak_log} | awk '{ print $1 }') -gt 100 ] ; then
        >${bak_log}
fi

# shutdown virtual machines
#for l in $(vboxmanage list vms | grep -iE "centos601\+oracle11gr2|c7964-formal|win7-pure-zrhsh" | awk -F'"' '{ print $2 }')
#do
#       vboxmanage controlvm  ${l} poweroff  2>/dev/null
#done

}

backup_node1(){

#backup from local to local node1(1T_0)
echo -e "\nTime:$(date +%Y%m%d-%H%M%S)...... local backup node1(1T_0) begin,just wait .\n" >>${bak_log} 2>&1
rsync -azvP $src/down           ${bak_node_1}/backup >>${bak_log} 2>&1
rsync -azvP $src/work           ${bak_node_1}/backup >>${bak_log} 2>&1
rsync -azvP $src/life           ${bak_node_1}/backup >>${bak_log} 2>&1
rsync -azvP $src/tools          ${bak_node_1}/backup >>${bak_log} 2>&1
rsync -azvP $src/backup        ${bak_node_1}/backup
echo -e "\nTime:$(date +%Y%m%d-%H%M%S)...... local backup node1(1T_0) end.\n" >>${bak_log} 2>&1

}


backup_node2(){

#backup from local to local node2(1T_1)
echo -e "\nTime:$(date +%Y%m%d-%H%M%S)...... local backup node2(1T_1) begin,just wait .\n" >>${bak_log} 2>&1
rsync -azvP --delete $src/down          ${bak_node_2}/backup >>${bak_log} 2>&1
rsync -azvP --delete $src/work          ${bak_node_2}/backup >>${bak_log} 2>&1
rsync -azvP --delete $src/life          ${bak_node_2}/backup >>${bak_log} 2>&1
rsync -azvP --delete $src/tools         ${bak_node_2}/backup >>${bak_log} 2>&1
rsync -azvP --delete $src/backup        ${bak_node_2}/backup >>${bak_log} 2>&1
echo -e "\nTime:$(date +%Y%m%d-%H%M%S)...... local backup node2(1T_1) end.\n" >>${bak_log} 2>&1

}
 
#backup local file to Raspberry pi
backup_node3(){

#backup from local to remote node3(raspberrypi)
nc -zv ${bak_node3_ip} ${bak_node3_port}
if [ $? -eq 0 -a ${switch} -eq 0 ] ; then
        echo -e "\nTime:$(date +%Y%m%d-%H%M%S)...... remote backup node3(raspberrypi) begin,just wait .\n" >>${bak_log} 2>&1
        rsync -azvP --delete $src/down -e "ssh -p ${bak_node4_port}" ${bak_node_3} >>${bak_log} 2>&1
        echo -e "\nTime:$(date +%Y%m%d-%H%M%S)...... remote backup node3(raspberrypi) end.\n" >>${bak_log} 2>&1
else
        echo -e "\n...... Host ${bak_node3_ip} is offline" >>${bak_log} 2>&1
fi

}

#  backup full disk of sumsang SSD and other important folders
backup_node4(){

#backup from local to remote node4(DELL server S7)
nc -zv ${bak_node4_ip} ${bak_node4_port}
if [ $? -eq 0 -a ${switch} -eq 0 ] ; then
        echo -e "\nTime:$(date +%Y%m%d-%H%M%S)...... remote backup node4(DELL server S7) begin,just wait .\n" >>${bak_log} 2>&1
        rsync -azvP $src/down/CV -e "ssh -p ${bak_node4_port}" ${bak_node_4} >>${bak_log} 2>&1
        echo -e "\nTime:$(date +%Y%m%d-%H%M%S)...... remote backup node4(DELL server S7)  end.\n" >>${bak_log} 2>&1
else
        echo -e "\n...... Host ${bak_node4_ip} is offline" >>${bak_log} 2>&1
fi

}

backup_node5(){

#backup from local to local node5(3T), full syncronous
echo -e "\nTime:$(date +%Y%m%d-%H%M%S)...... local backup node2(3T) begin,just wait .\n" >>${bak_log} 2>&1
rsync -azvP --delete $src/down          ${bak_node_5}/backup >>${bak_log} 2>&1
rsync -azvP --delete $src/work          ${bak_node_5}/backup >>${bak_log} 2>&1
rsync -azvP --delete $src/life          ${bak_node_5}/backup >>${bak_log} 2>&1
rsync -azvP --delete $src/tools         ${bak_node_5}/backup >>${bak_log} 2>&1
rsync -azvP --delete $src/backup        ${bak_node_5}/backup >>${bak_log} 2>&1
echo -e "\nTime:$(date +%Y%m%d-%H%M%S)...... local backup node5(3T) end.\n" >>${bak_log} 2>&1

}

backup_node6(){

#backup from local to local node6(4T/backup/2T)
echo -e "\nTime:$(date +%Y%m%d-%H%M%S)...... local backup node6 begin,just wait .\n" >>${bak_log} 2>&1
rsync --delete -azvP ${src}             ${bak_node_6}
echo -e "\nTime:$(date +%Y%m%d-%H%M%S)...... local backup node6 end.\n" >>${bak_log} 2>&1

}

#define what is done when backup is over
post_do(){
#DELL server will not shutdown when it is dayTime .
if [ $(date '+ %k') -le ${end_hour} ] ; then
        #umount -l $bak_dev  && mount -o ro $bak_dev $bak_node_1
        ipmitool -U ${remote_boot_user} -P ${remote_boot_pwd} -H ${remote_boot_ip} power off
        ping -c 2 127.1 1>/dev/null 2>&1
        ipmitool -U ${remote_boot_user} -P ${remote_boot_pwd} -H ${remote_boot_ip} power off
        #umount -l $bak_dev && mount $bak_dev $bak_node_1
fi

}

pre_do
#backup_node1
#backup_node2
backup_node3  #Raspberry pi
backup_node4  #DELL server S7
backup_node5
#backup_node6
post_do

exit 0

------------------------------------------------------分割线-------------------------------------------------------

解释:

1  单独写一个函数pre_do用作数据备份前的各项检查,以便主备份函数的正常运行;

2  函数backup_node1和backup_node2是备份到本地两个磁盘的备份目录;

3  backup_node3和backup_node4分别用作备份数据到局域网内的树莓派板子挂载的移动硬盘和DELL服务器;

4  Dell服务器可以通过PC远程启动和关闭,函数post_do可以用来备份数据完成后的后置设置,包括关闭Dell服务器电源。

 

其实吧,shell老手会发现,代码过于冗余,因为几个backup_node实在太像了,所以可以考虑封装一个公共的backup函数,利用位置参数进行传递,

完成备份主过程。 见文章实例  https://peloo.net/?p=1339