Using rsync to backup Linux servers

Using rsync to backup Linux servers

linux Jul 12, 2021

Imagine that you have some servers running applications and you need to find a way to backup the files and preserve their properties, like the user, group, date, and permissions. How do you do that?

Does exist many ways to create a backup solution, but here I'll focus on a backup solution to:

  • Copy all files from one Linux server to another Linux.
  • Preserve all files properties, including date change and file permissions.
  • Allow a file restore simply by rsync the content back to the original server.
This is not a solution for any kind of backup routine!

Requirements

  • rsync
  • ssh authorized keys configured

You will need to install the rsync package. For example:

# Ubuntu/Debian
apt update
apt install -y rsync

# CentOS/Fedora
dnf install -y rsync

You also need to share the origin server public as an authorized key on the destiny server, so will not be required to enter the password on any rsync command execution. You can check how to do that on this post:

Setup SSH login to don’t ask password again
If you use the SSH a lot of time or want to sync files, probably you need to type your password many times a day. The way to avoid this problem and create a free communication between two Linux computers is to share the public ssh key. If you want

The rsync archive command

The command that you'll need is this:

# rsync that preservers all files attributes
rsync -zavh --delete [email protected]:/mnt/storage /mnt/backup

Now, explaining the command:

  • The user:
    You will need to run the command as root to be able to preserve the uid attribute.
  • Command arguments
    • -z compress file data during the transfer
    • -a archive mode; equals -rlptgoD
      (the most important arg. That one will preserve all files attributes)
    • -v increase verbosity (list all files been transferred)
    • -h output numbers in a human-readable format
    • --delete delete extraneous files from destination dirs
      (if a file was deleted on the source, so will also delete on destination)

Reference

Tags

Luiz Felipe F M Costa

I am a quality engineer at Red Hat / Ansible. I love automation tools, games, and coffee. I am also an active contributor to open-source projects on GitHub.