Rsync upload local files without replacing remote files

This is basically one way sync- use at your own risk and dry run before any production deployment.

Suppose you have a list of files on a remote host, some of which already exist locally. What you want is to transfer only those files that are not found locally. If you blindly run scp with wildcard, it would fetch all remote files (existing as well as non-existing files), and overwrite existing local files. You want to avoid this.

In another similar situation, you may want to upload local files to a remote site, but without replacing any remote files. 

Using rsync:

If the local and remote hosts have rsync installed, using rsync will be the easiest way to copy only new files over, since rsync is designed for incremental/differential backups.

In this case, you need to explicitly tell rsync to skip any existing files during sync. Otherwise, rsync will try to use file modification time to sync two hosts, which is not what you want.

To download all remote files (over SSH) while skipping existing local files:

$ rsync -av --ignore-existing user@remote_host:/path/to/remote/directory/* /path/to/local/directory/

Similarly, to upload all local files (over SSH) without overwriting any duplicate remote files:

$ rsync -av --ignore-existing /path/to/local/directory/* user@remote_host:/path/to/remote/directory/

Sample command-

rsync -av --ignore-existing /opt/jf/tv-in/* root@



Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.