WebSVN - scripts - Rev 14 - /bash/trunk/v1.0/backup-home.sh

thorko Repositories scripts

Rev

Rev 2 | Blame | Compare with Previous | Last modification | View Log | RSS feed

#!/bin/bash
# this script creates backups by using rsync
# and hard links to preserve disk space
################################
# 2011-07-09 thorko
###############################

# config variables
KEEP=7                          # keep backup in days
SOURCES=/opt/backup/sources.conf                # list of directories to bakup
EXCLUDES=/opt/backup/excludes.conf              # list of directories or files to exclude
USER=youruser                   # remote user e.g. root
HOST=yourhost                   # remote host e.g. backup.de
PORT=22                         # remote port
KEY=/root/.ssh/id_dsa           # your private ssh key
DEST=/path/to/backup/location/on/remote/host    # remote backup location
LOG=/var/log/backup.log
LOCK=/tmp/backup.lock
################################

TODAY=$(date +'%Y%m%d')
LOGIN="ssh -i $KEY -p $PORT $USER@$HOST"

# locking
if [ -e $LOCK ]; then
        echo "Another instance running. Exit" >> $LOG 2>&1
        exit 0
fi
touch $LOCK

# check if remote host is reachable
ping -c 1 $HOST > /dev/null 2>&1
if [ $? -ne 0 ]; then
        echo "no network connectivity" >> $LOG 2>&1
        exit 1
fi

# rotate directories on remote system
echo "if [ ! -d ${DEST}/backup.${TODAY} ]; then
        /bin/cp -Rpl ${DEST}/\$(ls -1 ${DEST} | tail -1) ${DEST}/backup.${TODAY}
fi

if [ \$(ls -1 $DEST/ | wc -l) -gt $KEEP ]; then
        ls -1 -r --color=never $DEST/ | tail -n 1 | xargs rm -rf
fi"
> /tmp/cphome.sh

cat /tmp/cphome.sh | $LOGIN

/usr/bin/rsync -LpDtgHrz -e "ssh -i $KEY -p $PORT" --log-file=$LOG --delete --exclude-from=$EXCLUDES --files-from=$SOURCES / $USER@$HOST:$DEST/backup.$TODAY

rm -f $LOCK