Return to Snippet

Revision: 31382
at September 17, 2010 22:32 by eusonlito


Updated Code
#!/bin/sh

# -------------------------------------
# Script to do incremental rsync backups
# from a central backup server
#
# 17/08/2010 - A Navalla Suiza <[email protected]> 
#
# http://snipplr.com/view/39964/backup-servers-from-a-central-backup-server/
#
# This script is freely distributed under the GPL
# -------------------------------------


# -------------------------------------
# Configuration
#
# You only should touch this section
# -------------------------------------

# email address to send the error mails
MAILADDR="[email protected]"

# user to login into servers (recommended root)
USER="root"

# root directory to for backup stuff
BASEBACKUP="/backup"

# folder name to full copy
FULLNAME="full"

# folder name to incremental changes
INCREMENTALNAME="incremental"

# days to store the incremental changes
INCREMENTALTIME=7

# host list to backup
HOSTS="myserver1.com myserver2.com myserver3.com"

# log directory
LOGS=`dirname "$(readlink -f "$0")"`"/logs"

# Size max in bytes to log files. After that size, log will be rotated and compressed
LOGSIZE=10000

# Search commands in...
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11"

# -------------------------------------
# Functions definition
# -------------------------------------

echo_log () {
	if [ "$CMD_ECHO" != '' ]; then
		$CMD_ECHO "$1" >> $LOG
	fi
}

echo_start () {
	echo_log ''
	echo_log '----------------------------------------------------'
	echo_log "[$1] Start at: `$CMD_DATE "+%Y-%m-%d %H:%M:%S"`"
	echo_log '----------------------------------------------------'
}

echo_end () {
	echo_log ''
	echo_log '----------------------------------------------------'
	echo_log "[$1] End at: `$CMD_DATE "+%Y-%m-%d %H:%M:%S"`"
	echo_log '----------------------------------------------------'
}

do_rsync () {
	$CMD_KILLALL $CMD_RSYNC > /dev/null 2> /dev/null

	OPTIONS="--force --ignore-errors --delete --backup --backup-dir="$INCREMENTDIR/" --exclude /proc/ --exclude /dev/ --exclude /tmp/ --exclude /sys/ -e $CMD_SSH -avpz $USER@$HOST:/ $BASECOPY"

	echo_log ''
	echo_log '----------------------------------------------------'
	echo_log "$CMD_RSYNC $OPTIONS"
	echo_log '----------------------------------------------------'
	echo_log ''

	$CMD_RSYNC $OPTIONS >> $LOG 2>> $LOG

	echo_log ''
	echo_log '----------------------------------------------------'
	echo_log "$CMD_FIND $BASEINCREMENT -maxdepth 1 -type d -mtime +$INCREMENTALTIME -exec $CMD_RM -rf {} \;"
	echo_log '----------------------------------------------------'
	echo_log ''

	$CMD_FIND $BASEINCREMENT -maxdepth 1 -type d -mtime +$INCREMENTALTIME -exec $CMD_RM -rf {} \; >> $LOG 2>> $LOG
}

do_mail_error () {
	echo_log ''
	echo_log "[ERROR] Backup problem for $HOST"
	echo_log ''
	echo_log $1

	{ $CMD_ECHO 'Subject: [ERROR] Backup problem for '.$HOST; $CMD_ECHO $1; } | $CMD_SENDMAIL $MAILADDR
}


# -------------------------------------
# Script execution
# -------------------------------------

LOG="$LOGS/error.log"

COMMANDS="echo date sendmail awk bzip2 du find install killall mv rm rsync ssh"

for i in $COMMANDS; do
	exists=`which $i`

	if [ "$exists" = "" ]; then
		echo_log ''
		echo_log "Command $i does not exists in $PATH. Backup will be cancelled."

		if [ "$CMD_SENDMAIL" != "" ]; then
			do_mail_error "Command $i does not exists. Backup will be cancelled"
		fi

		echo_end

		exit 1
	fi

	eval "CMD_"`echo $i | tr "[:lower:]" "[:upper:]"`"='$exists'"
done

if [ ! -d $BASEBACKUP ]; then
	echo_log ''
	echo_log "Backup folder ($BASEBACKUP) does not exists"

	do_mail_error "Backup folder ($BASEBACKUP) does not exists"

	echo_end

	exit 1
fi

if [ ! -d $LOGS ]; then
	echo_log ''
	echo_log "Logs folder ($LOGS) does not exists"

	do_mail_error "Logs folder ($LOGS) does not exists"

	echo_end

	exit 1
fi

for HOST in $HOSTS; do
	LOG="$LOGS/$HOST.log"

	if [ -f "$LOG" ]; then
		if [ $LOGSIZE -lt "`$CMD_DU -s $LOG | $CMD_AWK -F' ' '{print $1}'`" ]; then
			OLD_LOG=$LOG.`$CMD_DATE "+%Y%m%d-%H%M%S"`

			$CMD_MV $LOG $OLD_LOG

			$CMD_BZIP2 -9 $OLD_LOG
		fi
	fi

	echo_start $HOST

	if [ "$HOST" = '' ]; then
		echo_log ''
		echo_log "No HOST is defined at first parameter"

		do_mail_error "No HOST is defined at first parameter"

		echo_end $HOST

		exit 1
	fi

	test=`$CMD_SSH -q -q $USER@$HOST $CMD_ECHO 1 || $CMD_ECHO 2`

	if [ "$test" = '2' ]; then
		echo_log ''
		echo_log "Can not connect to $HOST server"

		do_mail_error "Can not connect to $HOST server"

		echo_end $HOST

		exit 1
	fi

	ARCHIVEROOT="$BASEBACKUP/$HOST"
	BASECOPY="$ARCHIVEROOT/$FULLNAME"
	BASEINCREMENT="$ARCHIVEROOT/$INCREMENTALNAME"
	INCREMENTDIR="$BASEINCREMENT/"`$CMD_DATE "+%Y-%m-%d"`

	if [ ! -d $BASECOPY ]; then
		$CMD_INSTALL -d "$BASECOPY"
	fi

	if [ ! -d $BASEINCREMENT ]; then
		$CMD_INSTALL -d "$BASEINCREMENT"
	fi

	do_rsync

	echo_end $HOST
done

Revision: 31381
at September 17, 2010 21:30 by eusonlito


Updated Code
#!/bin/sh

# -------------------------------------
# Script to do incremental rsync backups
# from a central backup server
#
# 17/08/2010 - A Navalla Suiza <[email protected]> 
#
# http://snipplr.com/view/39964/backup-servers-from-a-central-backup-server/
#
# This script is freely distributed under the GPL
# -------------------------------------


# -------------------------------------
# Configuration
#
# You only should touch this section
# -------------------------------------

# email address to send the error mails
MAILADDR="[email protected]"

# user to login into servers (recommended root)
USER="root"

# root directory to for backup stuff
BASEBACKUP="/backup"

# folder name to full copy
FULLNAME="full"

# folder name to incremental changes
INCREMENTALNAME="incremental"

# days to store the incremental changes
INCREMENTALTIME=7

# host list to backup
HOSTS="myserver1.com myserver2.com myserver3.com"

# log directory
LOGS=`dirname "$(readlink -f "$0")"`"/logs"

# Size max in bytes to log files. After that size, log will be rotated and compressed
LOGSIZE=10000

# Search commands in...
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11"

export $PATH

# -------------------------------------
# Functions definition
# -------------------------------------

echo_log () {
	if [ "$CMD_ECHO" != '' ]; then
		$CMD_ECHO "$1" >> $LOG
	fi
}

echo_start () {
	echo_log ''
	echo_log '----------------------------------------------------'
	echo_log "[$1] Start at: `$CMD_DATE "+%Y-%m-%d %H:%M:%S"`"
	echo_log '----------------------------------------------------'
}

echo_end () {
	echo_log ''
	echo_log '----------------------------------------------------'
	echo_log "[$1] End at: `$CMD_DATE "+%Y-%m-%d %H:%M:%S"`"
	echo_log '----------------------------------------------------'
}

do_rsync () {
	$CMD_KILLALL $CMD_RSYNC > /dev/null 2> /dev/null

	OPTIONS="--force --ignore-errors --delete --backup --backup-dir="$INCREMENTDIR/" --exclude /proc/ --exclude /dev/ --exclude /tmp/ --exclude /sys/ -e $CMD_SSH -avpz $USER@$HOST:/ $BASECOPY"

	echo_log ''
	echo_log '----------------------------------------------------'
	echo_log "$CMD_RSYNC $OPTIONS"
	echo_log '----------------------------------------------------'
	echo_log ''

	$CMD_RSYNC $OPTIONS >> $LOG 2>> $LOG

	echo_log ''
	echo_log '----------------------------------------------------'
	echo_log "$CMD_FIND $BASEINCREMENT -maxdepth 1 -type d -mtime +$INCREMENTALTIME -exec $CMD_RM -rf {} \;"
	echo_log '----------------------------------------------------'
	echo_log ''

	$CMD_FIND $BASEINCREMENT -maxdepth 1 -type d -mtime +$INCREMENTALTIME -exec $CMD_RM -rf {} \; >> $LOG 2>> $LOG
}

do_mail_error () {
	echo_log ''
	echo_log "[ERROR] Backup problem for $HOST"
	echo_log ''
	echo_log $1

	{ $CMD_ECHO 'Subject: [ERROR] Backup problem for '.$HOST; $CMD_ECHO $1; } | $CMD_SENDMAIL $MAILADDR
}


# -------------------------------------
# Script execution
# -------------------------------------

LOG="$LOGS/error.log"

COMMANDS="echo date sendmail awk bzip2 du find install killall mv rm rsync ssh"

for i in $COMMANDS; do
	exists=`which $i`

	if [ "$exists" = "" ]; then
		echo_log ''
		echo_log "Command $i does not exists in $PATH. Backup will be cancelled."

		if [ "$CMD_SENDMAIL" != "" ]; then
			do_mail_error "Command $i does not exists. Backup will be cancelled"
		fi

		echo_end

		exit 1
	fi

	eval "CMD_"`echo $i | tr "[:lower:]" "[:upper:]"`"='$exists'"
done

if [ ! -d $BASEBACKUP ]; then
	echo_log ''
	echo_log "Backup folder ($BASEBACKUP) does not exists"

	do_mail_error "Backup folder ($BASEBACKUP) does not exists"

	echo_end

	exit 1
fi

if [ ! -d $LOGS ]; then
	echo_log ''
	echo_log "Logs folder ($LOGS) does not exists"

	do_mail_error "Logs folder ($LOGS) does not exists"

	echo_end

	exit 1
fi

for HOST in $HOSTS; do
	LOG="$LOGS/$HOST.log"

	if [ -f "$LOG" ]; then
		if [ $LOGSIZE -lt "`$CMD_DU -s $LOG | $CMD_AWK -F' ' '{print $1}'`" ]; then
			OLD_LOG=$LOG.`$CMD_DATE "+%Y%m%d-%H%M%S"`

			$CMD_MV $LOG $OLD_LOG

			$CMD_BZIP2 -9 $OLD_LOG
		fi
	fi

	echo_start $HOST

	if [ "$HOST" = '' ]; then
		echo_log ''
		echo_log "No HOST is defined at first parameter"

		do_mail_error "No HOST is defined at first parameter"

		echo_end $HOST

		exit 1
	fi

	test=`$CMD_SSH -q -q $USER@$HOST $CMD_ECHO 1 || $CMD_ECHO 2`

	if [ "$test" = '2' ]; then
		echo_log ''
		echo_log "Can not connect to $HOST server"

		do_mail_error "Can not connect to $HOST server"

		echo_end $HOST

		exit 1
	fi

	ARCHIVEROOT="$BASEBACKUP/$HOST"
	BASECOPY="$ARCHIVEROOT/$FULLNAME"
	BASEINCREMENT="$ARCHIVEROOT/$INCREMENTALNAME"
	INCREMENTDIR="$BASEINCREMENT/"`$CMD_DATE "+%Y-%m-%d"`

	if [ ! -d $BASECOPY ]; then
		$CMD_INSTALL -d "$BASECOPY"
	fi

	if [ ! -d $BASEINCREMENT ]; then
		$CMD_INSTALL -d "$BASEINCREMENT"
	fi

	do_rsync

	echo_end $HOST
done

Revision: 31380
at September 17, 2010 21:27 by eusonlito


Updated Code
#!/bin/sh

# -------------------------------------
# Script to do incremental rsync backups
# from a central backup server
#
# 17/08/2010 - A Navalla Suiza <[email protected]> 
#
# http://snipplr.com/view/39964/backup-servers-from-a-central-backup-server/
#
# This script is freely distributed under the GPL
# -------------------------------------


# -------------------------------------
# Configuration
#
# You only should touch this section
# -------------------------------------

# email address to send the error mails
MAILADDR="[email protected]"

# user to login into servers (recommended root)
USER="root"

# root directory to for backup stuff
BASEBACKUP="/backup"

# folder name to full copy
FULLNAME="full"

# folder name to incremental changes
INCREMENTALNAME="incremental"

# days to store the incremental changes
INCREMENTALTIME=7

# host list to backup
HOSTS="myserver1.com myserver2.com myserver3.com"

# log directory
LOGS=`dirname "$(readlink -f "$0")"`"/logs"

# Size max in bytes to log files. After that size, log will be rotated and compressed
LOGSIZE=10000

# Search commands in...
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11"

export $PATH

# -------------------------------------
# Functions definition
# -------------------------------------

echo_log () {
	if [ "$CMD_ECHO" != '' ]; then
		$CMD_ECHO "$1" >> $LOG
	fi
}

echo_start () {
	echo_log ''
	echo_log '----------------------------------------------------'
	echo_log "[$1] Start at: `$CMD_DATE "+%Y-%m-%d %H:%M:%S"`"
	echo_log '----------------------------------------------------'
}

echo_end () {
	echo_log ''
	echo_log '----------------------------------------------------'
	echo_log "[$1] End at: `$CMD_DATE "+%Y-%m-%d %H:%M:%S"`"
	echo_log '----------------------------------------------------'
}

do_rsync () {
	$CMD_KILLALL rsync > /dev/null 2> /dev/null

	OPTIONS="--force --ignore-errors --delete --backup --backup-dir="$INCREMENTDIR/" --exclude /proc/ --exclude /dev/ --exclude /tmp/ --exclude /sys/ -e $CMD_SSH -avpz $USER@$HOST:/ $BASECOPY"

	echo_log ''
	echo_log '----------------------------------------------------'
	echo_log "$CMD_RSYNC $OPTIONS"
	echo_log '----------------------------------------------------'
	echo_log ''

	$CMD_RSYNC $OPTIONS >> $LOG 2>> $LOG

	echo_log ''
	echo_log '----------------------------------------------------'
	echo_log "$CMD_FIND $BASEINCREMENT -maxdepth 1 -type d -mtime +$INCREMENTALTIME -exec $CMD_RM -rf {} \;"
	echo_log '----------------------------------------------------'
	echo_log ''

	$CMD_FIND $BASEINCREMENT -maxdepth 1 -type d -mtime +$INCREMENTALTIME -exec $CMD_RM -rf {} \; >> $LOG 2>> $LOG
}

do_mail_error () {
	echo_log ''
	echo_log "[ERROR] Backup problem for $HOST"
	echo_log ''
	echo_log $1

	{ $CMD_ECHO 'Subject: [ERROR] Backup problem for '.$HOST; $CMD_ECHO $1; } | $CMD_SENDMAIL $MAILADDR
}


# -------------------------------------
# Script execution
# -------------------------------------

LOG="$LOGS/error.log"

COMMANDS="echo date sendmail awk bzip2 du find install killall mv rm rsync ssh"

for i in $COMMANDS; do
	exists=`which $i`

	if [ "$exists" = "" ]; then
		echo_log ''
		echo_log "Command $i does not exists in $PATH. Backup will be cancelled."

		if [ "$CMD_SENDMAIL" != "" ]; then
			do_mail_error "Command $i does not exists. Backup will be cancelled"
		fi

		echo_end

		exit 1
	fi

	eval "CMD_"`echo $i | tr "[:lower:]" "[:upper:]"`"='$exists'"
done

if [ ! -d $BASEBACKUP ]; then
	echo_log ''
	echo_log "Backup folder ($BASEBACKUP) does not exists"

	do_mail_error "Backup folder ($BASEBACKUP) does not exists"

	echo_end

	exit 1
fi

if [ ! -d $LOGS ]; then
	echo_log ''
	echo_log "Logs folder ($LOGS) does not exists"

	do_mail_error "Logs folder ($LOGS) does not exists"

	echo_end

	exit 1
fi

for HOST in $HOSTS; do
	LOG="$LOGS/$HOST.log"

	if [ -f "$LOG" ]; then
		if [ $LOGSIZE -lt "`$CMD_DU -s $LOG | $CMD_AWK -F' ' '{print $1}'`" ]; then
			OLD_LOG=$LOG.`$CMD_DATE "+%Y%m%d-%H%M%S"`

			$CMD_MV $LOG $OLD_LOG

			$CMD_BZIP2 -9 $OLD_LOG
		fi
	fi

	echo_start $HOST

	if [ "$HOST" = '' ]; then
		echo_log ''
		echo_log "No HOST is defined at first parameter"

		do_mail_error "No HOST is defined at first parameter"

		echo_end $HOST

		exit 1
	fi

	test=`$CMD_SSH -q -q $USER@$HOST $CMD_ECHO 1 || $CMD_ECHO 2`

	if [ "$test" = '2' ]; then
		echo_log ''
		echo_log "Can not connect to $HOST server"

		do_mail_error "Can not connect to $HOST server"

		echo_end $HOST

		exit 1
	fi

	ARCHIVEROOT="$BASEBACKUP/$HOST"
	BASECOPY="$ARCHIVEROOT/$FULLNAME"
	BASEINCREMENT="$ARCHIVEROOT/$INCREMENTALNAME"
	INCREMENTDIR="$BASEINCREMENT/"`$CMD_DATE "+%Y-%m-%d"`

	if [ ! -d $BASECOPY ]; then
		$CMD_INSTALL -d "$BASECOPY"
	fi

	if [ ! -d $BASEINCREMENT ]; then
		$CMD_INSTALL -d "$BASEINCREMENT"
	fi

	do_rsync

	echo_end $HOST
done

Revision: 31379
at September 9, 2010 19:05 by eusonlito


Updated Code
#!/bin/sh

# -------------------------------------
# Script to do incremental rsync backups
# from a central backup server
#
# 08/03/2010 - A Navalla Suiza <[email protected]> 
#
# http://snipplr.com/view/39964/backup-servers-from-a-central-backup-server/
#
# This script is freely distributed under the GPL
# -------------------------------------


# -------------------------------------
# Configuration
#
# You only should touch this section
# -------------------------------------

# email address to send the error mails
MAILADDR="[email protected]"

# user to login into servers (recommended root)
USER="root"

# root directory to for backup stuff
BASEBACKUP="/backup"

# folder name to full copy
FULLNAME="full"

# folder name to incremental changes
INCREMENTALNAME="incremental"

# days to store the incremental changes
INCREMENTALTIME=7

# host list to backup
HOSTS="myserver1.com myserver2.com myserver3.com"

# log directory
LOGS=`dirname "$(readlink -f "$0")"`"/logs"

# Size max in bytes to log files. After that size, log will be rotated and compressed
LOGSIZE=10000

# -------------------------------------
# Functions definition
# -------------------------------------

echo_log () {
	$CMD_ECHO $1 >> $LOG
}

echo_start () {
	echo_log ''
	echo_log '----------------------------------------------------'
	echo_log "Start at: `$CMD_DATE "+%Y-%m-%d %H:%M:%S"`"
	echo_log '----------------------------------------------------'
}

echo_end () {
	echo_log ''
	echo_log '----------------------------------------------------'
	echo_log "End at: `$CMD_DATE "+%Y-%m-%d %H:%M:%S"`"
	echo_log '----------------------------------------------------'
}

do_rsync () {
	$CMD_KILLALL rsync > /dev/null 2> /dev/null

	OPTIONS="--force --ignore-errors --delete --backup --backup-dir="$INCREMENTDIR/" --exclude /proc/ --exclude /dev/ --exclude /tmp/ --exclude /sys/ -e $CMD_SSH -avpz $USER@$HOST:/ $BASECOPY"

	echo_log ''
	echo_log '----------------------------------------------------'
	echo_log "$CMD_RSYNC $OPTIONS"
	echo_log '----------------------------------------------------'
	echo_log ''

	$CMD_RSYNC $OPTIONS >> $LOG 2>> $LOG

	echo_log ''
	echo_log '----------------------------------------------------'
	echo_log "$CMD_FIND $BASEINCREMENT -maxdepth 1 -type d -mtime +$INCREMENTALTIME -exec $CMD_RM -rf {} \;"
	echo_log '----------------------------------------------------'
	echo_log ''

	$CMD_FIND $BASEINCREMENT -maxdepth 1 -type d -mtime +$INCREMENTALTIME -exec $CMD_RM -rf {} \; >> $LOG 2>> $LOG
}

do_mail_error () {
	echo_log ''
	echo_log "[ERROR] Backup problem for $HOST"
	echo_log ''
	echo_log $1

	{ $CMD_ECHO 'Subject: [ERROR] Backup problem for '.$HOST; $CMD_ECHO $1; } | $CMD_SENDMAIL $MAILADDR
}


# -------------------------------------
# Script execution
# -------------------------------------

LOG="$LOGS/error.log"

COMMANDS="echo sendmail awk bzip2 date du find install killall mv rm rsync ssh"

for i in $COMMANDS; do
	exists=`which $i`

	if [ "$exists" = "" ]; then
		echo_log ''
		echo_log "Command $i does not exists. Backup will be cancelled"

		if [ "$CMD_SENDMAIL" != "" ]; then
			do_mail_error "Command $i does not exists. Backup will be cancelled"
		fi

		echo_end

		exit 1
	fi

	eval "CMD_"`echo $i | tr "[:lower:]" "[:upper:]"`"='$exists'"
done

if [ ! -d $BASEBACKUP ]; then
	echo_log ''
	echo_log "Backup folder ($BASEBACKUP) does not exists"

	do_mail_error "Backup folder ($BASEBACKUP) does not exists"

	echo_end

	exit 1
fi

if [ ! -d $LOGS ]; then
	echo_log ''
	echo_log "Logs folder ($LOGS) does not exists"

	do_mail_error "Logs folder ($LOGS) does not exists"

	echo_end

	exit 1
fi

for HOST in $HOSTS; do
	LOG="$LOGS/$HOST.log"

	if [ -f "$LOG" ]; then
		if [ $LOGSIZE -lt "`$CMD_DU -s $LOG | $CMD_AWK -F' ' '{print $1}'`" ]; then
			OLD_LOG=$LOG.`$CMD_DATE "+%Y%m%d-%H%M%S"`

			$CMD_MV $LOG $OLD_LOG

			$CMD_BZIP2 -9 $OLD_LOG
		fi
	fi

	echo_start

	if [ "$HOST" = '' ]; then
		echo_log ''
		echo_log "No HOST is defined at first parameter"

		do_mail_error "No HOST is defined at first parameter"

		echo_end

		exit 1
	fi

	test=`$CMD_SSH -q -q $USER@$HOST $CMD_ECHO 1 || $CMD_ECHO 2`

	if [ "$test" = '2' ]; then
		echo_log ''
		echo_log "Can not connect to $HOST server"

		do_mail_error "Can not connect to $HOST server"

		echo_end

		exit 1
	fi

	ARCHIVEROOT="$BASEBACKUP/$HOST"
	BASECOPY="$ARCHIVEROOT/$FULLNAME"
	BASEINCREMENT="$ARCHIVEROOT/$INCREMENTALNAME"
	INCREMENTDIR="$BASEINCREMENT/"`$CMD_DATE "+%Y-%m-%d"`

	if [ ! -d $BASECOPY ]; then
		$CMD_INSTALL -d "$BASECOPY"
	fi

	if [ ! -d $BASEINCREMENT ]; then
		$CMD_INSTALL -d "$BASEINCREMENT"
	fi

	do_rsync

	echo_end
done

Revision: 31378
at September 8, 2010 21:14 by eusonlito


Updated Code
#!/bin/sh

# -------------------------------------
# Script to do incremental rsync backups
# from a central backup server
#
# 08/03/2010 - A Navalla Suiza <[email protected]> 
#
# http://snipplr.com/view/39964/backup-servers-from-a-central-backup-server/
#
# This script is freely distributed under the GPL
# -------------------------------------


# -------------------------------------
# Configuration
#
# You only should touch this section
# -------------------------------------

# email address to send the error mails
MAILADDR="[email protected]"

# user to login into servers (recommended root)
USER="root"

# root directory to for backup stuff
BASEBACKUP="/backup"

# folder name to full copy
FULLNAME="full"

# folder name to incremental changes
INCREMENTALNAME="incremental"

# days to store the incremental changes
INCREMENTALTIME=7

# host list to backup
HOSTS="myserver1.com myserver2.com myserver3.com"

# log directory
LOGS=`dirname "$(readlink -f "$0")"`"/logs"

# Size max in bytes to log files. After that size, log will be rotated and compressed
LOGSIZE=10000

# -------------------------------------
# Commands paths
# -------------------------------------

CMD_AWK=`which awk`
CMD_BZIP=`which bzip9`
CMD_DATE=`which date`
CMD_DU=`which du`
CMD_ECHO=`which echo`
CMD_FIND=`which find`
CMD_INSTALL=`which install`
CMD_KILLALL=`which killall`
CMD_MV=`which mv`
CMD_RM=`which rm`
CMD_RSYNC=`which rsync`
CMD_SENDMAIL=`which sendmail`
CMD_SSH=`which ssh`


# -------------------------------------
# Functions definition
# -------------------------------------

echo_log () {
	$CMD_ECHO $1 >> $LOG
}

echo_start () {
	echo_log ''
	echo_log '----------------------------------------------------'
	echo_log "Start at: `$CMD_DATE "+%Y-%m-%d %H:%M:%S"`"
	echo_log '----------------------------------------------------'
}

echo_end () {
	echo_log ''
	echo_log '----------------------------------------------------'
	echo_log "End at: `$CMD_DATE "+%Y-%m-%d %H:%M:%S"`"
	echo_log '----------------------------------------------------'
}

do_rsync () {
	$CMD_KILLALL rsync > /dev/null 2> /dev/null

	OPTIONS="--force --ignore-errors --delete --backup --backup-dir="$INCREMENTDIR/" --exclude /proc/ --exclude /dev/ --exclude /tmp/ --exclude /sys/ -e $CMD_SSH -avpz $USER@$HOST:/ $BASECOPY"

	echo_log ''
	echo_log '----------------------------------------------------'
	echo_log "$CMD_RSYNC $OPTIONS"
	echo_log '----------------------------------------------------'
	echo_log ''

	$CMD_RSYNC $OPTIONS >> $LOG 2>> $LOG

	echo_log ''
	echo_log '----------------------------------------------------'
	echo_log "$CMD_FIND $BASEINCREMENT -maxdepth 1 -type d -mtime +$INCREMENTALTIME -exec $CMD_RM -rf {} \;"
	echo_log '----------------------------------------------------'
	echo_log ''

	$CMD_FIND $BASEINCREMENT -maxdepth 1 -type d -mtime +$INCREMENTALTIME -exec $CMD_RM -rf {} \; >> $LOG 2>> $LOG
}

do_mail_error () {
	echo_log ''
	echo_log "[ERROR] Backup problem for $HOST"
	echo_log ''
	echo_log $1

	{ $CMD_ECHO 'Subject: [ERROR] Backup problem for '.$HOST; $CMD_ECHO $1; } | $CMD_SENDMAIL $MAILADDR
}


# -------------------------------------
# Script execution
# -------------------------------------

LOG="$LOGS/error.log"

if [ ! -d $BASEBACKUP ]; then
	echo_log ''
	echo_log "backups ($BASEBACKUP) folder does not exists"

	do_mail_error "backups ($BASEBACKUP) folder does not exists"

	echo_end

	exit 1
fi

if [ ! -d $LOGS ]; then
	echo_log ''
	echo_log "logs ($LOGS) folder does not exists"

	do_mail_error "LOGS ($LOGS) folder does not exists"

	echo_end

	exit 1
fi

for HOST in $HOSTS; do
	LOG="$LOGS/$HOST.log"

	if [ -f "$LOG" ]; then
		if [ $LOGSIZE -lt "`$CMD_DU -s $LOG | $CMD_AWK -F' ' '{print $1}'`" ]; then
			OLD_LOG=$LOG.`$CMD_DATE "+%Y%m%d-%H%M%S"`

			$CMD_MV $LOG $OLD_LOG

			$CMD_BZIP -9 $OLD_LOG
		fi
	fi

	echo_start

	if [ "$HOST" = '' ]; then
		echo_log ''
		echo_log "No HOST is defined at first parameter"

		do_mail_error "No HOST is defined at first parameter"

		echo_end

		exit 1
	fi

	test=`$CMD_SSH -q -q $USER@$HOST $CMD_ECHO 1 || $CMD_ECHO 2`

	if [ "$test" = '2' ]; then
		echo_log ''
		echo_log "Can not connect to $HOST server"

		do_mail_error "Can not connect to $HOST server"

		echo_end

		exit 1
	fi

	ARCHIVEROOT="$BASEBACKUP/$HOST"
	BASECOPY="$ARCHIVEROOT/$FULLNAME"
	BASEINCREMENT="$ARCHIVEROOT/$INCREMENTALNAME"
	INCREMENTDIR="$BASEINCREMENT/"`$CMD_DATE "+%Y-%m-%d"`

	if [ ! -d $BASECOPY ]; then
		$CMD_INSTALL -d "$BASECOPY"
	fi

	if [ ! -d $BASEINCREMENT ]; then
		$CMD_INSTALL -d "$BASEINCREMENT"
	fi

	do_rsync

	echo_end
done

Revision: 31377
at September 3, 2010 19:52 by eusonlito


Updated Code
#!/bin/sh

# -------------------------------------
# Script to do incremental rsync backups
# from a central backup server
#
# 08/03/2010 - A Navalla Suiza <[email protected]> 
#
# http://snipplr.com/view/39964/backup-servers-from-a-central-backup-server/
#
# This script is freely distributed under the GPL
# -------------------------------------


# -------------------------------------
# Configuration
#
# You only should touch this section
# -------------------------------------

# email address to send the error mails
MAILADDR="[email protected]"

# user to login into servers (recommended root)
USER="root"

# root directory to for backup stuff
BASEBACKUP="/backup"

# folder name to full copy
FULLNAME="full"

# folder name to incremental changes
INCREMENTALNAME="incremental"

# days to store the incremental changes
INCREMENTALTIME=7

# host list to backup
HOSTS="myserver1.com myserver2.com myserver3.com"

# log directory
LOGS=`dirname "$(readlink -f "$0")"`"/logs"

# Size max in bytes to log files. After that size, log will be rotated and compressed
LOGSIZE=10000

# -------------------------------------
# Commands paths
# -------------------------------------

CMD_AWK=`which awk`
CMD_BZIP=`which bzip9`
CMD_DATE=`which date`
CMD_DU=`which du`
CMD_ECHO=`which echo`
CMD_FIND=`which find`
CMD_INSTALL=`which install`
CMD_KILLALL=`which killall`
CMD_MV=`which mv`
CMD_RM=`which rm`
CMD_RSYNC=`which rsync`
CMD_SENDMAIL=`which sendmail`
CMD_SSH=`which ssh`


# -------------------------------------
# Functions definition
# -------------------------------------

echo_log () {
	$CMD_ECHO $1 >> $LOG
}

echo_start () {
	echo_log ''
	echo_log '----------------------------------------------------'
	echo_log "Start at: `$CMD_DATE "+%Y-%m-%d %H:%M:%S"`"
	echo_log '----------------------------------------------------'
}

echo_end () {
	echo_log ''
	echo_log '----------------------------------------------------'
	echo_log "End at: `$CMD_DATE "+%Y-%m-%d %H:%M:%S"`"
	echo_log '----------------------------------------------------'
}

do_rsync () {
	$CMD_KILLALL rsync > /dev/null 2> /dev/null

	OPTIONS="--force --ignore-errors --delete --backup --backup-dir="$INCREMENTDIR/" --exclude /proc/ --exclude /dev/ --exclude /tmp/ --exclude /sys/ -e $CMD_SSH -avpz $USER@$HOST:/ $BASECOPY"

	echo_log ''
	echo_log '----------------------------------------------------'
	echo_log "$CMD_RSYNC $OPTIONS"
	echo_log '----------------------------------------------------'
	echo_log ''

	$CMD_RSYNC $OPTIONS >> $LOG 2>> $LOG

	echo_log ''
	echo_log '----------------------------------------------------'
	echo_log "$CMD_FIND $BASEINCREMENT -maxdepth 1 -type d -mtime +$INCREMENTALTIME -exec $CMD_RM -rf {} \;"
	echo_log '----------------------------------------------------'
	echo_log ''

	$CMD_FIND $BASEINCREMENT -maxdepth 1 -type d -mtime +$INCREMENTALTIME -exec $CMD_RM -rf {} \; >> $LOG 2>> $LOG
}

do_mail_error () {
	echo_log ''
	echo_log "[ERROR] Backup problem for $HOST"
	echo_log ''
	echo_log $1

	{ $CMD_ECHO 'Subject: [ERROR] Backup problem for '.$HOST; $CMD_ECHO $1; } | $CMD_SENDMAIL $MAILADDR
}


# -------------------------------------
# Script execution
# -------------------------------------

LOG="$LOGS/error.log"

if [ ! -d $BASEBACKUP ]; then
	echo_log ''
	echo_log "backups ($BASEBACKUP) folder does not exists"

	do_mail_error "backups ($BASEBACKUP) folder does not exists"

	echo_end

	exit 1
fi

if [ ! -d $LOGS ]; then
	echo_log ''
	echo_log "logs ($LOGS) folder does not exists"

	do_mail_error "LOGS ($LOGS) folder does not exists"

	echo_end

	exit 1
fi

for HOST in $HOSTS; do
	LOG="$LOGS/$HOST.log"

	if [ $LOGSIZE -lt "`$CMD_DU -s $LOG | $CMD_AWK -F' ' '{print $1}'`" ]; then
		OLD_LOG=$LOG.`$CMD_DATE "+%Y%m%d-%H%M%S"`

		$CMD_MV $LOG $OLD_LOG

		$CMD_BZIP -9 $OLD_LOG
	fi

	echo_start

	if [ "$HOST" = '' ]; then
		echo_log ''
		echo_log "No HOST is defined at first parameter"

		do_mail_error "No HOST is defined at first parameter"

		echo_end

		exit 1
	fi

	test=`$CMD_SSH -q -q $USER@$HOST $CMD_ECHO 1 || $CMD_ECHO 2`

	if [ "$test" = '2' ]; then
		echo_log ''
		echo_log "Can not connect to $HOST server"

		do_mail_error "Can not connect to $HOST server"

		echo_end

		exit 1
	fi

	ARCHIVEROOT="$BASEBACKUP/$HOST"
	BASECOPY="$ARCHIVEROOT/$FULLNAME"
	BASEINCREMENT="$ARCHIVEROOT/$INCREMENTALNAME"
	INCREMENTDIR="$BASEINCREMENT/"`$CMD_DATE "+%Y-%m-%d"`

	if [ ! -d $BASECOPY ]; then
		$CMD_INSTALL -d "$BASECOPY"
	fi

	if [ ! -d $BASEINCREMENT ]; then
		$CMD_INSTALL -d "$BASEINCREMENT"
	fi

	do_rsync

	echo_end
done

Revision: 31376
at September 3, 2010 19:46 by eusonlito


Initial Code
#!/bin/sh

# -------------------------------------
# Script to do incremental rsync backups
# from a central backup server
#
# A Navalla Suiza 08/03/2010
#
# This script is freely distributed under the GPL
# -------------------------------------


# -------------------------------------
# Configuration
#
# You only should touch this section
# -------------------------------------

# email address to send the error mails
MAILADDR="[email protected]"

# user to login into servers (recommended root)
USER="root"

# root directory to for backup stuff
BASEBACKUP="/backup"

# folder name to full copy
FULLNAME="full"

# folder name to incremental changes
INCREMENTALNAME="incremental"

# days to store the incremental changes
INCREMENTALTIME=7

# host list to backup
HOSTS="myserver1.com myserver2.com myserver3.com"

# log directory
LOGS=`dirname "$(readlink -f "$0")"`"/logs"

# Size max in bytes to log files. After that size, log will be rotated and compressed
LOGSIZE=10000

# -------------------------------------
# Commands paths
# -------------------------------------

CMD_AWK=`which awk`
CMD_BZIP=`which bzip9`
CMD_DATE=`which date`
CMD_DU=`which du`
CMD_ECHO=`which echo`
CMD_FIND=`which find`
CMD_INSTALL=`which install`
CMD_KILLALL=`which killall`
CMD_MV=`which mv`
CMD_RM=`which rm`
CMD_RSYNC=`which rsync`
CMD_SENDMAIL=`which sendmail`
CMD_SSH=`which ssh`


# -------------------------------------
# Functions definition
# -------------------------------------

echo_log () {
	$CMD_ECHO $1 >> $LOG
}

echo_start () {
	echo_log ''
	echo_log '----------------------------------------------------'
	echo_log "Start at: `$CMD_DATE "+%Y-%m-%d %H:%M:%S"`"
	echo_log '----------------------------------------------------'
}

echo_end () {
	echo_log ''
	echo_log '----------------------------------------------------'
	echo_log "End at: `$CMD_DATE "+%Y-%m-%d %H:%M:%S"`"
	echo_log '----------------------------------------------------'
}

do_rsync () {
	$CMD_KILLALL rsync > /dev/null 2> /dev/null

	OPTIONS="--force --ignore-errors --delete --backup --backup-dir="$INCREMENTDIR/" --exclude /proc/ --exclude /dev/ --exclude /tmp/ --exclude /sys/ -e $CMD_SSH -avpz $USER@$HOST:/ $BASECOPY"

	echo_log ''
	echo_log '----------------------------------------------------'
	echo_log "$CMD_RSYNC $OPTIONS"
	echo_log '----------------------------------------------------'
	echo_log ''

	$CMD_RSYNC $OPTIONS >> $LOG 2>> $LOG

	echo_log ''
	echo_log '----------------------------------------------------'
	echo_log "$CMD_FIND $BASEINCREMENT -maxdepth 1 -type d -mtime +$INCREMENTALTIME -exec $CMD_RM -rf {} \;"
	echo_log '----------------------------------------------------'
	echo_log ''

	$CMD_FIND $BASEINCREMENT -maxdepth 1 -type d -mtime +$INCREMENTALTIME -exec $CMD_RM -rf {} \; >> $LOG 2>> $LOG
}

do_mail_error () {
	echo_log ''
	echo_log "[ERROR] Backup problem for $HOST"
	echo_log ''
	echo_log $1

	{ $CMD_ECHO 'Subject: [ERROR] Backup problem for '.$HOST; $CMD_ECHO $1; } | $CMD_SENDMAIL $MAILADDR
}


# -------------------------------------
# Script execution
# -------------------------------------

LOG="$LOGS/error.log"

if [ ! -d $BASEBACKUP ]; then
	echo_log ''
	echo_log "backups ($BASEBACKUP) folder does not exists"

	do_mail_error "backups ($BASEBACKUP) folder does not exists"

	echo_end

	exit 1
fi

if [ ! -d $LOGS ]; then
	echo_log ''
	echo_log "logs ($LOGS) folder does not exists"

	do_mail_error "LOGS ($LOGS) folder does not exists"

	echo_end

	exit 1
fi

for HOST in $HOSTS; do
	LOG="$LOGS/$HOST.log"

	if [ $LOGSIZE -lt "`$CMD_DU -s $LOG | $CMD_AWK -F' ' '{print $1}'`" ]; then
		OLD_LOG=$LOG.`$CMD_DATE "+%Y%m%d-%H%M%S"`

		$CMD_MV $LOG $OLD_LOG

		$CMD_BZIP -9 $OLD_LOG
	fi

	echo_start

	if [ "$HOST" = '' ]; then
		echo_log ''
		echo_log "No HOST is defined at first parameter"

		do_mail_error "No HOST is defined at first parameter"

		echo_end

		exit 1
	fi

	test=`$CMD_SSH -q -q $USER@$HOST $CMD_ECHO 1 || $CMD_ECHO 2`

	if [ "$test" = '2' ]; then
		echo_log ''
		echo_log "Can not connect to $HOST server"

		do_mail_error "Can not connect to $HOST server"

		echo_end

		exit 1
	fi

	ARCHIVEROOT="$BASEBACKUP/$HOST"
	BASECOPY="$ARCHIVEROOT/$FULLNAME"
	BASEINCREMENT="$ARCHIVEROOT/$INCREMENTALNAME"
	INCREMENTDIR="$BASEINCREMENT/"`$CMD_DATE "+%Y-%m-%d"`

	if [ ! -d $BASECOPY ]; then
		$CMD_INSTALL -d "$BASECOPY"
	fi

	if [ ! -d $BASEINCREMENT ]; then
		$CMD_INSTALL -d "$BASEINCREMENT"
	fi

	do_rsync

	echo_end
done

Initial URL
https://navalla.backpackit.com/pub/2094408-how-to-configurar-un-servidor-de-backup

Initial Description
This script let you back up your servers at once from a central backup server. First you must copy the central server ssh public key into the servers to backup (without password). After that you can install this script with a cron.

Initial Title
Backup servers from a central backup server

Initial Tags
server, backup

Initial Language
Bash