Do not risk losing data. Back up your valuable data from the Linux command line. We are going to use the rsync command for this, and we have even found some optional graphical interfaces for it.
There are many ways to back up your files. We wanted to show you a robust, flexible and reliable way to protect your data. We choose rsync for its respected algorithms that calculate the differences between the files in the source directory and the destination directory. Only the differences between two versions of a file are transferred, not the whole file if that can be avoided.
When this efficiency is combined with its strong track record in making file copies and directory synchronizations since the mid-1990s, rsync is the perfect candidate to create backups from the Linux command line.
In addition, there are independent software programs that act as front-end for rsync. They provide graphical user interfaces (GUIs) to rsync that some people may find easier to use.
The simpler and faster it is to perform a backup, the more likely it is to do so.
Using rsync with an external hard drive in Linux
To back up your data to an external hard drive, the hard drive must be mounted and accessible to you. If you can write it, then you can also do it rsync. In this example, an external USB hard drive called SILVERXHD (for "Silver eXternal Hard Drive") is connected to the Linux computer. It has been self-mounted by the operating system.
You will need to know the path to the unit. In GNOME, open the Nautilus file browser and locate the unit name in the sidebar.
Hover your mouse over the name of the external drive and a tooltip will show you the path to the Linux drive.
In this example, the tooltip informs us that the mount point for the file system on the external drive is "/ media / dave / SILVERXHD".
If the file browser does not, search for the external drive and open a terminal window in that location. Use the pwd command to print the path to the terminal window.
Copy the contents of the Linux source directory
To use rsync to copy the contents of a directory to your backup destination, use the following command.
The -r (recursive) option causes rsync to copy all nested subdirectories and their contents. Note that there is a slash «/» at the end of the word «SILVERXHD», but it has been rounded to the next line in the screenshot.
rsync -r / home / dave / Documents / / media / dave / SILVERXHD /
The file is copied and returned to the command line.
If we look at the external USB drive, we will see that the directories found in the Documents directory have been copied to the root of the external drive.
Copy the source directory and its contents
If you wanted the Documents directory and its contents to be copied to the external drive, delete the "/" from the end of "/ home / dave / Documents" on the command line, as follows:
rsync -r / home / dave / Documents / media / dave / SILVERXHD /
To avoid confusion, I deleted the two directories previously copied from the external drive before this second command was executed.
If we let the second copy complete and take another look at the external drive, we will see that the Documents directory has been copied. Its content is within that directory. They are not at the root of the external drive.
Copy to a specific destination directory
To copy to a specific directory on the destination hard disk, add the directory name to the destination path. Suppose we want to copy the contents of the "/ home / dave / Documents" directory to a directory called "backups" on the external drive.
We would do this with the following command.
rsync -r / home / dave / Documents / / media / dave / SILVERXHD / backups /
Checking on the external disk we can see that the backup directory has been created, and within that directory are the contents of the directory «/ home / dave / Documents».
Preserve ownership and permissions of files in Linux
Use the -a (file) option to preserve file attributes, such as modification dates, file ownership, access permissions, etc., for copied files, symbolic links, and special lock files .
rsync -ra / home / dave / Documents / / media / dave / SILVERXHD / backups /
Use of Verbose mode
The -v (verbose) option forces rsync to list the files as they are copied to Linux.
rsync -rav / home / dave / Documents / / media / dave / SILVERXHD / backups /
A summary of the backup is presented when the copy is completed.
Sent: The bytes transferred to the target.
Received: The bytes received on the host.
Bytes / second: is the effective transfer rate.
Total size: Represents the size of the data that would have been sent if rsync had not been used. In subsequent runs of rsync, only file differences will be transferred. This figure will represent the data that did not have to be transferred.
Accelerate: This is the relationship between the amount of data that had to be sent and the total amount of data there is. If rsync needs to copy all the files in its entirety (the first time it is run, for example) the speed will be 1.0. The next time rsync is used, transfers will be optimized.
It will only send the differences between the files, not the complete files. Unchanged fields will be ignored. The acceleration figure will represent the relationship between the small amount of data that was necessary to transfer and the total size of the files.
Use of Progress Option
The -P (progress) option causes rsync to generate a small progress report after copying each file.
rsync -raP / home / dave / Documents / / media / dave / SILVERXHD / backups /
The information provided can be viewed between each file copied from Linux.
The information provided is:
Byte size: Data transferred for this file.
Percentage: Percentage of the transferred file.
B / s: Data transfer rate.
Remaining time: Estimated time to transfer this file.
xfr #: The number of files transferred so far.
To suck: The number of files that remain to be checked and verified by the optimization algorithms.
Adding more speed
To speed up transfers, use the -z (compression) option. This compresses the file in the transfer, but the file is stored uncompressed in the destination directory.
The compression option will not produce significant benefits for transfers that involve many small files. For larger file collections, you can reduce the transfer time in a significant way.
We are also using the –partial option. rsync will delete partially transferred files caused by network failures or other interruptions in the Linux system. The –partial option forces rsync to leave the files partially transferred to the destination. The net time rsync running will not have to re-transfer the parts of the partially transferred files.
Note that you may not want to use this option if there is a risk that someone will confuse partially transferred files with fully transferred files.
rsync -ravz –partial / home / dave / Documents / / media / dave / SILVERXHD / backups /
In our example, the benefits are marginal.
The acceleration rate has improved, but by two hundredths of a percentage point. In a real world scenario, your speed improvements will be more impressive.
Using rsync in a network
So far we have been pointing to an external USB drive. To use a network location as the backup destination, use the path to that location on the command line. There is a storage device connected to the network (NAS) on the network on which this article was investigated.
We can use the same trick that we used before to identify the route to the NAS, hovering over the connection to that device in Nautilus.
There are no special options for backing up a network; All of them are options that we have already used.
rsync -ravz –partial / home / dave / Documents / / media / dave / NAS / dave / backups /
There is no difference in the output format.
It is not surprising that there is a significant improvement in the number of bytes / second.
If we run rsync once again, we can see that there are no files to transfer because there have been no changes, but there are still some bytes transferred from one side to another. This is the amount of data that must be transferred to compare the list of files at the destination with the list of files at the source.
The acceleration ratio is an order of magnitude better in this case. In practice, their performance coefficients will be between our two pseudo-artificial readings.
Use of rsync over SSH
rsync supports backing up through an SSH connection. We need to provide the name of the user account and the SSH location on the command line. We are using a network name, but you can also use an IP address.
Observe the ":" between the details of the SSH connection and the start of the network path on the remote target.
rsync -ravz –partial / home / dave / Documents / email@example.com: / home / dave / Backups /
You will be asked for the password of the user account on the remote machine. This is not your password on the source machine.
The backup will be completed as usual. Performance is not as fast as a normal network connection, due to the encryption and decryption that takes place in the secure shell connection.
We can easily create automated backups by adding entries to your crontab file.
We will configure an automated backup to run every day at 04:30 (if the computer is on at that time, of course). The syntax of the rsync command does not change at all.
Ctrl + O will write the changes to the file and Ctrl + X will close the nano editor.
Putting a friendly face on Rsync
People who feel less comfortable with the command line can use one of the many programs that put a graphical user interface (GUI) in rsync. Two good examples are luckyBackup and Grsync. Both programs allow you to select many of the rsync options through the user interface.
The Grsync program focuses on being a visual wrapper for rync. It provides easy access to rsync options and adds only a limited set of new features.
The luckyBackup program is much more than just a wrapper for rsync. It is a backup program that uses rsync behind the scenes. For example, luckyBackup can make multiple "snapshots" of your backup. You can then "go back" to the versions of the files in any of the snapshots.
To install Grsync
To install Grsync on Ubuntu, use this command:
sudo apt-get install grsync
To install Grsync on Fedora, use this command:
sudo dnf install grsync
To install Grsync in Manaro use this command:
sudo pacman -Syu grsync
To install luckyBackup
To install luckyBackup on Ubuntu, use the following command:
sudo apt-get install luckybackup
To install luckyBackup on Fedora use the following command:
sudo dnf install luckybackup
In Manjaro you must install luckyBackup from the Arch Users Repository (AUR). You can do it with the pamac package manager.
Don't take risks, back up your data frequently
Backups are absolutely vital. Make backup copies frequently, make backup copies in many locations and on different media. Once configured, rsync He can do all that for you.