Backup of files in Linux using command line

May 11, 2021

Comments

Adsense

Adsense

Adsense

Adsense

In IT or our day-to-day computer industry activities, taking backup is one of the most important activities. Previously, offices were required to keep important paper in a safe place; but if a fire breaks out, then everything is finished. In the digital world, taking backup makes our life easier and safeguards us against data loss.

There are many software tools available on the market for taking software backups. We will study one of the most popular software backup command-line utilities, rsync.

Backup command rsync

The command-line utility rsync is the most widely used backup command in Linux for backing up or synchronizing data. This utility was developed in 1996 by Andrew Tridgell and Paul Mackerras.

This utility is mostly installed in all popular Linux distributions. If it is not installed, then run the following commands:

For CentOS or Red Hat:

# yum install rsync

For Debian or Ubuntu

# apt-get install rsync

rsync is a powerful utility. It can copy or synchronize files in the same computer or across the network in another continent-based computer over the internet.

The basic syntax for using rsync is as follows:

$ rsync -options source_folder destination_folder

Let us consider that you want to copy from /home/student/data_folder to your mounted USB pen drive /media/usb_drive/data_folder. Then, the backup command would be:

For CentOS or Red Hat:

$ sudo rsync -a /home/student/test /run/media/student/name_of_drive/test

For Debian or Ubuntu:

$ sudo rsync -a /home/student/test /media/student/ name_of_drive /test

The preceding command will copy a test folder to your mounted USB pen drive. Of course, you will need to check the exact path for the mounted pen drive. As per the volume label of the pen drive, the exact path of the destination folder may change. We have used the -r option from recursively copying folder with all of its subfolders and files.

If we want to ensure that in the source folder, a certain file or folder is deleted, then corresponding files or folders should be deleted from the destination backup folder as well, and for that we need to use the -delete option.

If we want to backup symbolic link files along with ownership, file permissions, and time stamps, then we should use option -a.

Then, the updated command would be:

For CentOS or Red Hat:

$ sudo rsync -a -delete /home/student/test  /run/media/student/ganesh/test

For Debian or Ubuntu:

$ sudo rsync -a -delete /home/student/test  /media/student/ganesh/test

If want to observe the progress of the backup, then add the -v option.

For CentOS or Red Hat:

$ sudo rsync -av -delete  /home/student/test /run/media/student/ganesh/test

For Debian or Ubuntu:

$ sudo rsync -av -delete /home/student/test  /media/student/ganesh/test

If file sizes are very big and you want to compress the files and then take a backup, then simply add the -z option. This will save network bandwidth if you are going to transfer GB-or TB-sized data.

For CentOS or Red Hat:

$ sudo rsync -avz -delete /home/student/test /run/media/student/ganesh/test

For Debian or Ubuntu:

$ sudo rsync -avz -delete /home/student/test /media/student/ganesh/test

By default, rsync deletes any partially transferred files if the backup operation is interrupted. If we want to keep partially transferred files, then we need to add the -P option. The updated backup command will be as follows:

For CentOS or Red Hat:

$ sudo rsync -avzP -delete /home/student/test /run/media/student/ganesh/test

For Debian or Ubuntu:

$ sudo rsync -avzP -delete /home/student/test  /media/student/ganesh/test

Backup across the network

For taking backup across the network, we will need to install the ssh protocol package. Normally, it will already be installed. If it is not installed, then use the following command:

For CentOS or Red Hat

# sudo yum install ssh

For Debian or Ubuntu:

# sudo apt-get install ssh

The command to synchronize data from across the network to your local folder will be as follows:

$ rsync -avzP --delete -e ssh [email protected]_address:source-folder /destination-folder

Look at the following example:

$ rsync -avzP --delete -e ssh [email protected]: /home/student/data-folder /home/student/data-folder

If we want to synchronize local folders to a remote computer, then the command would be as follows:

$ rsync -avzP --delete -e ssh source-folder [email protected]_address:destination-folder

The actual command would be as follows:

$ rsync -avzP --delete -e ssh /home/student/data-folder [email protected]:/home/student/data-folder  

You will need to replace the username and IP address of the destination PC with the required username and password.

If the remote PC has been configured with port forwarding, such as when we have to use port number 12345 while using the ssh command, then the rsync command will be as follows:

$ rsync -avzP --delete -e 'ssh -p 12345' [email protected]: /home/student/data-folder /home/student/data-folder 

Automating backup activity

If you want to automate taking backup activity every day at 7.30 pm, then you will need to use the crontab functionality.

You will need to enter the crontab -ecommand and enter the rsync command in it.

For regular backup at 7.30 pm every day, enter the following line in the crontab editor:

30 19 * * * rsync -avz -delete /home/student/data-folder  /media/usb_drive/data-folder 

The preceding command will back up data at 30 minutes past 19 hours or 7 pm every day.

I suggest you keep one backup of important data locally and one copy remotely. Local copy backup should be undertaken more frequently, and remote backup less frequently. Of course, you will need to decide backup frequency according to the importance of the data and your business requirements.

Satish Kumar

Satish Kumar

I am Satish Kumar, Founder of LinuxConcept. Linux and F.O.S.S enthusiast, love to work on open source platform and technologies.

Adsense

Adsense

Adsense

Adsense

Adsense

Adsense

Adsense

Adsense

Adsense

Related Posts

Using awk in Linux

awk is a program that has its own programming language for performing data-processing and generating reports. The GNU version of awk is gawk. awk processes data, which can be received from a standard input, input file, or as the output of any other command or process....

sed – non-interactive stream editor uses in Linux

The stream editor (sed) is a very popular non-interactive stream editor. Normally, whenever we edit files using the vi editor, we need to open the file using the vi command, then we interact with the file to see the content of the file on screen, then edit it, and...

0 Comments

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

eight + five =

News & Updates

Join Our Newsletter

Adsense

Adsense

Adsense

Adsense