linux - 递归地创建文件夹的新 tar 文件,排除另一个 tar 文件中包含的文件

标签 linux bash unix tar

我每天都会备份我的邮件服务器电子邮件。

每天晚上更新一个 tar 文件以包含当天的新电子邮件,并通过 ftp 下载到备份服务器。

随着时间的推移,这个 tar 文件变得太大而无法处理。

为了解决这个问题,我决定最好创建一个新的 tar 文件,其中包含所有邮件,减去昨晚备份中已存在的邮件。所以最终只有当天的新文件。

然后我可以将这个小得多的文件传输到我的备份服务器。

最后,我可以将新的 tar 文件合并到两台服务器上的主服务器中,为第二天做好准备。

我有什么想法可以做到这一点吗?

最佳答案

tarfind 结合使用 以下是使用 findtar

创建完整增量备份的示例脚本
#!/bin/bash
bkdir="/home/backup"
bklog="/var/log/backup.log"
dbkdir="/home/backup/files/daily"
wbkdir="/home/backup/files/weekly"


curdate=`date +%Y-%M-%d-%H:%M:%S`
ardate=`echo $curdate  | sed -e 's/:/_/g' -e 's/-/_/g'`
wday=`date +%a`

files_full_backup () {
  echo -e "Archiving files...n"
  tar cjpf "$1/full_files_$ardate.tar.bz2" "$bkdir"
}

files_inc_backup () {
  echo -e "Archiving files...n"
  find $bkdir -mtime -1 -exec tar cvjpf "$1/inc_files_$ardate.tar.bz2" {} ;
}

### add some choice what kind of backup to do - full or incremental
if [ $wday != Sun ]
  then
    echo -e "As today is not Sunday - I'll start incremental backup.n"
    files_inc_backup $dbkdir
  else
    echo -e "As today is Sunday - I'll start full backup.n"
    files_full_backup $wbkdir
fi

仅使用tar(增量备份)

man tar 显示它具有“增量功能”:

-g, --listed-incremental=FILE
              Handle new GNU-format incremental backups.  FILE is the name of a snapshot file, where tar stores addi‐
              tional information which is used to decide which files changed since the previous incremental dump and,
              consequently, must be dumped again.  If FILE does not exist when creating an archive, it will  be  cre‐
              ated  and  all  files will be added to the resulting archive (the level 0 dump).  To create incremental
              archives of non-zero level N, create a copy of the snapshot file created during the level N-1, and  use
              it as FILE.

              When listing or extracting, the actual contents of FILE is not inspected, it is needed only due to syn‐
              tactical requirements.  It is therefore common practice to use /dev/null in its place.

要创建增量备份,请使用:

tar --create --file=`date +%s`.tbz2 --bzip --listed-incremental=example.snar --verbose example/

或简称:

   tar -cvjg example.snar -f `date +%s`.tbz2  example/

要恢复备份,需要将 bcakup 的所有部分从最旧到最新解压:

 tar --extract --incremental --file level0.tar
 tar --extract --incremental --file level1.tar
 tar --extract --incremental --file level2.tar

或者,简而言之:

   for i in *.tbz2; do tar -xjGf "$i"; done;

这是一个每周一次(或每月一次,取决于注释行)创建零级存档的脚本:

#!/bin/sh
   SOURCE="$1"
   test -d "$SOURCE" || exit 1

   DEST_DIR=`date +%G-%V`; #weekly
   #DEST_DIR=`date +%Y-%m`; #monthly

   mkdir -p $DEST_DIR;
   shift;
   tar --create "$@" --preserve-permissions --totals --bzip \
   --file="$DEST_DIR"/`date +%F-%s`.tbz2 \
   --listed-incremental="$DEST_DIR"/backup.snar \
   --no-check-device --exclude-vcs \
   --exclude-tag-under=access.log --exclude='*.log' \
   --exclude-caches --exclude-tag-under=IGNORE.TAG "$SOURCE"

并执行它:

   ./backup.sh example/ -v

关于linux - 递归地创建文件夹的新 tar 文件,排除另一个 tar 文件中包含的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53279483/

相关文章:

c - 如何以原始用户身份从 sudo 程序中执行命令?

c - 确定 CPU 利用率 - Solaris unix

linux - 如何比较 unix 中的两个文件并合并包含匹配和不匹配数据的文件 1 和 2

regex - 从 ls 输出中获取文件大小

ruby-on-rails - 在一行中使用端口查找并杀死 PID

python - 在shell脚本中捕获python脚本抛出的异常

C/C++程序同时连接32位DB2和64位DB2

linux - gdate 在这个 shell 脚本中是什么意思?

linux - 如何使用 shell 脚本从 dhcp.lease 文件中提取 IP 地址、MAC 地址和名称?

linux - 非阻塞 I/O 问题