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