DEV Community

Cover image for How to Take Remote Backups Using rsync Over SSH (Day 12 of 30)
Sheikh Hassaan Bin Nadeem
Sheikh Hassaan Bin Nadeem

Posted on

4 1 1 1 1

How to Take Remote Backups Using rsync Over SSH (Day 12 of 30)

Table of Contents

  1. Introduction
  2. Why Organizations Still Use rsync
  3. Understanding rsync and SSH
  4. Step-by-Step Guide to Remote Backups
  5. Real-World Use Case
  6. Summary

1. Introduction

Backups are essential for any system administrator. While many organizations invest in commercial backup solutions like Veritas NetBackup, Veeam, or Windows NT Backup, there's a simpler, open-source tool that's been trusted for years: rsync.

In this article, we'll explore how to use rsync over SSH to perform remote backups efficiently and securely.


2. Why Organizations Still Use rsync

Despite the availability of commercial tools, many organizations prefer rsync because:

  • Simplicity: Easy to set up and use.
  • Efficiency: Transfers only changed files, saving bandwidth and time.
  • Security: Works seamlessly over SSH for encrypted transfers.
  • Flexibility: Suitable for various backup scenarios, from small directories to entire systems.

3. Understanding rsync and SSH

rsync is a command-line utility that synchronizes files and directories between two locations. When combined with SSH, it allows secure data transfer over networks.

Basic Syntax:

rsync [options] [source] [user@remote_host:destination]
Enter fullscreen mode Exit fullscreen mode

Common Options:

  • -a: Archive mode (preserves permissions, timestamps, symbolic links, etc.)
  • -v: Verbose output
  • -z: Compress data during transfer
  • -e ssh: Use SSH for data transfer

4. Step-by-Step Guide to Remote Backups

Step 1: Ensure rsync is Installed

On most Linux systems, rsync is pre-installed. To check:

rsync --version
Enter fullscreen mode Exit fullscreen mode

If not installed, you can install it using your package manager:

  • For Red hat/CentOS:
  sudo yum install rsync
Enter fullscreen mode Exit fullscreen mode
  • For Debian/Ubuntu:
  sudo apt-get install rsync
Enter fullscreen mode Exit fullscreen mode

Step 2: Perform the Backup

To back up a directory from a remote server to your local machine:

rsync -avz -e ssh user@remote_host:/path/to/remote/directory /path/to/local/destination
Enter fullscreen mode Exit fullscreen mode

Example:

rsync -avz -e ssh user@192.168.1.100:/var/www/html /home/user/backup/html
Enter fullscreen mode Exit fullscreen mode

This command will:

  • Connect to remote_host as user.
  • Synchronize the /var/www/html directory from the remote server to /home/user/backup/html on the local machine.
  • Preserve file permissions, timestamps, and compress data during transfer.

5. Real-World Use Case

Imagine you're managing a web server hosted on AWS, and you want to back up the website files to your local machine daily.

Step 1: Set Up SSH Access

Ensure you can SSH into the remote server:

ssh user@192.168.1.100
Enter fullscreen mode Exit fullscreen mode

Step 2: Create a Backup Script

Create a script named backup.sh:

#!/bin/bash
rsync -avz -e ssh user@192.168.1.100:/var/www/html /home/user/backup/html
Enter fullscreen mode Exit fullscreen mode

Make the script executable:

chmod +x backup.sh
Enter fullscreen mode Exit fullscreen mode

Step 3: Schedule the Backup

Use cron to schedule the backup daily at 2 AM:

0 2 * * * /path/to/backup.sh
Enter fullscreen mode Exit fullscreen mode

This setup ensures that your website files are backed up daily without manual intervention.


6. Summary

While commercial backup solutions offer advanced features, rsync remains a reliable and efficient tool for many organizations. Its simplicity, combined with the security of SSH, makes it an excellent choice for remote backups.

By following the steps outlined above, you can set up automated, secure backups for your systems, ensuring data integrity and peace of mind.

Connect with me on LinkedIn: https://linkedin.com/comm/mynetwork/discovery-see-all?usecase=PEOPLE_FOLLOWS&followMember=sheikhhassaanbinnadeem

Warp.dev image

The best coding agent. Backed by benchmarks.

Warp outperforms every other coding agent on the market, and gives you full control over which model you use. Get started now for free, or upgrade and unlock 2.5x AI credits on Warp's paid plans.

Download Warp

Top comments (0)

Runner H image

Automate Your Workflow in Slack, Gmail, Notion & more

Runner H connects to your favorite tools and handles repetitive tasks for you. Save hours daily. Try it free while it’s in beta.

Try for Free

👋 Kindness is contagious

Discover fresh viewpoints in this insightful post, supported by our vibrant DEV Community. Every developer’s experience matters—add your thoughts and help us grow together.

A simple “thank you” can uplift the author and spark new discussions—leave yours below!

On DEV, knowledge-sharing connects us and drives innovation. Found this useful? A quick note of appreciation makes a real impact.

Okay