zoukankan      html  css  js  c++  java
  • 【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 "33[32mUsage:
    Please Enter Your Backup Files or Directories
    ---------------------------------
    ---------
    
    Usage: { $0 /boot /etc }33[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 "33[32mThe $TARGET_DIR Created Successfully!33[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 "--------------------------------------------------------
    33[32mThese Full
    _Backup System Files Backup Successfully!33[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 "--------------------------------------------------------
    33[32mThese Add_
    Backup System Files $TARGET_DIR/$YEAR/$MONTH/$DAY/$FILES Backup Successfully!33[0m"fi
    }
    
    sleep 3
    Full_Backup;Add_Backup

    使用:

    # 指定要备份的目录即可
    [root@rhel8 shell]# sh auto_bak_system.sh /tanbaobao/log/
  • 相关阅读:
    OAuth2 协议原理简析及Azure AD OAuth2示例
    MySQL Innodb MVCC(多版本并发控制)
    乐观锁和悲观锁
    数据库索引
    SQL盲注
    JAVA array to list and list to array
    缓存穿透,缓存雪崩和缓存击穿
    四、Spring Cloud 之旅 -- Ribbon 负载均衡
    JavaScript中 require、import 有什么区别?
    vue 2.x Vue 3.x 日常采坑之 设置alias别名、background引入图片、全局引入scss文件 的问题
  • 原文地址:https://www.cnblogs.com/HeiDi-BoKe/p/13086930.html
Copyright © 2011-2022 走看看