Do not risk data loss. Back up your valuable data from the Linux command line. We will use the rsync command for this, and we have even found some nice optional graphical interfaces.
There are many ways to make a backup copy of your files. We wanted to show you a robust, flexible and reliable way to protect your data. We chose rsync because of its well respected algorithms which calculate the differences between the files in the source directory and the target directory. Only differences between two versions of a file are transferred, not the entire file if it can be avoided.
When this efficiency is combined with its solid experience in file copy and directory synchronization since the mid-1990s, rsync is an ideal candidate for creating backups from the Linux command line.
In addition, there is independent software that acts as a front-end server for rsync. They provide rsync with graphical user interfaces (GUIs) that some people may find easier to use.
The faster and easier a backup is, the more likely you are to do it.
Using rsync with an external hard drive
To back up your data to an external hard drive, it must be mounted and accessible. If you can write to it, rsync can too. In this example, an external USB hard drive called SILVERXHD (for "Silver eXternal Hard Drive") is plugged into the Linux computer. It was automatically mounted by the operating system.
You will need to know the path to the drive. Under GNOME, open the Nautilus file browser and locate the drive name in the sidebar.
Hover over the name of the external drive and a tooltip will show you the path to the drive.
In this example, the tooltip informs us that the mount point of the file system on the external drive is "/ media / dave / SILVERXHD".
If your file browser does not, go to the external drive and open a terminal window there. Use the pwd command to print the path to the terminal window.
Copy the contents of the 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 is returned to the next line of the screen capture.
rsync -r / home / dave / Documents / / media / dave / SILVERXHD /
The copy of the file takes place and you return to the command line prompt.
If we look at the external USB drive, we see that the directories in the Documents directory have been copied to the root of the external drive.
Copy the source directory and its contents
If you want the Documents directory and its contents to be copied to the external drive, delete the "/" at the end of "/ home / dave / Documents" in the command line, as follows:
rsync -r / home / dave / Documents / media / dave / SILVERXHD /
To avoid confusion, I deleted the two previously copied directories from the external drive before the execution of this second command.
If we let the second copy finish and re-examine the external drive, we will see that the Documents directory has been copied. Its contents are in this directory. They are not at the root of the external drive.
Copy to a specific target directory
To copy to a specific directory on the target hard disk, add the directory name to the target 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 that with the following command.
rsync -r / home / dave / Documents / / media / dave / SILVERXHD / backups /
By checking on the external drive, we can see that the backup directory has been created and that it contains the contents of the directory "/ home / dave / Documents".
Preserving ownership of files and permissions
Use the -a (archive) option to preserve file attributes such as modification dates, file ownership, access permissions, and so on, for copied files, symbolic links, and files. special blocking files.
rsync -ra / home / dave / Documents / / media / dave / SILVERXHD / backups /
Use verbose mode
The -v (verbose) option causes rsync to list the files as they are copied.
rsync -rav / home / dave / Documents / / media / dave / SILVERXHD / backups /
A summary of the backup is presented at the end of the copy.
Sent: bytes transferred to the target.
Received: bytes received on the host.
Bytes / sec: is the effective transfer rate.
Total Size: Represents the size of the data that would have been sent if you did not use rsync. When performing rsync later, only the file differences will be transferred. This figure will represent the data that should not have been transferred.
Acceleration: This is the ratio of the amount of data to be sent to the total amount of data. If rsync needs to copy all the files in their entirety (the first time it is run, for example), the speed will be 1.0. When rsync is next used, transfers will be optimized. It will only send the differences between the files, not the whole files. Unmodified fields will be ignored. The acceleration digit will represent the ratio of the small amount of data to be transferred to the total file size.
Using the progression option
The -P (progress) option allows 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 seen between each copied file.
The information provided is:
Size in bytes: data transferred for this file.
Percentage: The percentage of the transferred file.
B / s: data transfer rate.
Remaining Time: Estimated time to transfer this file.
x #: number of files transferred to now.
to-chk: The number of files remaining 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 being transferred, but the file is stored uncompressed in the target directory.
The compression option will not bring significant benefits for transfers involving many small files. For larger file collections, the transfer time can be significantly reduced.
We also use the –partial option here. rsync will remove partially transferred files caused by network or other interrupts. The –partial option forces rsync to leave files partially transferred to the target. The net time rsyncs run, it will not be necessary to transfer the parts of the partially transferred files again.
Note that you may not want to use this option as it may 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 two hundredths of a percent! In a real scenario, your speed improvements will be more impressive.
Using rsync on a network
Until now, we were targeting an external USB drive. To use a network location as the target of the backup, use the path to this location on the command line. There is a storage device connected to the network (NAS) on the network on which this article was studied.
We can use the same trick we used previously to identify the NAS access path, by hovering over the connection to this device in Nautilus.
There are no special options for backup over a network; These are all the options we have already used.
rsync -ravz –partial / home / dave / Documents / / media / dave / NAS / dave / backups /
There is no difference in the format of the output.
Not surprisingly, there is a significant improvement in the Bytes / sec figure.
If we run rsync again, we can see that there is no file to transfer because there has been no change, but there are still a few bytes transferred in both directions. This is the amount of data to be transferred to compare the list of files in the target with the source file list.
The rate of acceleration is an order of magnitude higher in this case. In practice, your performance ratios will lie somewhere between our two pseudo-artificial readings.
Using rsync on SSH
rsync supports backup over an SSH connection. We must provide the name of the user account and the SSH location on the command line. We use a network name here, but you can also use an IP address.
Note the ":" between the details of the SSH connection and the beginning of the network path on the remote target.
rsync -ravz –partial / home / dave / Documents / email@example.com: / home / dave / Backups /
You will be prompted for the password of the user account on the remote machine. This is not your password on the source machine.
The backup will end as usual. The throughput is not as fast as a typical network connection, because of the encryption and decryption that takes place in the secure shell connection.
Automate your backups
We can easily create automated backups by adding entries to your crontab file.
We will set up an automatic backup for it to run every day at 4:30 (if the computer is turned on at this time, of course). The syntax of the rsync command does not change at all.
Ctrl + O will write your changes to the file and Ctrl + X will close the nano editor.
Put a friendly face on Rsync
Those less comfortable with the command line can use one of the many programs that put a graphical user interface (GUI) on rsync. Two good examples are backup chance and Grsync. These two programs allow many rsync options to be selected via 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.
One of the Grsync parameters dialogs,
The luckyBackup program is more than just a wrapper for rsync. This is a backup program that uses rsync in the background. For example, LuckyBackup can create multiple "snapshots" of your backup. You can then "restore" the versions of the files in any of the snapshots.
One of the luckyBackup parameters dialogs.
To install Grsync
To install Grsync in Ubuntu, use this command:
sudo apt-get install grsync
To install Grsync in Fedora, use this command:
sudo dnf installs grsync
To install Grsync in Manaro, use this command:
sudo pacman -Syu grsync
To install LuckyBackup
To install luckyBackup in Ubuntu, use the following command:
sudo apt-get install luckybackup
To install luckyBackup in Fedora, use the following command:
sudo dnf installs luckybackup
In Manjaro, you need to install luckyBackup from the Arch User Repository (AUR). You can do this with the pamac package manager.
Do not risk this, save your data often
Backups are absolutely vital. Perform frequent backups, back up to multiple locations and media. Once it is configured, rsync can do all this for you.