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!
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:
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 asroot
to be able to preserve theuid
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
- 17 useful rsync (remote sync) Command Examples in Linux
https://www.linuxtechi.com/rsync-command-examples-linux/ - Sysadmin tools: Using rsync to manage backup, restore, and file synchronization
https://www.redhat.com/sysadmin/rsync-synchronize-backup-restore