Table of Contents

Incremental backup

With this script, an incremental backup using hard links from a remote computer is created.

How it works

For each backup a subdirectory is created. If the script runns the first time, all data is copied from the specified directories.

If backups have already been created, the script copies only those data that have changed since the last backup. For all unchanged files, only hard links to the relevant file from the last backup will be created.

To save disk space, all directories older than X days will be deleted.

If the target partition can not be mounted during the backup, or an error occurs, an email is sent with an error messsage.

Benefits

Restrictions

The target file system must support hard links (eg: ext2 / 3, xfs, reiser, …).

Preparations

In order to access the remote computer without a password a private / public key authentication without a password is to be set up. For this type of authentication, a key pair must be created on the computer that performs the backup. The public key must be transferred and set up properly to all computers from which data will be copied during the backup process.

The procedure how this authentication can be established, is described in the article on secure shell (ssh).

Script

   #!/bin/bash
   # Copyright by Michael Mayer
 
   # This program is free software: you can redistribute it and / or modify
   # It under the terms of the GNU General Public License as published by
   # The Free Software Foundation, either version 3 of the License, or
   # (At your option) any later version.
 
   # This program is distributed in the hope that it will be useful,
   # But WITHOUT ANY WARRANTY; without even the implied warranty of
   # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   # GNU General Public License for more details.
 
   # You should have received a copy of the GNU General Public License
   # Along with this program. If not, see <http://www.gnu.org/licenses/>.
 
   ######################################################################
   # # Begin configuration section
   # #
   # Device to be mounted to store the backups
   BACKUP_DEV=/dev/sdX
   #
   # Destination directory in which the backups should be stored
   BACKUP_TO="</backup/directory>"
   #
   # Hostname/IP and directory from which the data should be fetched for the backup
   BACKUP_SRC="<user1>@<hostname1>:/dir1<user2>@<hostname2>:/dir2..."
   #
   # Beginning of the directory name for all backups
   PREFIX="backup_"
   #
   # Number of old backups that are to be retained
   KEEP_BACKUPS=14
   #
   # Email address is entered as the sender of a message
   FROM_ADRESS=<from-emailaddress>
   #
   # Email address to which error messagew should be sent
   TO_ADDRESS=<to-emailaddress>
   # #
   # # End configuration section
   ######################################################################
 
   TIMESTAMP=`date +% Y% m% d_% H% M% S`;
 
   mount $BACKUP_DEV $BACKUP_TO
   if [$? -ne 0]; then
      # send error message
      echo "$TIMESTAMP - Mounting of the backup-media FAILED" >> /var/log/backup
      echo "Error during backup. The partition $BACKUP_DEV could not be mounted." | \
          mail -s "Backup - Automatic error"- r $FROM_ADDRESS $TO_ADDRESS
      return
   else
     cd $BACKUP_TO
     DAY=`date "+%Y%m%d"`
     OLDEST=`ls-1t | grep backup | tail -1`
     NEWES=`ls-1tr | grep backup | tail -1`
     NUM=`ls -1 | grep backup | wc-l`
 
     # Check whether more than $KEEP_BACKUPS backups exist
     if [$NUM -ge $KEEP_BACKUPS]; then
       # Delete the oldest backup
       rm-r $OLDEST
     fi
 
     for BACKUP_FILES in $BACKUP_SRC
     do
       if [$NUM -gt 0]; then
         rsync -avcz --rsh=ssh --delete --link-dest=$BACKUP_TO$NEWEST $BACKUP_FILES $BACKUP_TO$PREFIX$DAY
       else
         rsync -avcz --rsh=ssh $BACKUP_FILES $BACKUP_TO$PREFIX$DAY
       fi
       if [$? -eq 0]; then
         echo "$ IMESTAMP - Backup of $BACKUP_FILES OK" >> /var/log/backup.log
       else
          echo "$TIMESTAMP - Backup of $BACKUP_FILES to $BACKUP_TO FAILED" >> /var/log/backup
          echo "This incremental backup did not work." \
              mail -s "Backup - Automatic error" -r $FROM_ADDRESS  $TO_ADDRESS
       fi
     done
 
     # Create a link to the newly created directory
     rm last_backup
     ln -s $ BACKUP_TO$PREFIX$DAY last_backup
 
   fi
 
   cd /
   umount $BACKUP_TO

See also