Rsync备份一个源目录并再次检查多个目录 [英] Rsync backup one source directory and check agains multiple directories

查看:35
本文介绍了Rsync备份一个源目录并再次检查多个目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据日期将"sdir"目录复制(rsync)到tdir目录.我想要一个完整的副本到mon目录,仅调整哪些已更改,仅调整哪些已更改,等等,但我没有得到所需的结果.请参阅我运行的实际命令:

I am trying to copy(rsync) the "sdir" directory to tdir directories depending on what day it is. I want one full copy to mon directory, tues only what changed, wed only what changed, etc. but I am not getting the results I need. Refer to actual command I ran:

[jesse@localhost test]$ tree
.
├── sdir
│   ├── file1
│   ├── file2
│   ├── file3
│   ├── file4
│   └── file5
└── tdir
    ├── fri
    ├── mon
    │   ├── file1
    │   └── file2
    ├── thu
    ├── tue
    │   └── file3
    └── wed
        ├── file3
        ├── file4
        └── file5

7 directories, 11 files
[jesse@localhost test]$ touch sdir/file6
[jesse@localhost test]$ rsync -av --dry-run sdir/ --compare-dest=/home/jesse/test/tdir/mon/ /home/jesse/test/tdir/tue/ /home/jesse/test/tdir/wed/ /home/jesse/test/tdir//thu/ --backup-dir=/home/jesse/test/tdir/fri/
sending incremental file list
./
file3
file4
file5
file6

sent 294 bytes  received 31 bytes  650.00 bytes/sec
total size is 0  speedup is 0.00 (DRY RUN)
[jesse@localhost test]$ rsync -av --dry-run sdir/ --compare-dest=/home/jesse/test/tdir/mon/ /home/jesse/test/tdir/tue/ /home/jesse/test/tdir/wed/ /home/jesse/test/tdir//thu/ --backup-dir=/home/jesse/test/tdir/thu/
sending incremental file list
./
file3
file4
file5
file6

sent 294 bytes  received 31 bytes  650.00 bytes/sec
total size is 0  speedup is 0.00 (DRY RUN)
[jesse@localhost test]$ rsync -av --dry-run sdir/ --compare-dest=/home/jesse/test/tdir/mon/ /home/jesse/test/tdir/tue/ /home/jesse/test/tdir/wed/ --backup-dir=/home/jesse/test/tdir/thu/
sending incremental file list
./
file6

sent 208 bytes  received 22 bytes  460.00 bytes/sec
total size is 0  speedup is 0.00 (DRY RUN)
[jesse@localhost test]$ rsync -av --dry-run sdir/ --compare-dest=/home/jesse/test/tdir/mon/ /home/jesse/test/tdir/tue/ /home/jesse/test/tdir/wed/ /home/jesse/test/tdir/thu/ --backup-dir=/home/jesse/test/tdir/thu/
sending incremental file list
./
file3
file4
file5
file6

sent 294 bytes  received 31 bytes  650.00 bytes/sec
total size is 0  speedup is 0.00 (DRY RUN)
[jesse@localhost test]$ rsync -av  sdir/ --compare-dest=/home/jesse/test/tdir/mon/ /home/jesse/test/tdir/tue/ /home/jesse/test/tdir/wed/ /home/jesse/test/tdir/thu/ --backup-dir=/home/jesse/test/tdir/thu/
sending incremental file list
./
file3
file4
file5
file6

sent 438 bytes  received 95 bytes  1,066.00 bytes/sec
total size is 0  speedup is 0.00
[jesse@localhost test]$ tree
.
├── sdir
│   ├── file1
│   ├── file2
│   ├── file3
│   ├── file4
│   ├── file5
│   └── file6
└── tdir
    ├── fri
    ├── mon
    │   ├── file1
    │   └── file2
    ├── thu
    │   ├── file3
    │   ├── file4
    │   ├── file5
    │   └── file6
    ├── tue
    │   └── file3
    └── wed
        ├── file3
        ├── file4
        └── file5

7 directories, 16 files
[jesse@localhost test]$ 

推荐答案

我注意到rsync手册页明确指出"从2.6.4版本开始,多个-link-dest 可能会提供目录",对于-compare-dest 也有类似的注释,但对于-backup-dir 却没有类似的注释(这很有意义).如果要将备份限制为仅包含不在许多其他目录之一中的内容",则可以使用-*-dest 选项.

I note that the rsync man page clearly states that "Beginning in version 2.6.4, multiple --link-dest directories may be provided", and there's a similar note for --compare-dest but not for --backup-dir (which makes sense). If you want to restrict your backup to "only things that are not in one of a number of other directories", the --*-dest options are the way to go.

这是我的测试和结果:

$ mkdir src mon tue wed
$ for n in {1..3}; do date > src/file$n; sleep 1; done
$ rsync -avi src/ mon/
building file list ... done
.d..t.... ./
>f+++++++ file1
>f+++++++ file2
>f+++++++ file3

sent 346 bytes  received 92 bytes  876.00 bytes/sec
total size is 87  speedup is 0.20
$ for n in {4..5}; do date > src/file$n; sleep 1; done
$ rsync -avi --compare-dest=$PWD/mon/ src/ tue/
building file list ... done
.d..t.... ./
>f+++++++ file4
>f+++++++ file5

sent 295 bytes  received 70 bytes  730.00 bytes/sec
total size is 145  speedup is 0.40
$ for n in {6..7}; do date > src/file$n; sleep 1; done
$ rsync -avi --compare-dest=$PWD/mon/ --compare-dest=$PWD/tue/ src/ wed/
building file list ... done
.d..t.... ./
>f+++++++ file6
>f+++++++ file7

sent 319 bytes  received 70 bytes  778.00 bytes/sec
total size is 203  speedup is 0.52
$

此处的显着区别是,每个比较目录都需要使用自己的选项.表示法不是:

The notable difference here is that each of the compare directories needs its own option. The notation is not:

rsync --compare-dest one/ two/ src/ dest/

但是:

rsync --compare-dest=one/ --compare-dest=two/ src/ dest/

要将其放入每天运行的代码中,您可以尝试执行以下操作(未经测试的脚本):

To put this into code that gets run each day, you might try something like the following (untested script):

#!/usr/bin/env bash

# Store our rsync arguments in an array, for easier handling.
rsync_args=(-a)

# If we're running interactively, show us rsync verbose output.
if [ -t 0 ]; then
  rsync_args+=(-vi)
fi

# Step through weekdays, adding days to our argument list, stopping at "today"
read today < <(date '+%a')
for day in Mon Tue Wed Thu Fri; do
  [ "$day" = "$today" ] && break
  rsync_args+=(--compare-dest=$PWD/${day,,})
done

# And finally, copy everything.
rsync "${rsync_args[@]}" src/ "${day,,}"/

请注意,bash在参数扩展中的大小写控制( $ {s,} $ {s ^^} )是bash版本4的功能.

Note that bash's case control in parameter expansion (${s,,} and ${s^^}) are features of bash version 4.

这篇关于Rsync备份一个源目录并再次检查多个目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆