Link to home
Start Free TrialLog in
Avatar of tonelm54
tonelm54

asked on

rsync - delete from desination

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
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Did you try the "--delete" option?

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

wmp
Dear,

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

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

Regards
Avatar of tonelm54
tonelm54

ASKER

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

Open in new window


In folder1 I have:-
folder1
     \bye
     \hi

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:-
folder2
     \bye
     \byeNot
     \folder1
          \bye
          \hi
     \folder2
          \folder1
               \bye
               \hi
          \hi

Which obviously hasnt replicated the folder stucture correctly :-(

Any ideas?
In order to delete even nonempty subdirectories on the target you must additionally specify the "--force" option.
So,
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
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,

Eg.
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.
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).

FPATH=/path/to/folder
SITE=remote.site.url
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!

Dan
IT4SOHO

PS: I'll leave it to you to read up on the OTHER options that I place in my rsync scripts! :-)
ASKER CERTIFIED SOLUTION
Avatar of jlevie
jlevie

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.  

Example:
# 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