Performing A Linux System Backup The Right Way

laptop performing backup

There are numerous reasons why a system backup needs to be done. One such reason that has gained popularity since 2010 is defending against ransomware. For Linux users, this was not an issue as the majority of strains targeted Windows. Increasingly, Linux and Linux servers are coming under attack. Whether it’s QNAPCrypt or a new variant of Ryuk with the potential to target Unix-based operating systems backups can help prevent lasting damage caused by an infection.

Backups also fill more mundane roles in helping prevent data loss during system upgrades that may take a nasty turn. The above are only two important reasons why backups need to be done but the how is yet to be answered.

Types of System Backup

There are several great linux backup solutions that have been brought to the market over the years to make the process easier. To go through each would take far longer than any reader would want to dedicate time to, for that reason the article will deal with tar, which stands for Tap Archiver, backups and the types of backups that can be implemented using tar.

The first type of backup is known as a full system backup. A full system backup creates an entire copy of all the data currently found on the system. This requires large storage space and these can take time to complete. However, restoring data from a full system backup is relatively fast. It also allows for better storage management as all the backup data is stored in one single file. Disadvantages include redundant backups of certain files that hardly ever change and security issues in that the file can be stolen and now all the data stored in the single file is compromised.

The second type of backup strategy is known as incremental backups. This requires a full backup to have already been completed, but rather than doing a full backup every time, certain blocks of data are backed up if they have been modified since the last backup. This method has the advantage of using less storage space and faster backup times but does have the disadvantage of taking longer to restore data if the need arises.

Full System Backup with Tar

In order to conduct a full system backup, you need to first create a directory called bin. The second step involves informing Linux that bash will be used as the interpreter with the following command.

#!/bin/bash

The third step is to define the tar stamp in your backup so as to separate backups from each other.

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

The fourth step involves specifying which directory you will store the backup.

BACKUP_DIR=”/mydirectory/backup”

The fifth step is to specify which directories you want to backup.

SOURCE=”$HOME/sourcedirectory1″

Lastly, input the following tar command to backup data.

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

Incremental Backup with Tar

The following list of commands will create and store backups in dated folders pertaining to the date of the creation of the backup. To do this users require cron as well as tar. Cron is typically used to create time-based execution processes.

The first step is to set bash as the interpreter and set variables similar to those used in a full backup scenario.

#!/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)

The second step is to exclude temporary files and files created when the system starts.

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

The third step is to switch to the root directory in order to ensure all paths are correctly interpreted.

cd /

The fourth step is to create the backup directory.

mkdir -p ${BACKUP_DIR}

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

The fifth step is to determine the number associated with the last backup and then remove the last parts of the file name.

lastname=${!#}

backupnr=${lastname##*backup-}

backupnr=${backupnr%%.*}

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

The sixth step performs the function of renaming the current backup to one as the above code can only record up to 30 different backups.

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}

Lastly, the following command will read the timestamp of each file to determine if any changes were made and then only store the new changes if made.

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

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

Conclusion

In practice, Linux admins will attempt to save space by backing up small sets of data incrementally. If the admin is responsible for a large set of data full backups will be performed occasionally while combining the full backup with other types of backup before the next full backup is to be scheduled.

(Visited 279 times, 1 visits today)
Patricia Rounsaville
I have been working as a professional writer for more than three years and particularly enjoy writing about Mac and Apple products of all kinds and mobile technology. I am also a bit of a nerd at heart and love all things tech related.