How do I perform a Backup? Can I perform a remote backup? How can I keep it synchronized by transferring only the changes since the last backup?
In this post we are going to perform a backup and learn how to keep it fresh.
It is important to clarify that this method can be used to perform a “system backup”, but it is not the target of this post.
The objective is to perform a backup of a concrete path and keep it synchronized by copying only the modifications since the previous backup. That is going to save time and resources as only the changes will be transferred.
Rsync
To perform the backup we will be using “rsync” tool. It allows us to be efficient, specially in the backup maintenance.
It allows as well to perform easy remote backups using SSH.
This post assumes that rsync is installed in the local machine and in the remote machine in case of remote backup.
There is a section at the end of the post named “Rsync options”, which explains the meaning of each flag in the examples, check it for a detailed explanation of every option used.
Backup Samples
We will review the most common ways of rsync backups through a few examples:
- Rsync local backup
- Remote backup with key access
- Remote backup with/without password
You can easily adapt these examples to your needs.
If you have any doubt or a different need, you can use the comments below.
A special case might be when you need to move instead of copying the files. That case is not discussed here, you can read further about it in this post Rsync copy and source deletion.
Synchronization
All these examples synchronize the source with the backup, which means that if any element is deleted in the source, it will be deleted as well in the backup next time it synchronizes.
The way you perform the initial backup and the way you synchronize it is exactly the same, you just execute it each time you want to sync again.
1. Rsync local backup
In this example, we want to backup “/my/important/location/” in “/mnt/backupUnit/MyImpLocBackup”.
Those locations can be local or a mount point (for instance, smbfs/cifs, nfs, sshfs …).
rsync -vrlptog \ --stats \ --delete-before \ --log-file=/mnt/backupUnit\MyImpLoc_Backup.log \ --exclude lost+found \ /my/important/location/ \ /mnt/backupUnit/MyImpLocBackup
You have to adapt this sample to your needs, change source path, backup path, and log path and name.
2. Remote backup with key access
In this sample we want to backup “/MyFiles” in a remote server by accessing with a key file.
The local backup path is “/mnt/backupUnit/MyFilesBackup/”.
rsync -vrlptog \ --stats \ --delete-before -e "ssh -i /home/myuser/keyfile.pem" \ --logfile=/mnt/backupUnit/transfer.log \ ubuntu@dns.compute.amazonaws.com:/MyFiles/ \ /mnt/backupUnit/MyFilesBackup/
As you can see in the code, in this example we are using an Amazon virtual machine. Don’t forget to adapt the code by using your source path, local backup path, vps or server address, keyfile and log path and name.
3. Remote backup with/without password
This time, we want to backup “/MyFiles” from our server using username/password access.
password access
This way is a common way for “manual backups”.
rsync -vrlptog \ --stats \ --delete-before \ --logfile=/mnt/backupUnit/transfer.log \ usuario@myserver.example.com:/MyFiles/ \ /mnt/backupUnit/MyFilesBackup/
Once you execute this rsync command, you need to provide the password.
passwordless access
If you want automatic backups, you need to workaround the password phase.
You can achieve that by configuring passwordless access to the server from the backup client.
Once you have configured passwordless access, you can use the exact same rsync command as seen in the previous example, only this time password will not be required.
rsync -vrlptog \ --stats \ --delete-before \ --logfile=/mnt/backupUnit/transfer.log \ usuario@myserver.example.com:/MyFiles/ \ /mnt/backupUnit/MyFilesBackup/
At the end, we will have our backup done/synced and the log in “/mnt/backupUnit/transfer.log”
Rsync options
In this section, we are going to review the rsync flags used in the examples, so you can check and modify as you need.
-vrlptog
v: “verbose” Increases rsync verbosity level.
r: “recursive” Include sub directories.
l: “links” Copy symlinks as symlinks.
p: “perms” Preserve permissions.
t: “times” Preserve modification times.
o: “owner” Keep owner.
g: “group” Keep group.
–stats
It prints a report with quite useful data at the end.
–delete-before
Deletes those files that do not exist in the source before it starts copying the new ones. It is useful to save some space in the backup unit.
–log-file=/path/MyImpLoc_Backup.log
Specifies path and name, if you want to create a log.
–exclude lost+found
You can specify if you want to exclude some elements from being backed up or synced.
For more information, you can check manual page of rsync:
man rsync
Or check the output of:
rsync --help