Shell+TAR实现增量备份

企业中日常备份的数据包括/boot、/etc、/root、/data目录等,备份的策略参考:每周1-6执行增量备份,每周日执行全备份。同时在企业中备份操作系统数据均使用Shell脚本完成,此处auto_backup_system.sh脚本供参考,后面章节会系统讲解Shell脚本,脚本内容如下:

#!/bin/bash

#Automatic Backup Linux System Files

#By Author www.jfedu.net

#Define Variables

SOURCE_DIR=(

$*

)

TARGET_DIR=/data/backup/

YEAR=`date +%Y`

MONTH=`date +%m`

DAY=`date +%d`

WEEK=`date +%u`

FILES=system_backup.tgz

CODE=$?

if

[ -z $SOURCE_DIR ];then

echo -e “Please Enter a File or Directory You Need to Backup:\n——————————————–\nExample $0 /boot /etc ……”

exit

fi

#Determine Whether the Target Directory Exists

if

[ ! -d $TARGET_DIR/$YEAR/$MONTH/$DAY ];then

mkdir -p $TARGET_DIR/$YEAR/$MONTH/$DAY

echo “This $TARGET_DIR Created Successfully !”

fi

#EXEC Full_Backup Function 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 `echo ${SOURCE_DIR[@]}`

[ “$CODE” == “0” ]&&echo -e “——————————————–\nFull_Backup System Files Backup Successfully !”

fi

}

#Perform incremental BACKUP Function Command

Add_Backup()

{

cd $TARGET_DIR/$YEAR/$MONTH/$DAY ;

if

[ -f $TARGET_DIR/$YEAR/$MONTH/$DAY/$FILES ];then

read -p “$FILES Already Exists, overwrite confirmation yes or no ? : ” SURE

if [ $SURE == “no” -o $SURE == “n” ];then

sleep 1 ;exit 0

fi

#Add_Backup Files System

if

[ $WEEK -ne “7” ];then

cd $TARGET_DIR/$YEAR/$MONTH/$DAY ;tar -g $TARGET_DIR/snapshot -czvf $$_$FILES `echo ${SOURCE_DIR[@]}`

[ “$CODE” == “0” ]&&echo -e “—————————————–\nAdd_Backup System Files Backup Successfully !”

fi

else

if

[ $WEEK -ne “7” ];then

cd $TARGET_DIR/$YEAR/$MONTH/$DAY ;tar -g $TARGET_DIR/snapshot -czvf $FILES `echo ${SOURCE_DIR[@]}`

[ “$CODE” == “0” ]&&echo -e “——————————————-\nAdd_Backup System Files Backup Successfully !”

fi

fi

}

Full_Backup;Add_Backup