How to Backup Files and Folders in Debian 10

In the IT world, it is important to keep a copy of your data as possible in order to utilize it when required in case of disk failure or when removed by mistake.  Therefore taking a regular backup at the end of the day is a good practice of a responsible computer user.

‘rsync’ command is normally used to copy the large data. However, ‘cp’ command is also used when copying a small number of directories and files on a local machine.

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


You need to have the following for this tutorial to work.

1. Two Debian 10 machines with root privileges

Back up directories with cp command

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

The syntax of ‘cp’ command is as follows.

cp -option1 -option2 source destination

For understanding, we are going to copy the 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 preserve the directory attributes such as file mode, ownership, timestamps etc.

r: It is used to recursively copy the directories which are inside main directory

v: It is used to verbose the output

The following is the sample output.

Copy directories with CP command

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

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

The following is the sample output.

Use wildcard in copy command

Back up directories with rysnc

As we have already said that the most widely used command to backup files and folders is the ‘rsync’. Therefore, let’s discuss what it is and how it is used.

What is rysnc?

Rsync stands for remote sync and it was written by Andrew Tridgell and Paul Mackerras on 19 June 1996. It is an efficient command for file syncing and transfer between local and network machines. It is available by default on most of the systems. However, you can install it with the help of following simple commands if it is not available (Run the commands with root privileges).

apt-get update
apt-get install rsync

You also need to have an ssh client and server installed on both network machines before syncing the data. Run the following commands with root privileges on both Debian 1o machines.

apt-get install ssh

Back up directories on the local machine

The basic syntax when syncing files on the local machine are as follows.

rsync option source-directory destination-directory

If you want to keep the metadata like ownership, permissions, date of creation, etc. You have to use -a option. If you want to recursively copy the directories inside the directory, you have to use -r option.

rsync -ar sourcedirectory destinationdirectory

Similary, if you want to see the progress while syncing is in progress, use the -v option. The commands should like as follows,

rsync -avr sourcedirectory destinationdirectory

Suppose we want to sync files and folders located at Desktop/log to Karim/logro, the command should look like as follows.

rsync -avr Desktop/log Karim/logro

The following is the sample output.

Use rsync command to backup files

Let’s discuss one more example and say we have a folder data-1 located at hard disk 1 (/media/hdd1/data-1) and you want to sync it to the second hard disk at /media/hdd2/. The complete command should look like as follows.

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

Command when executed will create a data-1 directory in the second hard drive and will copy all the contents on the destination path /media/hdd2/.

Backup files and directories over the network

The syntax is slightly different when transferring data over the network. When you want to sync a local directory with a remote directory, the command should look like as follows.

rsync [-options] PathofSourceFolder username@IPAddressofRemoteHost:PathofDestinationFolder

Suppose I have a testfolder located inside my local machine at /home/karim/testfolder and I want to sync it at /home/karim. Remote user is ‘karim’ and machine IP address is Run the following command on the terminal.

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

As soon as you execute this command, you will be asked to enter the password of the remote machine.

Below is the sample output after syncing the directory.

Remote backup with rsync

When you want to sync a remote directory with a local directory, the command should look like as follows.

rsync [-options] username@IPAddressofRemoteHost:PathofSourceFolder PathofDestinationFolder

Suppose we have a remote folder ‘testfolder’ located at /home/karim/ and I want to sync with local machine at the location /home/karim/. The remote machine IP address is and the user name is karim.

Execute the following command on the terminal.

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

Below is the sample output.

Rsync backup

How to automate the backup

It is more convenient to automate the backup so that system engineers do not need to worry about manually executing the commands and taking the backups every day.

There is a famous tool called ‘crontab’ in Linux which is used for automating the backup process. We can schedule to run all of the above commands daily, weekly, or monthly. If you haven’t installed crontab on your Linux distribution, run the following commands on the terminal with sudo privileges.

apt-get update
apt-get install cron

Once you have installed crontab, run the following command on the terminal to open the crontab editor.

crontab -e

Sample output should look as follows.

Set up a backup cronjob

The crontab has the following five fields,

m h dm m dw command

m: specifies minute (0-59)

h: specifies the hour (0-23)

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

m: specifies the month (1-12)

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

Let’s take a previous example of syncing a directory from one disk to another disk and say we want to do this every day at 12 PM, the cron job should be as follows.

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

Suppose you want to take a backup every month on Sunday at 12 PM, the cron job should be written as follows.

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


You have read how we can take backup using extremely powerful command ‘rysnc’. We have concluded the article with a ‘crontab’. ‘rsync’ and ‘crontab’ is also a useful combination.