Rsync backup one source directory and check agains multiple directories

给你一囗甜甜゛ 提交于 2021-02-07 20:36:57

问题


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]$ 

回答1:


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.

Here's my test and results:

$ 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/

but rather:

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,,}"/

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



来源:https://stackoverflow.com/questions/39392443/rsync-backup-one-source-directory-and-check-agains-multiple-directories

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!