rsync - delete from desination

Posted on 2012-09-17
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

    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


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

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


    Author Comment

    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?
    LVL 68

    Expert Comment

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

    Author Comment

    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

    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 20

    Expert Comment

    by:Daniel McAllister
    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

    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.
    LVL 1

    Expert Comment

    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

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    The purpose of this article is to demonstrate how we can use conditional statements using Python.
    By default, Carbonite Server Backup manages your encryption key for you using Advanced Encryption Standard (AES) 128-bit encryption. If you choose to manage your private encryption key, your backups will be encrypted using AES 256-bit encryption.
    Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
    This tutorial will walk an individual through setting the global and backup job media overwrite and protection periods in Backup Exec 2012. Log onto the Backup Exec Central Administration Server. Examine the services. If all or most of them are stop…

    730 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now