Yuri Seleznev / Shutterstock.com

Don’t risk data loss. Back up your valuable data from the Linux command line. For this we will use the command rsync and we even found some nice GUIs for it.

There are many ways to back up your files. We wanted to show you a reliable, flexible and reliable way to protect your data. We chose rsync because of its respected algorithms that calculate the differences between files in the source directory and the target directory. Only the differences between the two versions of the file are transferred, not the entire file if this can be avoided.

When this efficiency is combined with its solid track record of doing file copies and directory synchronization since the mid-1990s, rsync is an excellent candidate for creating backups from the Linux command line.

In addition, there are independent programs that act as an interface to rsync . They provide a graphical user interface (GUI) for rsync which some people may find easier to use.

The easier and faster it is to make a backup, the more likely you are to make it.

Using rsync with an external hard drive

To back up your data to an external hard drive, the hard drive must be mounted and available to you. If you can write to it, then rsync . In this example, an external USB hard drive named SILVERXHD (for «Silver eXternal Hard Drive») is connected to a Linux computer. It was automatically mounted by the operating system.

You will need to know the path to the drive. In GNOME, open the Nautilus file browser and look for the drive name in the sidebar.

Hover your mouse over the name of the external drive and the tooltip will display the path to the drive.

tooltip for external drive

In this example, the tooltip tells us that the file system mount point on the external drive is «/media/dave/SILVERXHD».

If your file browser doesn’t, navigate to your external drive and open a terminal window at that location. Use command pwd to print the path to the terminal window.

Copy content from source directory

To use rsync To copy the contents of the directory to the backup destination, use the following command.

Parameter -r (recursive) forces rsync copy all nested subdirectories and their contents. Note that there is a slash «/» at the end of the word «SILVERXHD», but it is wrapped to the next line in the screenshot.

  rsync -r / home / dave / документы / / media / dave / SILVERXHD / 

rsync -r /home/dave/Documents//media/dave/SILVERXHD/ in a terminal window

The file will be copied and you will be returned to the command line.

If we look at the external USB drive, we can see that the directories in the Documents directory have been copied to the external drive’s root directory.

  Ls 

ls in a terminal window

Copying the source directory and its contents

If you wanted to copy the Documents directory and its contents to an external drive, remove the «/» at the end of «/home/dave/Documents» on the command line, like so:

  rsync -r / home / dave / документы / медиа / dave / SILVERXHD / 

rsync -r /home/dave/documents/media/dave/SILVERXHD/ in a terminal window

To avoid confusion, I removed the two previously copied directories from the external drive before running this second command.

If we let the second copy complete and take another look at the external drive, we can see that the Documents directory has been copied. Its contents are in this directory. They are not in the root of the external drive.

ls in a terminal window

Copying to a specific target directory

To copy to a specific directory on the target hard drive, add the directory name to the target path. Suppose we want to copy the contents of the «/home/dave/Documents» directory to the «backups» directory on an external drive.

We will do this with the following command.

  rsync -r / home / dave / документы / / media / dave / SILVERXHD / backups / 

rsync -r /home/dave/documents//media/dave/SILVERXHD/backups/terminal window

Checking the external drive, we can see that a backup directory has been created and that directory contains the contents of the «/home/dave/Documents» directory.

  Ls 
  резервные копии 

Output from ls in a terminal window

Maintain file ownership and permissions

Use the parameter -a (archive) to preserve file attributes such as modification dates, file ownership, permissions, etc. for copied files, symlinks, and special block files.

  rsync -ra / home / dave / документы / / media / dave / SILVERXHD / backups / 

rsync -ra /home/dave/Documents//media/dave/SILVERXHD/backups/ in a terminal window

Using detailed mode

Option -v (verbose) forces rsync list files as they are copied.

  rsync -rav / home / dave / Документы / / media / dave / SILVERXHD / backups / 

rsync -rav /home/dave/Documents//media/dave/SILVERXHD/backups/ in a terminal window

A summary of the backup is displayed after the backup is completed.

verbose output from rsync in a terminal window

  • Sent : bytes transferred to the target.
  • Received : bytes received at the host.
  • Byte/sec : Effective transfer rate.
  • Overall size : represents the size of the data that would be sent if you not used rsync . At subsequent rsync it will only transfer file differences. This figure will represent data that not had to be transferred.
  • Acceleration : This is the ratio between the amount of data that should have been sent and the total amount of data that is. If a rsync it is necessary to copy all the files in their entirety (for example, at the first start), the speedup will be 1.0. When rsync is used next time, it optimizes the transmission. It will only send the differences between files, not entire files. Files without changes will be ignored. The speedup will be the ratio between the small amount of data that needed to be transferred and the total size of the files.

Using the Progress Parameter

Option -P (progress) forces rsync generate a small progress report after copying each file.

  rsync -raP / home / dave / Документы / / media / dave / SILVERXHD / backups / 

rsync -raP /home/dave/Documents//media/dave/SILVERXHD/backups/ in a terminal window

The provided information can be seen between each copied file.

rsync output with progress in terminal window

Information provided:

Adding more speed

To speed up the transfer, use -z (compression). This compresses the file on transfer, but the file is stored uncompressed in the target directory.

The compression option will not provide significant benefits for transfers that include many small files. For collections of large files, this can significantly reduce transfer time.

We also use the option --partial here. rsync will delete partially transferred files caused by network failures or other failures. Option --partial makes rsync leave partially transferred files on the target --partial . Pure time rsync does not require retransfer of parts of partially transferred files.

Note that you may choose not to use this option if there is a risk of someone mistaking partially transferred files for fully transferred files.

  rsync -ravz --partial / home / dave / Документы / / media / dave / SILVERXHD / backups / 

rsync -ravz --partial /home/dave/Documents//media/dave/SILVERXHD/backups/ in a terminal window

In our example, the benefits are negligible.

rsync output in terminal window

The acceleration coefficient has improved, but by two hundredths of a percent! In a real scenario, your speed improvements will be more impressive.

Using rsync over the network

So far, we have focused on an external USB drive. To use a network location as a backup target, use the path to that location on the command line. There is a network-attached storage device (NAS) on the network that this article was researched on.

We can use the same trick we used earlier to determine the path to the NAS by hovering over the connection to that device in Nautilus.

No special options for network backup; these are all the options we have already used.

  rsync -ravz --partial / home / dave / Документы / / media / dave / NAS / dave / backups / 

rsync -ravz --partial /home/dave/Documents//media/dave/NAS/dave/Backups/ in a terminal window

There is no difference in the output format.

rsync output in terminal window

Unsurprisingly, bytes per second have improved significantly.

If we run rsync once again, we will see that there are no files to transfer because there were no changes, but there are still some bytes transferred back and forth. This is the amount of data that needs to be transferred in order to compare the list of files on the target with the list of files on the source.

fromrsync output without files to transfer in terminal window

The acceleration factor is an order of magnitude better in this case. In practice, your performance numbers will be somewhere between our two pseudo-artificial readings.

Using rsync over SSH

rsync supports backup via SSH connection. We need to specify the user account name and SSH location on the command line. Here we are using the network name, but you can also use the IP address.

Notice the «:» between the SSH connection details and the start of the network path on the remote target.

  rsync -ravz --partial / home / dave / Documents / dave@sulaco.local: / home / dave / Backups / 

rsync -ravz --partial /home/dave/Documents/dave@sulaco.local:/home/dave/Backups/ in a terminal window

You will be prompted to enter the user account password on remote computer. This is not your password on the source computer.

rsync over ssh connection in a terminal window

The backup will proceed as usual. Bandwidth is not as high as a normal network connection due to the encryption and decryption that takes place in a secure shell connection.

Automate your backups

We can easily create automatic backups by adding entries to your crontab file.

  crontab -e 

crontab -e in a terminal window

We will set the automatic backup to run every day at 04:30 (if the computer is turned on, of course). Command syntax rsync doesn’t change at all.

crontab open in nano in a terminal window

Ctrl + O will write your changes to the file and Ctrl + X will close the editor nano .

RELATED: How to Schedule Tasks in Linux: An Introduction to Crontab Files

Putting a friendly face on Rsync

People who are not comfortable working with the command line can use one of the programs that install a graphical user interface (GUI) on rsync . Two good examples are luckyBackup and Grsync . Both of these programs allow you to choose many options rsync through the user interface.

Program Grsync concentrates on being a visual shell for rync . It provides easy access to options rsync and adds only a limited set of new features.

One of the Grsync settings dialogs,
One of the Grsync settings dialogs,

Program luckyBackup is much more than just a shell for rsync . This is a backup program that uses rsync behind the scenes. For example, luckyBackup can take multiple «snapshots» of your backup. You can then «roll back» to the file versions in any of the snapshots.

One of the luckyBackup settings dialogs
One of the luckyBackup settings dialogs.

To install Grsync

to install Grsync on Ubuntu, use this command:

  sudo apt-get установить grsync 

sudo apt-get install grsync in terminal window

to install Grsync on Fedora, use this command:

  sudo dnf установить grsync 

sudo dnf install grsync

to install Grsync in Grsync use this command:

  Судо Пакман -Сю Грсинк 

sudo pacman -su grsync in a terminal window

To install luckyBackup

to install luckyBackup on Ubuntu, use the following command:

  sudo apt-get установить luckybackup 

sudo apt-get install luckybackup in terminal window

to install luckyBackup on Fedora, use the following command:

  sudo dnf установить luckybackup 

sudo dnf install luckyback in terminal window

In Manjaro you must install luckyBackup from the Arch Users Repository (AUR). You can do it with pamac packages pamac .

pamac package manager

Play it safe, back up your data often

Backups are absolutely essential. Back up often, back up to many locations, and back up to different media After setting rsync can do it all for you.

Похожие записи