#定义变量
SOURCE_DIR=/opt/shell/
BACKUP_DIR=/tmp
YEAR=date +%Y
MONTH=date +%m
DAY=date +%d
WEEK=date +%u
A_TIME=date +%H%M
FILES=${A_TIME}_shell_backup.tar.gz
if [ ! -d $SOURCE_DIR ];then
echo "this $SOURCE_DIR is not exist..."
exit
fi
#判断备份目录是否存在,不存在则创建
if [ ! -d $BACKUP_DIR/$YEAR/$MONTH/$DAY ];then
mkdir -p $BACKUP_DIR/$YEAR/$MONTH/$DAY
echo -e "\033[32mthe $BACKUP_DIR/$YEAR/$MONTH/$DAY created successfully\033[0m"
fi
#定义周日进行全量备份函数
Full_backup()
{
if [ "$WEEK" -eq 7 ];then
rm -rf $BACKUP_DIR/snapshot
cd $BACKUP_DIR/$YEAR/$MONTH/$DAY ;tar -g $BACKUPDIR/snapshot -czvf FULL$FILES $SOURCE_DIR
if [ $? -eq 0 ];then
echo -e "----------------\n\033[32mthis Full_backup is successfully!\033[0m"
else
echo -e "----------------\n\033[32mthis Full_backup is Failed!\033[0m"
exit 0
fi
fi
}
#定义其他天进行增量备份函数
Add_backup()
{
if [ "$WEEK" -ne 7 ];then
cd $BACKUP_DIR/$YEAR/$MONTH/$DAY ;tar -g $BACKUPDIR/snapshot -czvf ADD$FILES $SOURCE_DIR
if [ $? -eq 0 ];then
echo -e "----------------\n\033[32mthis Add_backup is successfully!\033[0m"
else
echo -e "----------------\n\033[32mthis Add_backup is Failed!\033[0m"
exit 0
fi
fi
}
sleep 3
Full_backup;Add_backup
原文:https://blog.51cto.com/1009516/2426140