auto_backup_system.sh 

#针对目录进行备份;每周1-6执行增量备份,每周日(值为:0 or 7)执行全备份

#!/bin/bash
#Automatic Backup Linux System Files
#By Author xiaoheiyaoshangtian
#2024/x/x

source_dir=($*)
target_dir=/tmp
year=$(date +%Y)
month=$(date +%m)
day=$(date +%d)
week=$(date +%u)
files=system_backup.tar.gz
code=$?

if [ -z $source_dir ];then
  echo -e "Please Enter File or Directory You Need to Backup:\n--------------------------------------------------\nExample    $0 /boot /etc ......"
  exit
fi

#determin whether the target directory exists
if [ ! -d $target_dir/$year/$month/$day ];then
  mkdir -p $target_dir/$year/$month/$day
  echo "This $target_dir/$year/$month/$day created successfully!"
fi

#exec full backup function command
full_backup()
{
  if [ "$week" -eq "0" ];then
    rm -rf $target_dir/snapshot
    cd $target_dir/$year/$month/$day
    tar -g $target_dir/snapshot -czvPf $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(default yes)?:" sure
    if [ "$sure" == "no" -o "$sure" == "n" -o "$sure" == "N" -o "$sure" == "NO" -o "$sure" ==  "No" ];then
      sleep 1
      exit 0
    fi
    if [ $week -ne "0" ];then
    cd $target_dir/$year/$month/$day
    tar -g $target_dir/snapshot -czvPf $$_$files $(echo ${source_dir[@]})
    [ "$code" == "0" ]&&echo -e "---------------------------------------\nadd_backup system files backup successfully!"
    fi
  else
    if [ $week -ne "0" ];then
    cd $target_dir/$year/$month/$day
    tar -g $target_dir/snapshot -czvPf $files $(echo ${source_dir[@]})
    [ "$code" == "0" ]&&echo -e "---------------------------------------\nadd_backup system files backup successfully!"
    fi
  fi 
}

full_backup
add_backup
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.

验证:

[root@logstash ~]# sh auto_backup_system.sh
Please Enter File or Directory You Need to Backup:
--------------------------------------------------
Example    auto_backup_system.sh /boot /etc ......
[root@logstash ~]# 
[root@logstash ~]# sh auto_backup_system.sh /tmp
This /tmp/2024/06/21 created successfully!
tar: /tmp: Directory is new
tar: /tmp/.ICE-unix: Directory is new
tar: /tmp/.Test-unix: Directory is new
tar: /tmp/.X11-unix: Directory is new
tar: /tmp/.XIM-unix: Directory is new
tar: /tmp/.font-unix: Directory is new
tar: /tmp/2024: Directory is new
tar: /tmp/2024/06: Directory is new
tar: /tmp/2024/06/21: Directory is new
/tmp/
/tmp/.ICE-unix/
/tmp/.Test-unix/
/tmp/.X11-unix/
/tmp/.XIM-unix/
/tmp/.font-unix/
/tmp/2024/
/tmp/2024/06/
/tmp/2024/06/21/
/tmp/snapshot
/tmp/2024/06/21/system_backup.tar.gz
---------------------------------------
add_backup system files backup successfully!
[root@logstash ~]# 
[root@logstash ~]# sh auto_backup_system.sh /tmp
system_backup.tar.gz already exists,overwrite confirmation yes or no(default yes)?:no
[root@logstash ~]# 
[root@logstash ~]# sh auto_backup_system.sh /tmp
system_backup.tar.gz already exists,overwrite confirmation yes or no(default yes)?:
/tmp/
/tmp/.ICE-unix/
/tmp/.Test-unix/
/tmp/.X11-unix/
/tmp/.XIM-unix/
/tmp/.font-unix/
/tmp/2024/
/tmp/2024/06/
/tmp/2024/06/21/
/tmp/2024/06/21/2577_system_backup.tar.gz
---------------------------------------
add_backup system files backup successfully!
[root@logstash ~]#
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
[root@logstash tmp]# ls
2024  snapshot
[root@logstash tmp]# 
[root@logstash tmp]# cd 2024/
[root@logstash 2024]# cd 06/21/
[root@logstash 21]# ls
2577_system_backup.tar.gz  system_backup.tar.gz
[root@logstash 21]#
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.