• Linux Shell 自动备份脚本


    写一个使用shell脚本增量备份系统文件,顺便复习一下shell脚本相关的命令,这个脚本可以根据自己的需求来备份不同的文件或者文件夹,进行完整备份和增量备份。

    参考的网址:http://blog.51cto.com/wgkgood/1330200

    直接上脚本如下:

     1 #########################################################################
     2 # File Name: back_template.sh
     3 # Author: happy
     4 # mail: zaqwsx526@163.com
     5 # Created Time: Wed 06 Jun 2018 12:17:16 PM CST
     6 #########################################################################
     7 #!/bin/bash
     8 SOURCE_DIR=(
     9     $*
    10     )
    11 TARGET_DIR=/backup/data/
    12 YEAR=`date +%Y`
    13 MONTH=`date +%m`
    14 DAY=`date +%d`
    15 WEEK=`date +%u`
    16 FILES=system_backup.tgz
    17 CODE=$?
    18 if [ -z "$*" ];then
    19     echo -e "Please Enter Your Backup Files or Directories
    --------------------------------------------
    Example $0 /boot /etc ......"
    20     exit
    21 fi
    22 #Determine Whether the Target Directory Exists
    23 if [ ! -d $TARGET_DIR/$YEAR/$MONTH/$DAY ];then
    24     mkdir -p $TARGET_DIR/$YEAR/$MONTH/$DAY
    25     echo "This $TARGET_DIR is Created Successfully !"
    26 fi
    27 #EXEC Full_Backup Function Command""
    28 Full_Backup()
    29 {
    30     if [ "$WEEK" -eq "7" ];then
    31         rm -rf $TARGET_DIR/snapshot
    32         cd $TARGET_DIR/$YEAR/$MONTH/$DAY
    33         tar -g $TARGET_DIR/snapshot -czvf $FILES `echo ${SOURCE_DIR[@]}`
    34         [ "$CODE" == "0" ]&&echo -e  "--------------------------------------------
    These Full_Backup System Files Backup Successfully !"
    35     fi
    36 }
    37 #Perform incremental BACKUP Function Command
    38 Add_Backup()
    39 {
    40     cd $TARGET_DIR/$YEAR/$MONTH/$DAY ;
    41     if [ -f $TARGET_DIR/$YEAR/$MONTH/$DAY/$FILES ];then
    42         read -p "These $FILES Already Exists, overwrite confirmation yes or no ? : " SURE
    43         if [ $SURE == "no" -o $SURE == "n" ];then
    44             sleep 1 ;exit 0
    45         fi
    46         #Add_Backup Files System
    47         if [ $WEEK -ne "7" ];then
    48             cd $TARGET_DIR/$YEAR/$MONTH/$DAY ;tar -g $TARGET_DIR/snapshot -czvf $$_$FILES `echo ${SOURCE_DIR[@]}`
    49             [ "$CODE" == "0" ]&&echo -e  "-----------------------------------------
    These Add_Backup System Files Backup Successfully !"
    50         fi
    51     else
    52         if [ $WEEK -ne "7" ];then
    53             cd $TARGET_DIR/$YEAR/$MONTH/$DAY ;tar -g $TARGET_DIR/snapshot -czvf $FILES `echo ${SOURCE_DIR[@]}`
    54             [ "$CODE" == "0" ]&&echo -e  "-------------------------------------------
    These Add_Backup System Files Backup Successfully !"
    55         fi
    56     fi
    57 }
    58 Full_Backup;Add_Backup
    ================================================================================== = 黑发不知勤学早,白首方悔读书迟。 --颜真卿《劝学》
  • 相关阅读:
    阿里云重磅发布DMS数据库实验室 免费体验数据库引擎
    阿里云移动端播放器高级功能---直播时移
    图解SQL的inner join、left join、right join、full outer join、union、union all的区别
    索引覆盖分析
    case when 性能优化
    Eclipse断点种类
    Eclipse高效率开发技巧
    VS Code编辑器
    正则表达式
    JDBC编程之预编译SQL与防注入式攻击以及PreparedStatement的使用教程
  • 原文地址:https://www.cnblogs.com/happy1983/p/9154907.html
Copyright © 2020-2023  润新知