flipje

backup-host-with-rsync

Jan 8th, 2012
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.26 KB | None | 0 0
  1. #!/bin/bash -x
  2. #-------------------------------------------------------------------------------
  3. # dit script backupt de /root/ /home/ /etc/ en /usr/ en de /data
  4. # flip hess 2011 06 20 [email protected]
  5. #
  6. #-------------------------------------------------------------------------------
  7.  
  8. # Global variables:
  9.  
  10. PATH='/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin'
  11. SCRIPT_PATH="${0}"
  12. HOST="${1}"
  13.  
  14.  
  15. # Functions:
  16.  
  17. # The main function.
  18. function fMain()
  19. {
  20.   # source and destination:
  21.   local DEST_DIR="/Users/flip/Backups/${1}"
  22.   local SOURCEDIRS='etc root usr home var data media'
  23.   # tunnel settings:
  24.   local SSH='ssh -A'
  25.   local SSHPORT='22'
  26.  
  27.   # rsync settings:
  28.   local RSYNC="rsync --exclude '.gvfs' -avz -e"
  29.   local RUSER='root'
  30.  
  31.   # time settings:
  32.   local TIME_STAMP="$(date '+%F_%H.%M.%S')"
  33.  
  34.   # Check whether arguments are given:
  35.   if [ ${#} != 1 ]
  36.   then
  37.     fShowUsage
  38.     return 1
  39.   fi
  40.  
  41.   # Check target directory:
  42.   if [ ! -d "${DEST_DIR}" ]
  43.   then
  44.     { echo "Backup directory \"${DEST_DIR}\" does not exist!, Creating..." ; mkdir ${DEST_DIR}; }
  45.   fi
  46.  
  47.   # Check whether SSH to  host without password is possible:
  48.   if ( ! ${SSH} -p${SSHPORT} -o 'BatchMode yes' -qq ${RUSER}@${HOST} exit 0 )
  49.   then
  50.     echo "No SSH access to host ${HOST}"
  51.     return 4
  52.   fi
  53.  
  54.   # ALL DIRS BACKUPPEN
  55.  
  56.   # for loopje:
  57.   for SOURCEDIR in ${SOURCEDIRS}
  58.   do
  59.      # Check target directory:
  60.      if [ ! -d ${DEST_DIR}/${SOURCEDIR} ]
  61.      then
  62.        echo "${DEST_DIR}/${SOURCEDIR} does not exist, creating..."
  63.        mkdir -p ${DEST_DIR}/${SOURCEDIR}
  64.        # check exit code
  65.        if [ ${?} != 0 ]
  66.        then
  67.          echo "Failed to create ${DEST_DIR}/${SOURCEDIR}, skipping....."
  68.          continue
  69.        fi
  70.      fi
  71.  
  72.      # rsync dir to backupdir location:
  73.      ${RSYNC} "${SSH} -p${SSHPORT}" ${RUSER}@${HOST}:/${SOURCEDIR} ${DEST_DIR}/${SOURCEDIR}
  74.  
  75.      # check if succeeded
  76.      if [ ${?} != 0 ]
  77.      then
  78.        echo " offdisk backup of ${SOURCEDIR} on \"${TIME_STAMP}\" has failed "
  79.        continue
  80.      fi
  81.   done
  82.  
  83.  
  84.   return 0
  85. }
  86.  
  87. # Shows usage.
  88. function fShowUsage()
  89. {
  90.   echo "Usage: ${SCRIPT_PATH} HOST"
  91.   return 0
  92. }
  93.  
  94.  
  95. # Start the program:
  96. fMain "${@}"
  97.  
  98. # Exit with previous return code:
  99. exit "${?}"
Advertisement
Add Comment
Please, Sign In to add comment