rsync - delete from desination

Posted on 2012-09-17
Medium Priority
Last Modified: 2013-12-21
I need to syncronise 2 folders between servers, serverA always has the right files and folder structure on it, however server B can have files and folders from that need to be deleted and/or replaced by the folder and file structure on ServerA.

Although I understand I could delete and start again, the idea is the users can do whatever they want during the day (create modify and delete) then overnight everything is replaced with the copy from ServerA.

We have limited bandwith between sites, so dont want to delete everything and then copy the folder structure again overnight.

Ive been reading up, and it seems RSync is going to be the best thing to use, however I cannot find anything about deleting from the destination if its not on the serverA

Does anyone have any notes or samples from what they have done in the past?

Thank you
Question by:tonelm54
LVL 68

Expert Comment

ID: 38405566
Did you try the "--delete" option?

It's meant to delete extraneous files from destination directories,
which seems pretty much what you're after.

LVL 10

Expert Comment

ID: 38405575

rsync --delete -av /home/src/ /home/dest/

This should delete files and folder on the destination that don't match


Author Comment

ID: 38405644
Ok, trying the command:-
rsync -avv -delete /home/tcross/sync/folder1/ /home/tcross/sync/folder2/

Open in new window

In folder1 I have:-

However after I have issued the command
rsync -avv -delete /home/tcross/sync/folder1/ /home/tcross/sync/folder2/

Open in new window

I have in folder2:-

Which obviously hasnt replicated the folder stucture correctly :-(

Any ideas?
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

LVL 68

Expert Comment

ID: 38405662
In order to delete even nonempty subdirectories on the target you must additionally specify the "--force" option.

Author Comment

ID: 38405718
rsync -avv -delete --force /home/tcross/sync/folder1/ /home/tcross/sync/folder2/

Open in new window

Still gives the same structure.

The output of the command is:-
sending incremental file list
delta-transmission disabled for local transfer or --whole-file
total: matches=0  hash_hits=0  false_alarms=0 data=0

sent 83 bytes  received 23 bytes  212.00 bytes/sec
total size is 0  speedup is 0.00
LVL 11

Expert Comment

ID: 38418079
Pardon me asking again,

have you given the --delete? The rsync should fail if the option passed to it are not right but just asking

rsynv -rvlpogt --delete /source/ /destination/ --progress

Just to make sure it is working, try to run an rsync on a different directory such as /tmp without delete. Then make changes to the destination and pass the --delete option again to see if it works,

rsync -rvlpogt /source/ /tmp/ --progress
Once you make sure /source/a and /tmp/a has the same structure, make some changes to /tmp/a and run,
rsync -rvlpogt --delete /source/a /tmp/a --progress
See if they are mirrored.
LVL 21

Expert Comment

by:Daniel McAllister
ID: 38419533
From what I've seen from your postings, you've partially got the right syntax, but your source/destination settings appear to have you messed up.

This is COMMON with rsync!

So, since you're rsyncing folders, you want to make SURE that the folder contents of one are not being rsynched with a SUBfolder in the target, but rather with the target itself.

For example, let's say I have a folder I want to be the EXACT SAME on two servers (essentially a remote mirror, with one being the master).

rsync -v --delete -r -u -l -pogt -z ${FPATH}/ ${SITE}:${FPATH}/

Open in new window

NOTE: by ensuring that BOTH of the paths are shown to be folders, rsync will know that the folders themselves are to be synched, not a folder within the destination folder. (Essentially, you remove the ambiguity).

I hope this helps!


PS: I'll leave it to you to read up on the OTHER options that I place in my rsync scripts! :-)
LVL 40

Accepted Solution

jlevie earned 2000 total points
ID: 38420562
To have folder2 on the remote be exactly the same as folder1 use:

rsync -av -delete /home/tcross/sync/folder1/* /home/tcross/sync/folder2

But if the folder names are the same on both the local and remote servers use:

rsync -avv -delete /home/tcross/sync/folder1 remote:/home/tcross/sync

See "man rsync" for and explanation of the differences in those commands.

Expert Comment

ID: 38420633
Expanding on Dan's post regarding target and subfolder.

It is important to understand how rsync behaves with or without a trailing slash (/).  Otherwise, you'll fall victim to the old What? I just copied my dir1 over and got dir1/dir1. scenario.

A trailing slash instructs rsync to work on the contents of the directory, kind of like saying:  scp -rp /dir1/*  remote:/dir1/

If you don't have write access to the parent directory but you do have permission to the destination target directory, use a slash.  

# Copy your home directory to another box.
export RSYNC_RSH="ssh -2"     # or use -e ssh option.
rsync -av /export/home/me/  remote:/export/home/me/.
rsync -av /export/home/me   remote:/export/home/.

Open in new window

# Promote a bunch of application files to clobber a bad build
rsync -av /var/tmp/CHG/599237  remote:/var/tmp/staging
ssh remote
su - app_id
rsync -av /var/tmp/staging/599237/  /software/webapps/my.war/WEB-INF/lib/.

Open in new window


Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Welcome back to our beginners guide of the popular Unix tool, cron. If you missed part one where we introduced this tool, the link is below. We left off learning how to build a simple script to schedule automatic back ups. Now, we’ll learn how to se…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This tutorial will show how to configure a single USB drive with a separate folder for each day of the week. This will allow each of the backups to be kept separate preventing the previous day’s backup from being overwritten. The USB drive must be s…
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question