Sync files with rsync
Published
Updated
rsync
Sync only what changed, fast and safe.
rsync copies only the files that changed, over SSH. Use it to sync uploads and content between the local machine and fortrabbit, next to git deployment or instead of SFTP.
About rsync
rsync is a command line tool that synchronizes folders. It compares source and destination and transfers only new and changed files, over SSH with the SSH key of the account. Run it on the local machine, not on the environment. Linux and macOS ship with it, check with rsync --version. On Windows, use it in WSL.
A wrong option or a swapped source and destination can overwrite or delete files. Run a dry run first.
Use cases
- Next to git deployment: sync what git does not track, such as uploads, assets and CMS content. Add these folders to the exclude patterns, or a deployment with the replace strategy deletes them.
- Instead of SFTP: upload a whole project and later only the changes, in one command.
With the frbit CLI
frbit files pull and frbit files push of the frbit CLI run rsync -az over SSH and fill in user and host. Run them from the project root:
# DOWN: fortrabbit to local folder
$ frbit files pull web/uploads -e {{app-env-id}}
# UP: local folder to fortrabbit, by app and environment name
$ frbit files push web/uploads -e {{app-name}}/{{app-env-name}}
# Preview, then sync and delete what is gone on the environment
$ frbit files pull web/uploads -e {{app-env-id}} --dry-run --delete
There is one path, not a source and a destination: web/uploads syncs /data/www/web/uploads on the environment with ./web/uploads locally. Without a path, the whole project folder syncs. --dry-run, --delete and --exclude are the only rsync options it passes on. A push asks to type the environment ID first. Use plain rsync for other options or for different paths on each side.
Commands
# UP: local folder to fortrabbit
rsync -av ./web/uploads/ {{app-env-id}}@ssh.{{region}}.frbit.app:web/uploads/
# DOWN: fortrabbit to local folder
rsync -av {{app-env-id}}@ssh.{{region}}.frbit.app:web/uploads/ ./web/uploads/
The first path is the source, the second the destination. A remote path is user@host:folder, relative to the home folder of the environment. A trailing / on the source copies the content of the folder, without it rsync copies the folder itself into the destination.
| Option | Effect |
|---|---|
-a | Archive: recursive, keeps links, permissions and times |
-v | Lists the transferred files |
-n | Dry run, shows what would happen |
-z | Compresses data during the transfer |
--exclude | Skips matching files |
--delete | Deletes files in the destination that are not in the source |
See the rsync man page for all options.
Preview with a dry run
Add -n to any command. rsync lists what it would transfer or delete and changes nothing:
rsync -avn ./web/uploads/ {{app-env-id}}@ssh.{{region}}.frbit.app:web/uploads/
Exclude files
rsync -av --exclude '*.jpg' --exclude '/cache' ./ {{app-env-id}}@ssh.{{region}}.frbit.app:
A pattern without a leading / matches anywhere in the path, with / only from the root of the source. Keep a longer list in a file and pass it with --exclude-from=.rsyncignore.
Delete obsolete files
By default rsync never deletes. With --delete, files that are missing in the source are deleted in the destination. Always run it with -n first:
rsync -avn --delete ./web/uploads/ {{app-env-id}}@ssh.{{region}}.frbit.app:web/uploads/
--delete-after deletes only after all new files are in place, which keeps a live site complete during the sync.
SSH options
Pass SSH options with -e, for example a specific key:
rsync -av -e 'ssh -i ~/.ssh/other_key' {{app-env-id}}@ssh.{{region}}.frbit.app:web/uploads/ ./web/uploads/
Further reading
- How rsync works
- Deploying code with rsync, the older and longer blog post