How to create server backups with rsync
Backup copies should play an important role in your server planning. Individual backups can be set up and performed quickly using the free synchronization tool or the rsync protocol.
- Automatic backup & easy recovery
- Intuitive scheduling and management
- AI-based threat protection
How to set up rsync backups on Linux servers
To use rsync on Linux operating systems, install the protocol in the package with the same name and create your backups using terminal commands. In the following section, we will use Ubuntu to show you the most important steps for setting up backup processes using rsync. We’ve also included practical examples as well.
Rsync is already installed in Ubuntu by default. If this is not the case, use the following command to install it:
sudo apt-get install rsync
bashIf rsync is installed, you can use terminal commands to specify the source and destination directories and the backup options. The respective source directory and the directory where rsync should store the backup copy must be specified as the source and destination paths. The standard mode (“Archive”) is executed as follows, for example:
rsync -a source directory target directory
bashUse the test run -n
to check the correctness of the specified parameters and directories. Incorrect entries can, in the worst case, lead to data loss. If some files are not copied as they should be, this is often due to a lack of access rights. If this happens, try executing the command as an administrator with sudo
in front of it.
- Unlimited traffic
- Fast SSD NVMe storage
- Free Plesk Web Host Edition
Five examples for using rsync backup
Once you know the fundamental commands, rsync is an outstanding tool for copying files and creating backups. You can either test and apply individual command setups or use proven combinations of the available rsync parameters. We have summarized five popular ways to carry out rsync backups in the following sections.
Standard backup with archive mode
The archive mode copies all files from the source directory to the target directory including all subdirectories. All authorizations, time stamps and device data are retained. This is the ideal and simplest solution in many cases as it combines various options in a single parameter. If you combine the mode with the -v
parameter, you will also receive comprehensive status information during the backup process.
rsync -av Source directory Target directory
bashThe respective source directory and the directory in which rsync should store the backup copy must be specified as the source and target path.
Identical copy of the source directory
Not only can rsync transfer files from A to B, it can also make identical copies of folders or entire directories. After standard archival, the files that were in the target folder before the rsync backup took place (but are not in the source folder) are deleted.
rsync -av --delete Source directory Target directory
bashrsync backup excluding files of a specific format
If you want to run an rsync backup excluding files in a specific format, you can use the --exclude
command to do so. The parameter allows you to define an individual character pattern that rsync uses as an indicator to ignore a file. The following sample code excludes .txt files.
rsync -av --exclude'*.txt' Source directory Target directory
bashBacking up files with a minimum or maximum size
If, instead of a specific character pattern, you want the file size to influence the exclusion of specific files in an rsync backup, you can use the --max-size
and --min-size
parameters. If you use the following command, only files ranging from at least 10 MB to at most 100 MB are copied:
rsync -av --min-size=10MB --max-size=100MB Source directory Target directory
bashBackup including character format conversion
You may have to convert files into a different character format in the target directory. If you want to transfer data from a Mac to a Linux server, for example, you’ll want to include character format conversion. Apple devices use UTF8-MAC by default, which is not available on Linux systems and would cause problems with special characters. The --iconv
option lets you easily adapt the character encoding as part of the rsync backup process (in the example below, from UTF8-MAC to UTF8):
rsync -av --iconv=UTF8-MAC,UTF8 Source directory Target directory
bashOverview of the most important rsync backup options
You can define the individual settings for your rsync backups using the different options, which can be abbreviated by letter or written out in full. The following table summarizes the most important parameters, which can be combined with each other as required:
Option | Function |
---|---|
-r , --recursive
|
rsync backup takes all subdirectories into account |
-u , --update
|
Instruction to skip files in the target directory that are newer than those in the source directory |
-c , --checksum
|
Distinguish source and target files based on checksums |
-l , --links
|
Symbolic links are copied as such (and not as files) |
-p , --perms
|
File permissions are retained |
-g , --group
|
Group file permissions are retained |
-t , --times
|
File time stamps (last change) are retained |
-o , --owner
|
File owners are retained (only if administrators) |
-D , --devices
|
Device data is retained |
-z , --compress
|
Automatic compression of the transferred files |
--compress-level=NUM
|
Determines the compression level; values (“NUM”) between 0 (no compression) and 9 (maximum compression) are possible |
-v , --verbose
|
More comprehensive details during the backup processes |
-q , --quiet
|
Hide all details on the backup process (except error messages) |
-a , --archive
|
Archive mode used as standard mode and identical to the option combination — rlptgoD |
-n , --dry-run
|
Test run in which no actual changes are made |
-h , --help
|
Auxiliary menu (can only be used without indicating source and target directories or other arguments) |
--bwlimit=KBPS
|
Restrict bandwidth (kilobytes per seconds); e.g. --bwlimit=30 (limit of 30 kbit/s)
|
--exclude=SAMPLE
|
Exclude a pattern from synchronization; e.g. --exclude sample folder (the “sample folder” folder is not synchronized.)
|
--delete
|
Delete all files that are in the target directory but not in the source directory |
--progress
|
Show the duration of the rsync backups and the transfer speed |
--list-only
|
List files instead of a backup |
--stats
|
Comprehensive report on the transferred data (number, size) |
--max-size=SIZE
|
Define a maximum file size; e.g. --max-size=10MB (only files with a size up to 10 MB are transferred.)
|
--ignore-errors
|
Prevent cancelation of the backup process in the event of an error |