#!/bin/bash # Script for individual Backups with rsync and hardlinks # Copyright (C) 2011 Marcel Jäpel (Firewire2002) # # 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 # 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 . ##################################################################################################################### Config SSH_HOST="10.0.0.1" SSH_PORT=22 SSH_PATH=/backup BACKUP_PREFIX=My_Hostname BACKUP_SOURCE=/ BACKUP_COUNTS=7 BACKUP_PARAMETER="-az --stats --delete-before --delete-excluded --partial --devices --specials --numeric-ids --hard-links --bwlimit=1024" BACKUP_EXCLUDE="--exclude /backup/ \ --exclude /mnt/backup/ \ --exclude /proc/ \ --exclude /sys/ \ --exclude /var/lib/vz/root/ \ --exclude /var/lib/vz/template/" ##################################################################################################################### Self PID=$$ SCRIPTNAME=$(basename $0) ##################################################################################################################### Search for last backup logger -p info -t ${SCRIPTNAME}[${PID}] Getting Last Backup ... DATE=$(date +%Y%m%d_%H%M%S) LAST_BACKUP=$(ssh -p ${SSH_PORT} ${SSH_HOST} "/bin/ls -1 ${SSH_PATH} 2>/dev/null | tail -n1") if [ "${LAST_BACKUP}" != "" ]; then LAST_BACKUP="--link-dest=${SSH_PATH}/${LAST_BACKUP}" fi ##################################################################################################################### Backup job logger -p info -t ${SCRIPTNAME}[${PID}] Starting Backup ... rsync ${BACKUP_PARAMETER} ${BACKUP_EXCLUDE} -e "ssh -p ${SSH_PORT}" $LAST_BACKUP ${BACKUP_SOURCE} ${SSH_HOST}:${SSH_PATH}/${BACKUP_PREFIX}_${DATE} 2>&1 | grep -v "file has vanished: " | while read LINE; do if [ "x$LINE" != "x" ]; then logger -p info -t ${SCRIPTNAME}[${PID}] ${LINE} fi done ##################################################################################################################### Delete old backups logger -p info -t ${SCRIPTNAME}[${PID}] Removing old Backups ... COUNTER=0 for DIR in $(ssh -p ${SSH_PORT} ${SSH_HOST} "/bin/ls -1 -r ${SSH_PATH}/ | grep ${BACKUP_PREFIX}") do let counter=$counter+1 if [ $counter -gt "${BACKUP_COUNTS}" ]; then ssh -p ${SSH_PORT} ${SSH_HOST} "rm -rf ${SSH_PATH}/${DIR}" fi done ##################################################################################################################### Finish exit 0