Backup is important to organizations that work with sensitive data and confidential files. There are many different scenarios where you might want to backup your Linux system. For instance, a major system upgrade or an important component installation can easily go wrong. In that case, a system backup would save you from any problem.

Recommended Read: Tar command Examples : Compress & Decompress the files\directories

Also Read: Executing Commands and Scripts at Reboot & Startup in Linux

There are a number of available Linux backup solutions with reliable performance. This article reviews some Linux backup options, including examples of using tar.

 

Linux Backup Types

Selecting the right backup option for your Linux system can be overwhelming considering the number of available options. The list below reviews some of the most common types of Linux backups.

 

Full Backups

Full backups make a complete copy of all the data on your system. A full backup requires more storage space compared to other backup types and it takes a lot of time. However, restoring data from a full backup is relatively fast.

https://services.vlitag.com/vpaid/?q=9ba3f2539ddb6b2df45a4f25fbfd7843&defaultVolume=&page_url=https://linuxtechlab.com

Some Linux admins try to save storage space by backing up small batches of data or folders.  Admins responsible for larger data sets usually implement full backups only occasionally, combining them with other backup types. A full backup is usually the starting point of other backup types.

 

Advantages of full backups:

  • Better storage management—the entire data set is stored in a single backup file. As a result, you do not need to search through different files to find the file you want to restore.
  • Fast data recoveryyou can easily restore the data because the data is stored as complete files.

 

Disadvantages of full backups:

  • Redundant backups—full backups waste a lot of storage space because most files rarely change and each backup is simply a copy of the previous one.
  • Longer to perform—full backups take longer than other types of backup because everything is backed up at once.
  • Security issues—an entire copy of the data can be stolen because everything is stored in one place.

 

Incremental Backups

Incremental backups reduce the time and network bandwidth required in full backups. A full backup is the starting point of an incremental backup. When a full backup is in place, admins backup only the blocks of data that have changed since the last backup. After a certain amount of time, you have to make a new full backup, depending on the retention policy.

For example, if you make a full backup on Tuesday, on Wednesday you backup only the data that changed since the full backup. On Thursday you backup only the data that changed since Wednesday.

Advantages of incremental backup:

  • Reduced backup time— backing up only the data that changed takes significantly less time than backing up all data.
  • Less storage space—backing up only certain files takes less storage space than full backup.

Disadvantages of incremental backup:

 

  • Slow full restoreto fully restore the data you need to restore multiple increments and the initial full backup.

 

  • Successful recovery depends on the integrity of all incrementsyou have to restore all increments to recover a particular file. If any increment restore fails, the entire file will be in an unrecoverable state.

 

 

Linux System Backup Examples

The following examples show how to create a full backup and an incremental backup using tar. Tar stands for Tape Archiver. Tar is a popular program for backing up and archiving on Linux systems. The tool functions both as a standalone command and a utility that responds to a variety of commands.

 

Creating a full backup with tar

First, you have to create a directory called bin. Then follow the next steps to backup your directory.

1.Inform Linux that you use bash as the interpreter

#!/bin/bash

2.Define the time stamp of your tar back up. The time stamp is used to separate several backups from one another

DATE=$(date +%Y-%m-%d-%H%M%S)

3.Specify the directory where you want to store the backup

BACKUP_DIR="/mydirectory/backup"

4.Specify the directories you want to backup

SOURCE="$HOME/sourcedirectory1"

5.The tar command that creates a backup file (mybackup) in the /mydirectory/ directory

tar -cvzpf $BACKUP_DIR/mybackup-$DATE.tar.gz $SOURCE

 

Create an incremental backup with tar

The following script stores incremental backups in a folder named by the date of the backup. You will also need cron in addition to tar. Cron enables time-based execution of processes.

1.Define the interpreter and set the variables similar to the full backup

#!/bin/bash

BACKUP_DIR=“/mydirectory/backup”

ROTATE_DIR=“/mydirectory/backup/rotate”

TIMESTAMP=“timestamp.dat”

SOURCE=“$HOME/sourcedirectory ”

DATE=$(date +%Y-%m-%d-%H%M%S)

2.Exclude temporary files and files that are created with each system start

EXCLUDE=“--exclude=/mnt/*--exclude=/proc/*--exclude=/sys/*--exclude=/tmp/*”

3.Switch to the root directory to make sure that all paths are interpreted correctly

cd /

4.Create the backup directory

mkdir -p ${BACKUP_DIR}

set -- ${BACKUP_DIR}/backup-??.tar.gz

5.Determine the number of the last backup by removing the last parts of the file name

lastname=${!#}

backupnr=${lastname##*backup-}

backupnr=${backupnr%%.*}

backupnr=${backupnr//\?/0}

6.The code records only 30 incremental backups at a time. After the number of backups reaches 30, the script moves all archive files into the rotate folder. Then all files starting with the letters ‘b’ and ‘t’ are moved into the new folder. Finally, the script resets the backup number to 1.

backupnr=$[10#${backupnr}]

if [ “$[backupnr++]” -ge 30 ]; then

  mkdir -p ${ROTATE_DIR}/${DATE}

  mv ${BACKUP_DIR}/b* ${ROTATE_DIR}/${DATE}

  mv ${BACKUP_DIR}/t* ${ROTATE_DIR}/${DATE}

  backupnr=1

fi

backupnr=0${backupnr}

backupnr=${backupnr: -2}

7.The incremental backup is enabled by -g. This tar command reads the timestamp of each file, compares it with the existing data recorded in timestamp.dat, and then determines what changes have been made since the last backup. Only the changes are stored in the new archive.

filename=backup-${backupnr}.tar.gz

tar -cpzf ${BACKUP_DIR}/${filename} -g ${BACKUP_DIR}/${TIMESTAMP} -X $EXCLUDE ${SOURCE}

 

Conclusion

There are many Linux backup options out there. Some options that were not mentioned in the article include:

  • Differential backups that record every change made since the last execution of the full backup.
  • Network Backups use the client-server model to send data across the network to backup destinations.
  • File Transfer Protocol (FTP) backups that leverage the client-server architecture to make backups using FTP.

There are also many other Linux tools besides tar. For example, the dump utility backups file systems instead of individual files. Dump supports only full backups and incremental backups.

Some Linux admins prefer to choose an existing Linux System backup tool like tar. Others prefer an enterprise backup and disaster recovery solution that is also compatible with cloud environments. There is no bad or wrong backup strategy, just the strategy that fits you best.

Best compression software for Linux
----------------

Author Bio

Gilad David Maayan is a technology writer who has worked with over 150 technology companies including SAP, Samsung NEXT, NetApp and Imperva, producing technical and thought leadership content that elucidates technical solutions for developers and IT leadership.

LinkedIn: https://www.linkedin.com/in/giladdavidmaayan/

If you think we have helped you or just want to support us, please consider these:-

Connect to us: Facebook | Twitter

Linux TechLab is thankful for your continued support.