1,增量备份/root/shell文件,空间大于90不执行,小于90每天凌晨零点执行
tar -g /tmp/snapshot -czvf /tmp/2020_shellbak.gz /root/shell/
-g创建一个快照/tmp/snapshot 把/root/shell/备份到/tmp/2020_shellbak.gz
tar -g /tmp/snapshot -czvf /tmp/2020_add_shellbak.gz /root/shell/
把/root/shell/增量备份到/tmp/2020_add_shellbak.gz
http://blog.sina.com.cn/s/blog_4ac65f6f0100d4vu.html
#!/bin/bash
#zzz
#automatic backup linux system files
SOURCE_DIR=(
$*
)
TARGET_DIR=/date/backup
YEAR=`date +%Y`
MONTH=`date +%m`
DAY=`date +%d`
WEEK=`date +%u`
A_NAME=`date +%H%M`
FILES=${A_NAME}_system_backup.tgz
CODE=$?
if
[ -z "$*" ];then
echo -e "\033[32mUsage:\nPlease Enter your backfile\n---------------------------\nUsage:{$0 /boot /etc /root/shell}]\033[0m"
exit
fi
#determine whether the target director exists
if
[ ! -d $TARGET_DIR/$YEAR/$MONTH/$DAY ];then
mkdir -p $TARGET_DIR/$YEAR/$MONTH/$DAY
echo -e "\033[32mthe $TARGET_DIR/$YEAR/$MONTH/$DAY create successfull !\033[0m"
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 ${SOURCE_DIR[@]}
[ $CODE -eq 0 ] && echo -e "-----------------------------\n\033[32mtheose dir file backup successfully\033[0m"
fi
}
#preform incremental backup function command
Add_backup()
{
if
[ $WEEK -ne 7 ] ;then
cd $TARGET_DIR/$YEAR/$MONTH/$DAY ; tar -g $TARGET_DIR/snapshot -czvf $FILES ${SOURCE_DIR[@]}
[ $CODE -eq 0 ] && echo -e "-----------------------------\n\033[32mtheose dir file backup successfully\033[0m"
fi
}
sleep 3
##########################################
#test space
a=`df -h | grep /$ | awk '{print $5}' | sed s/%//g`
if
[ $a -le 90 ];then
echo -e "\033[32mthe space is enough $a\033[0m"
else
echo -e "\033[31mwarning,the space is not enough $a\033[0m"
exit
fi
Full_backup;Add_backup
然后开启定时任务crontab -e 0 0 * * * sh /root/shell/backupdir.sh /root/shell/