Question about rsync mirroring

OK, another Linux question. I’ve been looking into the rsync command. And I see that it can copy only files that has changed and the new files that were created. But to truly keep the files in sync (mirrored) does rsync remove the deleted files?

1 Like

That is the first step to using rsync.

Last time I checked, to make a mirror (i.e. allowing to also delete files on the destination) you just have to choose the appropriate command line options for the rsync command.
You can read the rsync manual here.

1 Like

this was one of the first questions i had when thinking about using rsync as well. i added the --delete option to mine after the single letter options. so mine is rsync -rahP --delete <source> <destination>. i’m pretty sure i got that from the arch wiki:

https://wiki.archlinux.org/index.php/rsync

2 Likes

Thanks Cord, this worked but I had a problem. I did not know how to fix. My command was:
rsync -rahPq --delete new old
Before the command both new and old had the same directories. After the command old had a new directory call new. So instead of updating old, it seem that new was copied to old. Is it b/c the destination had different name then source?

1 Like

without seeing the exact text of your command, i believe the answer is yes. for example, when i execute rsync -rahP --delete /home/user/tacos media/user/usb1, the tacos directory is copied onto my usb. it helps me to think of rsync as an advanced version of cp (copy) like in the arch wiki quote below.

if you want all of the contents and directories from (for example) /home/user/new to be rsynced into a directory called old on a usb (also just an example) located at /media/user/usb1/old then it sounds like you want to use a trailing slash on your source after new like so: rsync -rahPq --delete /home/user/new/ /media/user/usb1/old

Trailing slash caveat

Arch by default uses GNU cp (part of GNU coreutils). However, rsync follows the convention of BSD cp, which gives special treatment to source directories with a trailing slash “/”. Although

$ rsync -r source destination

creates a directory “destination/source” with the contents of “source”, the command

$ rsync -r source/ destination

copies all of the files in “source/” directly into “destination”, with no intervening subdirectory - just as if you had invoked it as

$ rsync -r source/. destination

This behavior is different from that of GNU cp, which treats “source” and “source/” identically (but not “source/.”). Also, some shells automatically append the trailing slash when tab-completing directory names. Because of these factors, there can be a tendency among new or occasional rsync users to forget about rsync’s different behavior, and inadvertently create a mess or even overwrite important files by leaving the trailing slash on the command line.

1 Like

This was my command.

I will update this post this afternoon.

BINGO! Adding the slashes work. Thanks for your help.
New command;
rsync -rahPq --delete /home/easyt50/Story/new/ old

1 Like

my apologies :slight_smile: i wasn’t reading your post literally enough. i assumed you were using shorthand for “new location” and “old location”. i just over-interpreted :grimacing:

1 Like

i’m glad to hear that helped :slight_smile:

Found this and it looks interesting. File sync using GUI. Have not tried it yet, but here is the link.

1 Like

Rsync is very particular about trailing slashes
There is a difference between
old being /old, /old/ and /old/*
new has some trivial rules too.

3 Likes

did you try it out and see if you liked it at all?

I ran the program and was impressed by all the options. I ran the compare against two files and it seem to find all the differences. Because I had not copied the files for a test run, I did not let the program updated the file.

1 Like

I’ve been using Grsync for a few years now, another GUI for rsync. No problems with it. It shows you the command line your GUI settings create.

3 Likes

Before I got my head around BTSync / Resilio Sync - I used to use scripts that ran “unison” - it’s kinda like rsync…

https://www.cis.upenn.edu/~bcpierce/unison/

One issue with Unison however - you need to have nearly or exactly the same versions on each side… e.g. Ubuntu default version is usually a bit more recent than Debian’s version…

1 Like

I don’t remember the exact reason anymore, but many years back I had to use unison, because it had a special feature that rsync did not have.