我正在尝试复制(rsync)" sdir"目录到tdir目录取决于它的日期。我想要一个完整的副本到mon目录,仅仅改变了什么,只修改了改变了等等,但我没有得到我需要的结果。 请参阅我运行的实际命令:
[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]$
答案 0 :(得分:1)
我注意到rsync手册页明确指出" 从版本2.6.4开始,可以提供多个--link-dest
目录",并且' ; --compare-dest
的类似注释,但--backup-dir
没有(这是有意义的)。如果您希望将备份限制为仅包含许多其他目录中的内容,那么--*-dest
选项就可以了。
这是我的测试和结果:
$ 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
$
这里的显着区别是每个的比较目录都需要自己的选项。符号不是:
rsync --compare-dest one/ two/ src/ dest/
而是:
rsync --compare-dest=one/ --compare-dest=two/ src/ dest/
要将其放入每天运行的代码中,您可以尝试类似以下内容(未经测试的脚本):
#!/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的功能。