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

The ‘rsync’ command is normally used to copy large data. However, the ‘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 to back up files and folders in Debian 12.

Prerequisite

It would be best if you had the following for this tutorial to work.

1. Two Debian 12 machines with root privileges

Back up directories with cp command

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

The syntax of ‘cp’ command is as follows.

cp -option1 -option2 source destination

To help you understand, we will 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.

How to Backup Files and Folders in Debian 12 Debian linux shell

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.

How to Backup Files and Folders in Debian 12 Debian linux shell

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 update
apt install rsync

Before syncing the data, you also need to install an SSH client and server on both network machines. 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 copy the directories inside the directory recursively, 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.

How to Backup Files and Folders in Debian 12 Debian linux shell

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/

When executed, the command will create a data-1 directory on the second hard drive and copy all its contents to 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 10.1.1.2. 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.

How to Backup Files and Folders in Debian 12 Debian linux shell

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 10.1.1.2 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.

How to Backup Files and Folders in Debian 12 Debian linux shell

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 automates 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 update
apt 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.

How to Backup Files and Folders in Debian 12 Debian linux shell

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/

Conclusion

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.