VOOZH about

URL: https://www.digitalocean.com/community/tutorials/how-to-use-rsync-to-sync-local-and-remote-directories

⇱ How To Use Rsync to Sync Local and Remote Directories | DigitalOcean


πŸ‘ How To Use Rsync to Sync Local and Remote Directories

Introduction

Rsync, which stands for remote sync, is a command-line tool for synchronizing files and directories locally and remotely. It uses a delta-transfer algorithm to minimize the amount of data copied by moving only the portions of files that have changed, making it highly efficient for tasks like backups and mirroring.

In this guide, you will learn how to use rsync for various file management tasks. We will cover its basic syntax, how to transfer files to and from a remote server, and how to use common options like --delete for mirroring and --dry-run for safe testing. You will also learn how to automate transfers with cron and troubleshoot frequent issues.

Deploy your frontend applications from GitHub using DigitalOcean App Platform. Let DigitalOcean focus on scaling your app.

Key Takeaways:

  • rsync is a file synchronization tool that efficiently copies local and remote directories by transferring only the changed portions of files.
  • The trailing slash (/) on the source path is critical, as it dictates whether rsync copies the contents of the directory or the directory itself.
  • Always test rsync commands with the --dry-run or -n flag to preview the outcome without making any actual changes to the files.
  • The -a (archive) flag is recommended for most use cases because it syncs recursively while preserving permissions, ownership, and modification times.
  • To create a true mirror, use the --delete option to remove files from the destination that are no longer present in the source directory.
  • For remote transfers, the -z flag compresses file data to reduce network usage, and the -P flag shows progress while allowing interrupted transfers to be resumed.
  • rsync uses SSH for secure remote operations, enabling you to β€œpush” files to a remote system or β€œpull” files from it.
  • You can automate synchronization tasks with cron, which requires using absolute paths in your command and configuring passwordless SSH key authentication.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the author(s)

πŸ‘ Jeanelle Horcasitas
Jeanelle Horcasitas
Author
Technical Writer
See author profile

Educator and writer committed to empowering our community by providing access to the knowledge and tools for making creative ideas into a reality

Former Senior Technical Writer at DigitalOcean, specializing in DevOps topics across multiple Linux distributions, including Ubuntu 18.04, 20.04, 22.04, as well as Debian 10 and 11.

πŸ‘ Manikandan Kurup
Manikandan Kurup
Editor
Senior Technical Content Engineer I
See author profile

With over 6 years of experience in tech publishing, Mani has edited and published more than 75 books covering a wide range of data science topics. Known for his strong attention to detail and technical knowledge, Mani specializes in creating clear, concise, and easy-to-understand content tailored for developers.

Still looking for an answer?

Was this helpful?

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Is it possible to backup multiple folders are once over SSH? Locally, this command works fine:

rsync -av /{folder1, folder2} /backup

But when run over ssh, it fails. Any ideas? The paths are correct and work when run individually. I am on OS X if that matters.

@TheUnknown: β€œBut when run over ssh” - what command do you try to run exactly?

Both of these fail:

rsync -av root@server.com:/{folder1, folder2} /backup rsync -av -e ssh root@server.com:/{folder1, folder2} /backup

Realized I put spaces in my examples.

This works: rsync -av /{folder1,folder2} /backup

These fail: rsync -av root@server.com:/{folder1,folder2} /backup rsync -av -e ssh root@server.com:/{folder1,folder2} /backup

I am still not able to transfer multiple folders over SSH, I just made a typo in my example. Sorry if I was not clear.

Is this a permanent connection so if file 1 changes on server 1 then its sent to directory2 on server 2? Or is it temporary and must be done manually?

Is it similar to lncron? Is one better to have change automatically updated on remote host?

Thanks.

@phillip: rsync only runs when you run the command. It doesn’t automatically sync both servers, take a look at <a href=β€œhttps://code.google.com/p/lsyncd/”>lsyncd</a>.

Most useful post Kamal. Thanks a lot !!

My Apologies Justin. I meant you not Kamal. Thanks for this most informative post.

πŸ‘ Creative Commons
This work is licensed under a Creative Commons Attribution-NonCommercial- ShareAlike 4.0 International License.
  • Deploy on DigitalOcean

    Click below to sign up for DigitalOcean's virtual machines, Databases, and AIML products.

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and AI-native businesses

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow β€” whether you're running one virtual machine or ten thousand.

Start building today

From GPU-powered inference and Kubernetes to managed databases and storage, get everything you need to build, scale, and deploy intelligent applications.

Β© 2026 DigitalOcean, LLC.Sitemap.
Dark mode is coming soon.