In the IT world, it is important to keep a copy of your data wherever possible so that you can use it in the event of a hard drive failure or accidental removal. Therefore, it is good practice for a responsible computer user to have a regular backup at the end of the day.
The ‘rsync’ command is usually used to copy the large data. However, the ‘cp’ command is also used when copying a small number of directories and files to a local computer.
In this article we will learn how we can backup files and folders in Debian 10.
Here are what you need for this tutorial to work.
1. Two Debian 10 machines with root privileges
Back up directories with the cp command
The first command we’ll discuss is ‘cp’. It is used to copy a small number of files and folders to a local computer. Therefore, it is practically not used in industry.
The syntax of the ‘cp’ command is as follows.
cp -option1 -option2 source target
To understand, let’s 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 keep the directory attributes like file mode, ownership, timestamp, etc.
r: It is used to recursively copy the directories that are in the main directory
v: Used to describe the output in detail
The following is the sample output.display
If you want to copy all files, directories and subdirectories to another directory, you can use the placeholder *. For example, the following command copies all data from an existing directory Desktop / log / to Karim / logro /.
cp -avr Desktop/log/* Karim/logro/
The following is the sample output.
Directories with rysnc. to back up
As mentioned earlier, the most common command used to back up files and folders is ‘rsync’. So let’s discuss what it is and how to use it.
What is rysnc
Rsync stands for Remote Sync and was written on June 19, 1996 by Andrew Tridgell and Paul Mackerras. It’s an efficient command for synchronizing and transferring files between local and network computers. It is available by default on most systems. However, you can install it using the following simple commands if it is not available (run the commands with root privileges).
apt-get install rsync
In addition, an SSH client and an SSH server must be installed on both network computers before you can synchronize the data. Execute the following commands with root privileges on both Debian 10 machines.
apt-get install ssh
Back up directories on the local computer
The basic syntax for synchronizing files on the local computer is as follows.
rsync option source directory target directory
If you want to keep the metadata like ownership, permissions, creation date, etc., you need to use the -a option. If you want to recursively copy the directories within the directory, you must use the -r option.
rsync -ar source directory target directory
Similarly, if you want to see the progress during the sync, use the -v option. The commands should look like this:
rsync -avr source directory target directory
Assuming we want to sync files and folders that are under Desktop / log with Karim / logro, the command should look like this.
rsync -avr Desktop/log Karim/logro
The following is the sample output.
Let’s discuss another example and say we have a folder data-1 on disk 1 (/ media / hdd1 / data-1) and you want to sync it with the second disk at / media / hdd2 /. The full command should look like this.
rsync -avr / media / hdd1 / data-1 / media / hdd2 /
When the command is run, a data-1 directory is created on the second hard drive and all content is copied to the destination path / media / hdd2 /.
Back up files and directories over the network
When transferring data over the network, the syntax is slightly different. If you want to synchronize a local directory with a remote directory, the command should look like this.
rsync [-options] Path source folder [email protected]: PathofDestinationFolder
Suppose I have a test folder on my local computer under / home / karim / testfolder and I want to synchronize it under / home / karim. The remote user is ‘karim’ and the computer 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 computer.
Below is the sample output after synchronizing the directory.
If you want to synchronize a remote directory with a local directory, the command should look like this.
rsync [-options] [email protected]: PathofSourceFolder PathofDestinationFolder
Suppose we have a remote ‘testfolder’ folder under / home / karim / and I want to synchronize with the local computer at / home / karim /. The IP address of the remote computer is 10.1.1.2 and the user name is karim.
Run the following command on the terminal.
rsync -avr [email protected]:/home/karim/ /home/karim/testfolder
Below is the sample output.
How to automate the backup
It is more convenient to automate the backup so that system engineers don’t have to worry about running the commands manually and making backups every day.
On Linux there is a well known tool called ‘crontab’ that is used to automate the backup process. We can schedule to run all of the above commands on a daily, weekly, or monthly basis. If you don’t have crontab installed on your Linux distribution, run the following commands on the terminal with sudo privileges.
apt-get install cron
After installing crontab, run the following command on the terminal to open the crontab editor.
The sample output should look like this.
The crontab has the following five fields,
mh dm m dw command
m: indicates the minute (0-59)
h: indicates the hour (0-23)
dm: indicates the day of the month (1-31)
m: indicates the month (1-12)
dw: Indicates the day of the week (0-6 where 0 is Sunday)
Let’s take a previous example of syncing a directory from one hard drive to another, and let’s say we want to do it every day at 12 noon. The cron job should look like this.
0 0 * * * rsync -avr /media/hdd1/data-1 /media/hdd2/
Assuming you want to make a backup every Sunday at 12:00 noon, then the cron job should be written like this.
0 0 1 * * rsync -avr /media/hdd1/data-1 /media/hdd2/
You’ve read how we can do backups using the extremely powerful ‘rysnc’ command. We ended the article with a ‘crontab’. ‘rsync’ and ‘crontab’ is also a useful combination.