【shell脚本】全备份和增量备份Linux系统脚本auto_bak_system.sh
# 全备份:
[root@rhel8 shell]# tar -g /tmp/snapshot -czvf /tmp/20200610_full_bak.tar.gz /data/www1/
# 增量备份
[root@rhel8 www1]# tar -g /tmp/snapshot -czvf /tmp/2020_add_bak.tar.gz /data/www1/
每周日
其余工作日
[root@rhel8 shell]# cat auto_bak_system.sh
#!/bin/bash
# Automatic Backup Linux System Files
# Author tanbaobao 2020/06/10
# Define Variable
## 源目录
SOURCE_DIR=(
$*
)
## 目标目录
TARGET_DIR=/data/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 Backup Files or Directories\n---------------------------------
---------\n\nUsage: { $0 /boot /etc }\033[0m" exit
fi
# Determine whether the Target 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_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" == "0" ]&&echo -e "--------------------------------------------------------\n\033[32mThese Full
_Backup System Files Backup Successfully!\033[0m"fi
}
# Perform 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" == "0" ]&&echo -e "--------------------------------------------------------\n\033[32mThese Add_
Backup System Files $TARGET_DIR/$YEAR/$MONTH/$DAY/$FILES Backup Successfully!\033[0m"fi
}
sleep 3
Full_Backup;Add_Backup
使用:
# 指定要备份的目录即可
[root@rhel8 shell]# sh auto_bak_system.sh /tanbaobao/log/
不幸运的人也有被眷顾的权利