6

I have two directories that I'd like to keep in sync.

For backup purposes I'd like to run the rsync command such that it will copy new or changed files to a separate USB drive (or another directory) which I can review prior to completing the sync.

In the end I want:

  1. original directory
  2. backup directory (untouched by this command other than to look to see what's there)
  3. separate incremental directory that contains only the new/changed files.

Is this possible?

  • Would you accept an answer that doesn't create the intermediate directory but does allow speculative viewing of what would happen if the rsync did run? – cryptarch Dec 12 '18 at 21:18
  • Never mind, I've added an answer that should cover whatever your needs are. – cryptarch Dec 12 '18 at 21:34
  • I just wondered whether you would consider marking my Answer as correct? Some time has passed and it seems like no one else is willing or able to propose a better solution. – cryptarch Dec 21 '18 at 23:21
2

If you only need to see which files will be affected, without seeing the differences between them, you can use the --dry-run option to rsync. Let's set up a sandpit for testing:

$ cd /tmp
$ mkdir -p testing/{a,b}
$ cd testing/
$ touch a/hello a/world
$ ls a
hello
world
$ rsync -rv --append-verify a/ b
$ ls b
hello
world

Now perform modifications to the contents of a:

$ echo 123 > a/hello 
$ touch a/abc

Now use rsync ... --dry-run ... to see what would happen:

$ rsync -rv --append-verify --dry-run a/ b
sending incremental file list
abc
hello

sent 103 bytes  received 22 bytes  250.00 bytes/sec
total size is 4  speedup is 0.03 (DRY RUN)

We can see no changes were actually made to b:

$ ls b
hello
world
$ cat b/hello
$

If you need to see the differences between the directories you can use diff:

$ /bin/diff -aurN a b
diff -aurN a/hello b/hello
--- a/hello 2018-12-13 08:16:23.376761456 +1100
+++ b/hello 2018-12-13 08:16:11.306761686 +1100
@@ -1 +0,0 @@
-123

If b is on a remote box and you need to see the differences, you'll need to create a local copy of b and then you can diff.

  • 1
    That's helpful - I'll have to parse the output and run a second script based on that to copy it elsewhere. Wish there was a one-liner rsync command, but that may not exist. I'll keep it open for a bit for other answers to crop up. – justin Dec 13 '18 at 0:30
  • 1
    @cryptarch Learned something useful, thks to line-by-line walk thru. – horaceT Dec 21 '18 at 22:39
  • 1
    This is helpful, but I think it does not answer the question. The OP wanted to directly perform the operation not to gather the information needed to do it. I believe a better answer is serverfault.com/a/508272/173599. – alexis Nov 29 '19 at 19:02
0

I believe a better answer is in https://serverfault.com/a/508272/173599. Here I quote the relevant command:

rsync -aHxv --compare-dest=folder2/ folder1/ folder3/

To compare folder1 with folder2 as destination, but copy instead in folder3.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.