shell编程实战系统备份脚本

常用备份数据方案:
每周日进行完整备份,周一至周六使用增量备份;
每周六进行完整备份,周日至周五使用增量备份。
企业备份数据的工具主要有tar、cp、rsync、scp、sersync、dd等工具,基于开源tar工具实现系统数据备份方案:
tar 工具手动全备份网站,-g参数指定新的快照文件:
tar -g/tmp/snapshot  -czvf/tmp/2018_full_system_data.tar.gz /data/sh/
tar 工具手动增量备份网站,-g参数指定全备已生成的快照文件,后续增量备份基于上一个增量备份快照文件:
tar -g /tmp/snapshot-czvf /tmp/2018_add01_system_data.tar.gz /data/sh/
tar工具全备、增量备份网站,shell脚本实现自动打包备份,编程思路:
系统备份数据按每天存放;
创建完整备份函数块;
创建增量备份函数快;
根据星期数判断完整或增量;
将脚本加入crontab实现自动备份。
tar 工具全备、增量备份网站,shell脚本实现自动打包备份:
#!/bin/bash
#Auto Backup LinuxSystem Files
#By author tang.com2018
#Define Path variables
SOURCE_DIR=(
$*
)
TARGET_DIR=/data/backup
YEAR=`date +%Y`
MONTH=`date +%m`
DAY=`date +%d`
WEEK=`date +%u`
A_NAME=`date +%H%M`
FILES=system_backup.tgz
CODE=$?
if [ -z "$*"];then
echo-e "\033[32mUsage:\nPlease Enter Your Backup Files orDirectories\n-----------------------\n\nUsages:{ $0 /boot /etc }\033[0m"
exit
fi
#Determine Whether theTarget Directory Exists
if  [ ! -d $TARGET_DIR/$YEAR/$MONTH/$DAY ];then
mkdir-p $TARGET_DIR/$YEAR/$MONTH/$DAY
echo-e "\033[32mThe $TARGET_DIR Created Successfully !\033[0m"
fi
#EXEC Full_BackupFunction Command
Full_Backup()
{
if [ "$WEEK"-eq "7" ];then
rm-rf $TARGET_DIR/snapshot
cd$TARGET_DIR/$YEAR/$MONTH/$DAY;tar -g $TARGET_DIR/snapshot -czvf $FILES${SOURCE_DIR[@]}
    [ "$CODE" == "0"]&&echo -e "---------------------------------\n\033[32mTheseFull_Backup System Files Backup Successfully !\033[0m"
fi
}
#Perform incrementalBACKUP Function Command
Add_Backup()
{
if [ $WEEK -ne"7" ];then
cd$TARGET_DIR/$YEAR/$MONTH/$DAY;tar -g $TARGET_DIR/snapshot -czvf $A_NAME$FILES${SOURCE_DIR[@]}
   [ "$CODE" == "0"]&&echo -e "---------------------------------\n\033[32mTheseAdd_Backup System Files $TARGET_DIR/$YEAR/$MONTH/$DAY/${YEAR}_$A_NAME$FILESBackup Successfully !\033[0m"
fi
}
sleep 3
Full_Backup;Add_Backup
crontab任务计划中添加如下语句,每天凌晨1点整执行备份脚本即可:
0 1 * * * /bin/sh/data/sh/auto_backup.sh /boot /etc/ >> /tmp/back.log 2>&1