/ Published in: Bash
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.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
#!/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
URL: https://navalla.backpackit.com/pub/2094408-how-to-configurar-un-servidor-de-backup