Data Backup And Recovery In Linux

data-backup-and-recovery-in-linux


Since applications are built using Linux files to store data, it is important for the application to backup important data files for data redundancy and recovery in case of file corruption or accidental deletion and overwriting of files.
Let's consider a scenario in which one is working on development, and the administrator receives a request to update a file, but when checking a file lost due to a bad sector in the disk. So in this scenario taking the data backup is a routine task for the admin to preserve the data.
In this section, you will learnt to perform backup and recovery of data files by means of cp and mv commands for backup, tar and cpio for recovery.

Data Backup Using CP And MV Command



Data Backup Using CP Command

To copy the content of the one file to another directory or partition for redundancy, "cp" can be used. To take backup of the existing destination file before overwriting it, use the command with the option "-b or --backup"

Option Usage
-f Force copy by removing the destination file if needed
-i Interactive - ask before overwrite
-l Link files instead of copy
-L Follow symbolic links
-b Take a backup of the destination file
cp [option]...SOURCE...DIRECTORY
cp -b login.dat ./backup_directory/login.dat
cp --backup login.dat ./backup_directory/login.dat


Data Backup Using MV Command

The "mv" command is used to move or rename files. Unlike the "cp" command, with the "mv" command file will be moved to the destination location without having a copy at the source. While moving it to another partition, backup of it can be taken and stored. While moving the files to another directory, it provides an option to take backup of the file as well. This can be done with the help of "-b" or "--backup" command option.

Option Usage
-n Does not overwrite existing file
-i Interactive - ask before overwrite
-u Move only when the source file is newer than the destination file
-b Take a backup of the destination file
mv [option]...SOURCE...DIRECTORY
mv -b login.dat ./backup_directory/login.dat
mv --backup login.dat ./backup_directory/login.dat

Difference Between CP And MV command

The difference between CP and MV command are as follows :

CP Command MV Command
A command which is used to create similar files or a directory in a new location. A command which is used to relocating the original file or a directory in a new location
Will not effect original content Will delete original content
cp [option]...SOURCE...DIRECTORY mv [option]...SOURCE...DIRECTORY
Create duplicate file Tranfer the original file

Data Recovery Using TAR And CPIO Command



Data Recovery Using TAR Command

GNU "tar" saves many files together into a single tape or disk archive, and can restore individual files from the archive Options. In this we will understand how to archive files and understand how to retrieve the data from archived files.

Option Usage
-cvf Create archive from file
-tvf List all files in archive.tar verbosely
-xf Extract all file from archive.tar
-cvzf Create a compressed gzip archive file
-rvf Append a file to the exiting archive
\\To create an archive file the following command is used.
tar -cvf login.tar /home//backup/
\\To get the files out of the archive, "tar" command with "-x" option can be used.
tar -xvf login.tar
\\To provide compression on the archive, use "-z" command option.
tar -cvzf all_login.tar /home//backup/
\\To extract a gzipped tar, use "xvzf" option.
tar -xvzf all_login.tar
\\To add files to the existing archive, the command option "-r" is to be used.
tar -rvf login.tar registration.dat

Data Backup Using CPIO Command

GNU "cpio" is a tool for creating and extracting archives, or copying files from one place to another. It handles a number of cpio formats as well as reading and writing tar files.
Copy-out mode : In copy-out mode, cpio copies files into an archive. It reads a list of filenames, one per line, on the standard input, and writes the archive onto the standard output.
Copy-in mode : In copy-in mode, cpio copies files out of an archive or lists the archive contents. It reads the archive from the standard input.
Copy-pass mode : In copy-pass mode, cpio copies files from one directory tree to another, combining the copy-out and copy-in steps without actually using an archive. It reads the list of files to copy from the standard input. The directory into which it will copy them is given as a non-option argument.

Option Usage
-ov Create an archive
-idv Extract file from archive
-it List the file in archive
-F Create/Extract tar archive file
\\To create an archive.
cpio -ov >login.cpio
\\To extract the files from the archive.
cpio -idv </home/backup/login.cpio
\\To create tar archive files using "cpio" use the command option "-F"
ls |cpio -ov -H tar -F all_login.tar
\\To view the content of the tar archive files using "cpio" command
cpio -it -F all_login.tar
\\To extract the files from tar archive
cpio -idv -F ./../all_login.tar


Best Linux Books



1. Learn Linux Quickly

Author :- Ahmed AlKabary
Edition :- 21 August 2020
Published by :- Packt Publishing Limited

This book doesn't assume any prior Linux knowledge, which makes it perfect for beginners. Intermediate and advanced Linux users will also find this book very useful as it covers a wide range of topics necessary for Linux administration. Learn over 116 Linux commands to develop the skills you need to become a professional Linux system administrator.

2.Linux: The Complete Reference

Author :- Richard Petersen
Edition :- 6th Edition
Published by :- McGraw Hill Education

Linux: The Complete Reference, Sixth Edition includes details on the very different and popular Debian (Ubuntu) and Red Hat/Fedora software installation and service management tools used by most distributions. This is a must-have guide for all Linux users.
This book explains how to get up-and-running on Linux, use the desktops and shells, manage applications, deploy servers, implement security measures, and handle system and network administration tasks.

Best Web Development Courses And Tutorials

1. Linux Command Line Basics(Udemy)

2. Linux Shell Scripting: A Project-Based Approach to Learning(Udemy)

3. Kali Linux Tutorial For Beginners(Udemy)

Also Check