当前位置: 代码网 > 服务器>服务器>Linux > shell实现目录增量备份的示例代码

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

2024年09月10日 Linux 我要评论
auto_backup_system.sh#针对目录进行备份;每周1-6执行增量备份,每周日(值为:0 or 7)执行全备份#!/bin/bash#automatic backup linux sys

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 目录增量备份内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网! 

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com