shell实现目录增量备份的示例代码

 更新时间:2024年08月28日 10:37:28   作者:小黑要上天  
本文主要介绍了shell实现目录增量备份的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

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

验证:

[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 ~]# 
[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]# 

到此这篇关于shell实现目录增量备份的示例代码的文章就介绍到这了,更多相关shell 目录增量备份内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家! 

相关文章

  • shell命令while循环中使用sleep命令代码示例

    shell命令while循环中使用sleep命令代码示例

    这篇文章主要介绍了shell命令while循环中使用sleep命令代码示例,分享了相关代码示例,小编觉得还是挺不错的,具有一定借鉴价值,需要的朋友可以参考下
    2018-02-02
  • Linux Bash脚本中的IFS的作用

    Linux Bash脚本中的IFS的作用

    ​​在处理文本数据时,Bash 将文本分割成多个字段,这些字段之间由 IFS 指定的字符进行分隔,本文主要介绍了Linux Bash脚本中的IFS的作用,具有一定的参考价值,感兴趣的可以了解一下
    2024-05-05
  • Linux下的fdisk指令用法场景分析

    Linux下的fdisk指令用法场景分析

    在Linux系统中,对磁盘进行分区是管理和利用磁盘空间的重要任务之一,本篇将以通俗易懂的方式,详细介绍fdisk指令的前世今生、功能、用法和应用场景,帮助读者全面了解fdisk指令,并掌握其在磁盘管理和问题排查方面的应用,需要的朋友可以参考下
    2024-01-01
  • ps命令输出进程状态S+的含义解析

    ps命令输出进程状态S+的含义解析

    这篇文章主要介绍了ps命令输出进程状态S后面加号的含义,本文通过ps命令输出说明,感兴趣的朋友跟随小编一起看看吧
    2019-12-12
  • shell命令之mv的具体使用

    shell命令之mv的具体使用

    本文主要介绍了shell命令之mv的具体使用,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-02-02
  • Linux下的tar压缩解压缩命令详解(小结)

    Linux下的tar压缩解压缩命令详解(小结)

    这篇文章给大家分享了linux下的tar 压缩解压缩命令,非常不错,具有参考借鉴价值,需要的朋友参考下
    2017-03-03
  • linux反弹shell的原理详解

    linux反弹shell的原理详解

    这篇文章主要介绍了linux反弹shell的原理详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-10-10
  • 基于shell的if和else详解

    基于shell的if和else详解

    下面小编就为大家带来一篇基于shell的if和else详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-06-06
  • 修改Apache配置指定php配置文件php.ini的位置方法

    修改Apache配置指定php配置文件php.ini的位置方法

    下面小编就为大家带来一篇修改Apache配置指定php配置文件php.ini的位置方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-02-02
  • shell脚本去重的三种方法小结

    shell脚本去重的三种方法小结

    在实际工作中,进行Shell去重操作是非常常见的任务之一,本文主要介绍了shell脚本去重的三种方法小结,具有一定的参考价值,感兴趣的可以了解一下
    2023-12-12

最新评论