Backup servers from a central backup server


/ Published in: Bash
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  1. #!/bin/sh
  2.  
  3. # -------------------------------------
  4. # Script to do incremental rsync backups
  5. # from a central backup server
  6. #
  7. # 17/08/2010 - A Navalla Suiza <equipo@anavallasuiza.com>
  8. #
  9. # http://snipplr.com/view/39964/backup-servers-from-a-central-backup-server/
  10. #
  11. # This script is freely distributed under the GPL
  12. # -------------------------------------
  13.  
  14.  
  15. # -------------------------------------
  16. # Configuration
  17. #
  18. # You only should touch this section
  19. # -------------------------------------
  20.  
  21. # email address to send the error mails
  22. MAILADDR="logs@mydomain.com"
  23.  
  24. # user to login into servers (recommended root)
  25. USER="root"
  26.  
  27. # root directory to for backup stuff
  28. BASEBACKUP="/backup"
  29.  
  30. # folder name to full copy
  31. FULLNAME="full"
  32.  
  33. # folder name to incremental changes
  34. INCREMENTALNAME="incremental"
  35.  
  36. # days to store the incremental changes
  37. INCREMENTALTIME=7
  38.  
  39. # host list to backup
  40. HOSTS="myserver1.com myserver2.com myserver3.com"
  41.  
  42. # log directory
  43. LOGS=`dirname "$(readlink -f "$0")"`"/logs"
  44.  
  45. # Size max in bytes to log files. After that size, log will be rotated and compressed
  46. LOGSIZE=10000
  47.  
  48. # Search commands in...
  49. export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11"
  50.  
  51. # -------------------------------------
  52. # Functions definition
  53. # -------------------------------------
  54.  
  55. echo_log () {
  56. if [ "$CMD_ECHO" != '' ]; then
  57. $CMD_ECHO "$1" >> $LOG
  58. fi
  59. }
  60.  
  61. echo_start () {
  62. echo_log ''
  63. echo_log '----------------------------------------------------'
  64. echo_log "[$1] Start at: `$CMD_DATE "+%Y-%m-%d %H:%M:%S"`"
  65. echo_log '----------------------------------------------------'
  66. }
  67.  
  68. echo_end () {
  69. echo_log ''
  70. echo_log '----------------------------------------------------'
  71. echo_log "[$1] End at: `$CMD_DATE "+%Y-%m-%d %H:%M:%S"`"
  72. echo_log '----------------------------------------------------'
  73. }
  74.  
  75. do_rsync () {
  76. $CMD_KILLALL $CMD_RSYNC > /dev/null 2> /dev/null
  77.  
  78. OPTIONS="--force --ignore-errors --delete --backup --backup-dir="$INCREMENTDIR/" --exclude /proc/ --exclude /dev/ --exclude /tmp/ --exclude /sys/ -e $CMD_SSH -avpz $USER@$HOST:/ $BASECOPY"
  79.  
  80. echo_log ''
  81. echo_log '----------------------------------------------------'
  82. echo_log "$CMD_RSYNC $OPTIONS"
  83. echo_log '----------------------------------------------------'
  84. echo_log ''
  85.  
  86. $CMD_RSYNC $OPTIONS >> $LOG 2>> $LOG
  87.  
  88. echo_log ''
  89. echo_log '----------------------------------------------------'
  90. echo_log "$CMD_FIND $BASEINCREMENT -maxdepth 1 -type d -mtime +$INCREMENTALTIME -exec $CMD_RM -rf {} \;"
  91. echo_log '----------------------------------------------------'
  92. echo_log ''
  93.  
  94. $CMD_FIND $BASEINCREMENT -maxdepth 1 -type d -mtime +$INCREMENTALTIME -exec $CMD_RM -rf {} \; >> $LOG 2>> $LOG
  95. }
  96.  
  97. do_mail_error () {
  98. echo_log ''
  99. echo_log "[ERROR] Backup problem for $HOST"
  100. echo_log ''
  101. echo_log $1
  102.  
  103. { $CMD_ECHO 'Subject: [ERROR] Backup problem for '.$HOST; $CMD_ECHO $1; } | $CMD_SENDMAIL $MAILADDR
  104. }
  105.  
  106.  
  107. # -------------------------------------
  108. # Script execution
  109. # -------------------------------------
  110.  
  111. LOG="$LOGS/error.log"
  112.  
  113. COMMANDS="echo date sendmail awk bzip2 du find install killall mv rm rsync ssh"
  114.  
  115. for i in $COMMANDS; do
  116. exists=`which $i`
  117.  
  118. if [ "$exists" = "" ]; then
  119. echo_log ''
  120. echo_log "Command $i does not exists in $PATH. Backup will be cancelled."
  121.  
  122. if [ "$CMD_SENDMAIL" != "" ]; then
  123. do_mail_error "Command $i does not exists. Backup will be cancelled"
  124. fi
  125.  
  126. echo_end
  127.  
  128. exit 1
  129. fi
  130.  
  131. eval "CMD_"`echo $i | tr "[:lower:]" "[:upper:]"`"='$exists'"
  132. done
  133.  
  134. if [ ! -d $BASEBACKUP ]; then
  135. echo_log ''
  136. echo_log "Backup folder ($BASEBACKUP) does not exists"
  137.  
  138. do_mail_error "Backup folder ($BASEBACKUP) does not exists"
  139.  
  140. echo_end
  141.  
  142. exit 1
  143. fi
  144.  
  145. if [ ! -d $LOGS ]; then
  146. echo_log ''
  147. echo_log "Logs folder ($LOGS) does not exists"
  148.  
  149. do_mail_error "Logs folder ($LOGS) does not exists"
  150.  
  151. echo_end
  152.  
  153. exit 1
  154. fi
  155.  
  156. for HOST in $HOSTS; do
  157. LOG="$LOGS/$HOST.log"
  158.  
  159. if [ -f "$LOG" ]; then
  160. if [ $LOGSIZE -lt "`$CMD_DU -s $LOG | $CMD_AWK -F' ' '{print $1}'`" ]; then
  161. OLD_LOG=$LOG.`$CMD_DATE "+%Y%m%d-%H%M%S"`
  162.  
  163. $CMD_MV $LOG $OLD_LOG
  164.  
  165. $CMD_BZIP2 -9 $OLD_LOG
  166. fi
  167. fi
  168.  
  169. echo_start $HOST
  170.  
  171. if [ "$HOST" = '' ]; then
  172. echo_log ''
  173. echo_log "No HOST is defined at first parameter"
  174.  
  175. do_mail_error "No HOST is defined at first parameter"
  176.  
  177. echo_end $HOST
  178.  
  179. exit 1
  180. fi
  181.  
  182. test=`$CMD_SSH -q -q $USER@$HOST $CMD_ECHO 1 || $CMD_ECHO 2`
  183.  
  184. if [ "$test" = '2' ]; then
  185. echo_log ''
  186. echo_log "Can not connect to $HOST server"
  187.  
  188. do_mail_error "Can not connect to $HOST server"
  189.  
  190. echo_end $HOST
  191.  
  192. exit 1
  193. fi
  194.  
  195. ARCHIVEROOT="$BASEBACKUP/$HOST"
  196. BASECOPY="$ARCHIVEROOT/$FULLNAME"
  197. BASEINCREMENT="$ARCHIVEROOT/$INCREMENTALNAME"
  198. INCREMENTDIR="$BASEINCREMENT/"`$CMD_DATE "+%Y-%m-%d"`
  199.  
  200. if [ ! -d $BASECOPY ]; then
  201. $CMD_INSTALL -d "$BASECOPY"
  202. fi
  203.  
  204. if [ ! -d $BASEINCREMENT ]; then
  205. $CMD_INSTALL -d "$BASEINCREMENT"
  206. fi
  207.  
  208. do_rsync
  209.  
  210. echo_end $HOST
  211. done

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.