How to backup files and folders in Debian 10

In the world of information technology, it is important to keep a copy of your data as much as possible so that you can use it if necessary in case of a disk failure or by mistake. Therefore, regular backups at the end of the day are good practice for a responsible computer user.

The rsync command is typically used to copy big data. However, the cp command is also used when copying a small number of directories and files to the local computer.

In this article, we will learn how to back up files and folders in Debian 10.

necessary condition

For this lesson to work, you need to have the following.

1. Two Debian 10 root machines

Backing up directories using the cp command

The first team we are going to discuss is cp. It is used to copy a small number of files and folders to the local computer. Therefore, it is practically not used in industry.

The syntax for the cp command is as follows.

cp -option1 -option2 destination source

For understanding, we are going to copy files from the desktop / log to Karim / logrot. Run the following command on the terminal.

cp -avr Desktop/log Karim/logro

A: It is used to save directory attributes such as file mode, ownership, timestamps, etc.

r: used to recursively copy directories in the main directory

v: used for verbose output

The following is an example output.

If you want to copy all files, directories and subdirectories to another directory, you can use the * wildcard. For example, the following command will copy all data from the existing directory Desktop / log / to Karim / logro /.

cp -avr Desktop/log/* Karim/logro/

The following is an example output.

Use wildcard in copy command

Backing up directories with rysnc

As we said, the most widely used command for backing up files and folders is rsync. So let’s discuss what it is and how it is used.

What is rysnc?

Rsync means remote synchronization, and it was written by Andrew Trigell and Paul Mackerras on June 19, 1996. This is an effective command to synchronize files and transfer between local and network machines. It is available by default on most systems. However, you can install it with the following simple commands if it is not available (Run commands with root privileges).

apt-get update
apt-get install rsync

You also need to install the ssh client and server on both network machines before data synchronization. Run the following commands with root privileges on both Debian 10o machines.

apt-get install ssh

Backing up directories on a local computer

The basic syntax for synchronizing files on a local computer is as follows.

rsync option

If you want to save metadata, such as ownership, access rights, creation date, etc., you must use the -a option. If you want to recursively copy directories inside a directory, you must use the -r option.

rsync -ar sourcedirectory destinationdirectory

Similarly, if you want to see progress during synchronization, use the -v option. The teams should look like this:

rsync -avr sourcedirectory destinationdirectory

Suppose we want to synchronize files and folders located in Desktop / log with Karim / logro, the command should look like this.

rsync -avr Desktop/log Karim/logro

The following is an example output.

Use rsync command to backup files

Let’s discuss another example and say that we have a data-1 folder located on hard drive 1 (/ media / hdd1 / data-1), and you want to synchronize it with the second hard drive in / media / hdd2 /. A complete team should look like this.

rsync -avr / media / hdd1 / data-1 / media / hdd2 /

The command, when executed, will create the data-1 directory on the second hard drive and copy all the contents along the destination path / media / hdd2 /.

Backing up files and directories over a network

The syntax is slightly different when transmitting data over a network. If you want to synchronize the local directory with the remote directory, the command should look like this.

Rsync [-options] PathofSourceFolder username @ IPAddressofRemoteHost: PathofDestinationFolder

Suppose I have a test folder located inside my local machine in / home / karim / testfolder, and I want to synchronize it in / home / karim. The remote user is Karim, and the IP address of the machine is 10.1.1.2. Run the following command on the terminal.

rsync -avr /home/karim/testfolder [email protected]:/home/karim/

As soon as you run this command, you will be prompted to enter the password for the remote computer.

The following is an example of output after directory synchronization.

Remote backup with rsync

If you want to synchronize the remote directory with the local directory, the command should look like this.

Rsync [-options] username @ IPAddressofRemoteHost: PathofSourceFolder PathofDestinationFolder

Suppose we have a remote folder called “testfolder” located at / home / karim /, and I want to synchronize with the local machine at / home / karim /. The IP address of the remote computer is 10.1.1.2, and the username is karim.

Run the following command on the terminal.

rsync -avr  [email protected]:/home/karim/ /home/karim/testfolder

The following is an example output.

Rsync backup

How to automate backups

It’s more convenient to automate backups so that system engineers do not have to worry about manually executing commands and doing backups every day.

On Linux, there is a well-known tool called crontab that is used to automate the backup process. We can plan the launch of all of the above teams daily, weekly or monthly. If you did not install crontab in your Linux distribution, run the following commands in a terminal with sudo privileges.

apt-get update
apt-get install cron

After installing crontab, run the following command on the terminal to open the crontab editor.

crontab -e

An example output should look like this.

Set up backup cronjob

Crontab has the following five fields,

m h dm m dw command

m: indicates the minute (0-59)

h: indicates hour (0-23)

dm: indicates the day of the month (1-31)

m: indicates month (1-12)

dw: indicates the day of the week (0-6, where 0 is Sunday)

Let’s look at the previous example of synchronizing a directory from one drive to another and say that we want to do this every day at 12 o’clock. The cron task should be as follows.

0 0 * * *  rsync -avr /media/hdd1/data-1 /media/hdd2/

Suppose you want to make backups every month on Sunday at 12 noon, the cron job should be written as follows.

0 0 1 * * rsync -avr /media/hdd1/data-1 /media/hdd2/

Conclusion

You read how we can back up using the extremely powerful rysnc team. We have completed the crontab article. Rsync and crontab are also a useful combination.

How to backup files and folders in Debian 10

Sidebar