How to Use rsync for Secure Linux Backups

If you use Linux, you’ve probably heard about backups.

Backups are just copies of your files. If your computer crashes, gets hacked, or you delete something by mistake, a backup can save you.

In this post, I’ll show you how to use a tool called rsync to make backups. I use it on my own server, and I’ll explain how you can use it too.


What Is rsync?

Let’s start with the basics.

rsync stands for remote sync.

It’s a tool that copies files and folders from one place to another. It works both on your computer and over the internet.

You can use rsync to:

  • Copy files from one folder to another.
  • Sync folders between two computers.
  • Create backups quickly.

Here’s what I like most about rsync:

  • It only copies the changes, not the whole file.
  • It keeps permissions and timestamps.
  • It works over SSH, so it’s secure.

Have you ever used a cloud backup tool like Google Drive or Dropbox? rsync is kind of like that, but it gives you more control and doesn’t rely on any company.


Why Use rsync for Backups?

Good question. There are many backup tools, so why pick rsync?

Here are a few reasons I use it:

  • It’s already included in most Linux systems.
  • You can run it with one command.
  • You can automate it with cron.
  • It works great on slow connections.
  • You can back up over SSH (encrypted).

It’s also fast. If you have 100,000 files but only 10 have changed, rsync will only copy those 10.

You don’t need fancy software. You don’t need a GUI. Just the terminal.


How to Check if rsync is Installed

Open your terminal and type:

rsync --version

If it shows the version, you’re ready. If not, you can install it.

On Ubuntu or Debian:

sudo apt install rsync

On CentOS or RHEL:

sudo yum install rsync

Done? Great. Now let’s make some backups.


Basic rsync Backup to a Local Folder

Let’s say I want to back up my /home/user/documents folder to /backup/docs.

Here’s the command:

rsync -av /home/user/documents/ /backup/docs/

Let me explain:

  • -a means archive mode. It keeps permissions, links, etc.
  • -v means verbose. It shows you what’s happening.
  • The slash (/) at the end of the source folder is important. It means “copy the contents, not the folder itself.”

Try it out. You’ll see a list of copied files.


Backing Up to a Remote Server (Over SSH)

Now let’s say you want to back up to another server.

That’s what I do. I have a home server and a VPS. I use rsync to copy my important files every day.

Here’s how I do it:

rsync -av -e ssh /home/user/documents/ user@remote-server:/backup/docs/

Explanation:

  • -e ssh tells rsync to use SSH for the connection.
  • user@remote-server is your login on the other machine.
  • You’ll be asked for your SSH password (unless you set up keys).

The files go through an encrypted tunnel, so no one can see them.

This is one of the main reasons I trust rsync.


Using rsync with SSH Keys

Typing your password every time can be annoying.

You can set up SSH keys to connect without a password. This also makes your backup script safer and easier.

To create a key:

ssh-keygen

Just press Enter for each prompt. Then copy the key to the other server:

ssh-copy-id user@remote-server

Now try the rsync command again. It should work without asking for a password.

If it does, you can use cron to schedule it.


Scheduling Automatic Backups with Cron

I like to run backups every night.

Here’s how to set that up:

  1. Open your crontab:
crontab -e
  1. Add a line like this:
0 2 * * * rsync -av -e ssh /home/user/documents/ user@remote-server:/backup/docs/

This means: run at 2:00 AM every day.

Your server will quietly back up while you sleep.


Helpful rsync Options

Here are some extra options I use:

  • --delete: Deletes files on the backup that were deleted in the source.
  • --exclude: Skips certain files or folders.
  • --progress: Shows file transfer progress.

Example:

rsync -av --delete --exclude "*.tmp" /home/user/ user@remote:/backup/

This will not copy any .tmp files and will remove files that no longer exist on the source.


Two Quick Lists

What I Back Up with rsync

  • Website files (like WordPress or HTML)
  • Config files in /etc/
  • Databases (after dumping with mysqldump)
  • Home directories

What I Like About rsync

  • Simple command line tool
  • Works on most Linux systems
  • Secure over SSH
  • Fast for large directories
  • Can exclude or include anything I want

Common Problems (and Fixes)

Problem: Permission denied

Solution: Make sure you have read access on the source and write access on the destination. Also check SSH permissions.

Problem: rsync keeps copying the same files

Solution: Make sure the file times and permissions are not changing on the source. Use -c if needed (it checks file contents, but it’s slower).


Comparing rsync to Other Backup Tools

There are other tools like:

  • scp – simpler, but no syncing
  • tar – good for full backups, but not incremental
  • rsnapshot – built on top of rsync, good for versioned backups

I’ve tried all three. I keep coming back to rsync because I like having control and understanding what’s happening.


Final Thoughts

I’ve used rsync for years. It’s helped me recover from accidents and broken servers more than once.

It’s not flashy. It’s not pretty. But it works.

Do you have backups set up yet? If not, rsync is a great place to start.

Leave a Reply