Introduction
Backing up data is an essential part of both individual and enterprise infrastructures. Machines with the Linux operating system can use rsync and ssh to facilitate the process.
Rsync is a command-line utility that lets you transfer files to local and remote locations. Rsync is convenient to use since it comes by default with most Linux distributions. You can customize the tool by using many of the available options.
In this use case, we will use SSH in combination with rsync to secure the file transfer.
Follow this tutorial to learn how to use rsync to backup data. The guide will list a few examples to illustrate how the process works.
Prerequisites
- Sudo or root privileges or a user with access to backup and destination directories
- SSH access to a server via command line/terminal window
- Rsync installed on the local and destination machine
Basic Rsync Syntax for Local and External Transfers
The syntax for using the rsync tool is different for local and remote transfers.
For local backups, the syntax follows this basic pattern:
rsync
options
SOURCE DESTINATION
To transfer files to an external location, we will use a pattern that is a bit different:
rsync
options
SOURCE [email protected]_or_hostname:DESTINATION
In both cases, the source and destination are a directory or a file path.
Backing up Data with Rsync
For a better understanding of what rsync does, we will use the verbose switch -v
. Additionally, since we will be backing up data in directories, we will use the archive mode -a
for recursive syncing. There are many rsync options and examples, so use any of them you need for your use case.
Rsync Dry Run – Precautions
The rsync utility allows you to manipulate your data in different ways. So, be careful when backing up your files. If you use a wrong option or a wrong destination, you may end up mixing your data. Even worse, you may unintentionally overwrite or delete files.
For that reason, use the --dry-run
option to confirm the tool does what you want to do. Accidental data loss can occur, but this option helps prevent it.
For simple transfers, you may not need to use --dry-run
, but when a larger set of data is in question, we strongly advise that you do.
Use the basic syntax format and add --dry-run
:
rsync
options
--dry-run SOURCE DESTINATION
Use Rsync to Back up Data Locally
We will start by performing a backup of a directory on the same Linux machine. The path can be any location – another partition, hard drive, external storage, etc.
Use the full path for both the source and destination to avoid errors.
For example, to back up a Dir1 from Documents to /media/hdd2/rscync_backup, use the rsync command in this form:
rsync -av /home/test/Documents/Dir1 /media/hdd2/rsync_backup
The output shows the list of transferred files and directories and other transfer details.
Note: To create a new directory at the destination and back up your files there, add a trailing slash (/
) at the end of the destination path. If you add the trailing slash to the source, then the source directory will not be created at the destination. Rsync only transfers its content in that case.
Use Rsync to Back up Data over Network
For secure data backup over the network, rsync uses SSH for transfers. Your server needs to be set to allow SSH connection.
Once you manage to connect to the remote machine over SSH, you can start backing up your data to a location on that machine.
For example, to back up Dir1 to backup on another machine over the network, enter:
rsync -av /home/test/Documents/Dir1 [email protected]:/home/test/backup
The output lists the directories and files rsync transferred to another machine.
You can check if the files really are on the remote server:
If you are connecting for the first time, you will need to enter your password and confirm when you get a prompt. There is no need to enter a username for remote transfers if you want to connect as the current user.
Note: You can evade entering a password every time you want to back up data with rsync over SSH. Set up SSH key-based authentication, and you will be able to use passwordless login to the remote machine.
The example we used here assumes that SSH uses the default port. If you need to specify a different port for the SSH connection, use the -e
flag and enter SSH options.
To specify port 4455, for example, run the above command in this format:
rsync -av -e 'ssh -p 4455' /home/test/Documents/Dir1 [email protected]:/home/test/backup
When needed, you can delete the source files after you transfer them to another location.
Compress Data when Backing up with Rsync
To save some space, you can compress your data before transferring it to another location. You can use rsync’s built-in option to compress data, or you can use a different tool to do that before running rsync.
To compress data during transfer, use the -z
switch with your rsync
command.
rsync -avz /home/test/Documents/Dir1 [email protected]:/home/test/backup
Another option is to use the zip
command to zip your files or directory and then run rsync
. In our case, we will zip Dir1 into Dir.zip:
zip /home/test/Documents/Dir1.zip /home/test/Documents/Dir1
Then transfer that file to another location:
rsync -avz /home/test/Documents/Dir1.zip [email protected]:/home/test/backup
Now, you have a zipped copy of your directory on a remote server. You can also do this for local transfers if you want to have a backup on another drive or partition.
Conclusion
This tutorial showed you how to back up data using rsync both locally and over a network. Take caution when using this tool and make sure you do a dry run if you are unsure about the rsync options you want to use.
You can check our other guides for more rsync examples or you can learn how to exclude files and directories with rsync.
原创文章,作者:6024010,如若转载,请注明出处:https://blog.ytso.com/226015.html