Magazine

Backup of Files in Linux Using Command Line

Posted on the 11 May 2021 by Satish Kumar @satish_kumar86

In IT or our day-to-day computer industry activities, takingbackupis 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 utilityrsyncis the most widely usedbackupcommand in Linux forbacking upor 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

rsyncis 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 usingrsyncis as follows:

$ rsync -options source_folder destination_folder

Let us consider that you want to copy from/home/student/data_folderto 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 -roption from recursively copying folder with all of its subfolders and files.

If we want to ensure that in thesourcefolder, 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 -deleteoption.

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

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-zoption. 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,rsyncdeletes any partially transferred files if the backup operation is interrupted. If we want to keep partially transferred files, then we need to add the-Poption. 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 takingbackupacross the network, we will need to install thesshprotocol 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 user@ip_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 user@ip_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 number12345while using thesshcommand, then thersynccommand 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 takingbackupactivity 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 thersynccommand 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 at30minutes past19hours or7pm 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.


Back to Featured Articles on Logo Paperblog