#!/bin/sh

#
# Distributed under the terms of the GNU General Public License v2
#
# Vincent Touchard (vincent - dot - touchard - at - enst-bretagne - dot - fr)
# 2007/04/01 - 23h45 CET
#
# Script mainly using rdiff-backup 1.1.x for doing my backups
#

#
# Configuration:
# needs backup.conf and default.inc
#
# backup.conf:
# list of clients (one per line) to backup using the following syntax
# "client_name client_address ssh_port OS remove_old_than"
# Comments are accepted
# If address is set to "localhost" then the script won't use SSH to connect to the client
# OS supported are:
#  - debian: OS that use dpkg
#  - gentoo: looks at /var/lib/portage/world
#  - RHEL
#  - fedora
#  - centos
#  - suse
#  - OS using rpm are supported using the four previous keywords
# backups older than "remove_older_than" will be removed, see man rdiff-backup for the syntax
#
# default.inc:
# default directories to backup, one per line
#
# $client_name.inc:
# If present, overides default.inc for the client $client_name
# One directory per line
#

#
# Backups will be stored in $BACKUP_PART/rdiff-backup/$client_name
# the list of installed packages will be in $BACKUP_PART/packages/$client_name
#

CONF_FILE="backup.conf"
DEFAULT_INC="default.inc"
LOCK_FILE="backup"
BACKUP_PART="/srv/backup"

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

MOUNT="/bin/mount"
UMOUNT="/bin/umount"
SSH="/usr/bin/ssh -C"

RDIFF_BACKUP="/usr/bin/rdiff-backup"

# Lock file
lockfile-create $LOCK_FILE || {
    echo "Cannot get a lock, script already running, exiting ...."
    exit 1
}
lockfile-touch $LOCK_FILE &
# Save the PID of the lockfile-touch process
LOCK_PID="$!"

$MOUNT $BACKUP_PART

[ -d "$BACKUP_PART/rdiff-backup" ] && {

    # Parse the configuration
    cat $CONF_FILE | grep -v -e "^\s*#" | grep -v -e "^\s*$" | while read computer address port os age; do
        echo "Backup of $computer ..."

        # Which folders to include
        inc_file=$DEFAULT_INC
        [ -f "$computer.inc" ] && inc_file="$computer.inc"

        # Calling rdiff-backup
        [ $address = "localhost" ] && {
            $RDIFF_BACKUP --remote-schema '%s' --include-globbing-filelist $inc_file --exclude '*' 'nice sudo /usr/bin/rdiff-backup --server --restrict-read-only /'::/ "$BACKUP_PART/rdiff-backup/$computer"
        } || {
            $RDIFF_BACKUP --remote-schema 'ssh -C %s nice sudo /usr/bin/rdiff-backup --server --restrict-read-only /' --include-globbing-filelist $inc_file --exclude '*' "$address -p $port"::/ "$BACKUP_PART/rdiff-backup/$computer"
        }

        echo "Removing backups older than $age ..."
        $RDIFF_BACKUP --remove-older-than $age "$BACKUP_PART/rdiff-backup/$computer"

        # backup list of installed packages
        mkdir -p "$BACKUP_PART/packages/$computer"
        case $os in
            "debian")
                # background ssh to avoid the while loop exiting prematurely
                $SSH -f $address -p $port "dpkg --get-selections" > "$BACKUP_PART/packages/$computer/$DATE";;
            "gentoo")
                # background ssh to avoid the while loop exiting prematurely
                $SSH -f $address -p $port "sudo cat /var/lib/portage/world" > "$BACKUP_PART/packages/$computer/$DATE";;
            "RHEL"|"fedora"|"suse"|"centos")
                # background ssh to avoid the while loop exiting prematurely
                $SSH -f $address -p $port "rpm -qa" > "$BACKUP_PART/packages/$computer/$DATE";;
        esac

    done
}

# To know which amount of space is used/remains
df -h | grep $BACKUP_PART
$UMOUNT $BACKUP_PART

# Unlock
kill "${LOCK_PID}"
lockfile-remove $LOCK_FILE