Linux Backup

How to recover the system after a failure with a Linux backup

A system failure is a matter of time, so better prevent. A backup is an historical version of your data that you can restore in case of need

Types of backup

  • full a backup of the whole data set
  • incremental backup only changes since the last backup

Periodic

  • Daily
  • Weekly
  • Monthly


Backup using a shell script

#!/bin/sh
####################################
#
# Backup to NFS mount script.
#
####################################
# What to backup.
backup_files="/home /var/spool/mail /etc /root /boot /opt"
# Where to backup to.
dest="/mnt/backup"
# Create archive filename.
day=$(date +%A)
hostname=$(hostname -s)
archive_file="$hostname-$day.tgz"
# Print start status message.
echo "Backing up $backup_files to $dest/$archive_file"
date
echo
# Backup the files using tar.
tar czf $dest/$archive_file $backup_files
# Print end status message.
echo
echo "Backup finished"
date
# Long listing of files in $dest to check file sizes.
ls -lh $dest

This script rotates through 7 backups - one for each day.
A modification can allow for rotation  of daily, weekly, and monthly backups.

Automating backup with cron

Cron is used to schedule the execution of scripts.
To run the backup script every day of every month of every year, at midnight

# m h dom mon dow command
0 0 * * * bash /usr/local/bin/backup.sh


To enter the cron job editor crontab -e or if you prefer vi just type

vi /etc/crontab

 

Restoring the backup


Use tar to test the integrity of an archive, or to extract its contents.

To list the contents of the archive

tar -tzvf /mnt/backup/host-Monday.tgz 


To extract a file from the archive

tar -xzvf /mnt/backup/host-Monday.tgz -C /tmp etc/hosts


To extract the full contents of the archive

tar -xzvf /mnt/backup/host-Monday.tgz


Or you may use an open source tool  like http://www.bacula.org