Using rsync to backup Linux servers
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!
- 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:
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
rootto be able to preserve the
- Command arguments
-zcompress file data during the transfer
-aarchive mode; equals -rlptgoD
(the most important arg. That one will preserve all files attributes)
-vincrease verbosity (list all files been transferred)
-houtput numbers in a human-readable format
--deletedelete extraneous files from destination dirs
(if a file was deleted on the source, so will also delete on destination)
- 17 useful rsync (remote sync) Command Examples in Linux
- Sysadmin tools: Using rsync to manage backup, restore, and file synchronization