#!/bin/bash #Auto Backup Linux System Files #by author rivers on 2021-09-28 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 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 $A_NAME$FILES ${SOURCE_DIR[@]} [ "$CODE" == "0" ]&&echo -e "-----------------------------------------\n\033[32mThese Add_Backup System Files $TARGET_DIR/$YEAR/$MONTH/$DAY/${YEAR}_$A_NAME$FILES Backup Successfully !\033[0m" fi } sleep 3 Full_Backup;Add_Backup
时间: 2023-09-15 21:22:52 浏览: 153
这是一个Linux系统文件自动备份脚本,可以备份指定的文件或目录。备份文件被打包成system_backup.tgz,并且按照年/月/日的目录结构存储在指定的目录下。该脚本还包括了全量备份和增量备份两种备份方式。全量备份在每周日进行,增量备份在其他时间进行。其中增量备份使用了tar的-g选项,可以根据快照文件只备份新增或修改的文件,从而减少备份的时间和存储空间。
阅读全文