flipje

backup-over-ssh-tunnel

Jul 8th, 2011
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.73 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.  
  13.  
  14. # Functions:
  15.  
  16. # The main function.
  17. function fMain()
  18. {
  19.   # source and destination:
  20.   local DEST_DIR='/backups/HOST'  
  21.   local SOURCEDIRS='etc root usr home var'
  22.  
  23.   # tunnel settings:
  24.   local LOCALHOST='127.0.0.1'
  25.   local TUNNELPORT='2622'
  26.  
  27.   # ssh settings:
  28.   local SSH="ssh -q -A"
  29.   local SSHPORT='22'
  30.   local USER='YOU'    
  31.   local KEYCHAIN_FILE='/root/.keychain/backup-sh'
  32.  
  33.   # hops in between:
  34.   local HOP1='bastionhost.example.com'
  35.   local HOP2='fisthop.example.com'
  36.   local HOP3='nexthop.internal.example.com'
  37.   local HOP4='final.destination.internal.example.com'
  38.  
  39.   # how long should the tunnel stay open:
  40.   local SLEEPYTIME='1200'
  41.  
  42.   # rsync settings:
  43.   local RSYNC="rsync -q"
  44.   local RUSER='root'
  45.  
  46.   # time settings:
  47.   local TIME_STAMP="$(date '+%F_%H.%M.%S')"
  48.  
  49.   # Check whether arguments are given:
  50.   if [ ${#} -gt 0 ]
  51.   then
  52.     fShowUsage
  53.     return 1
  54.   fi
  55.  
  56.   # Load SSH agent environment variables:
  57.   if [ -f "${KEYCHAIN_FILE}" ]
  58.   then
  59.     . "${KEYCHAIN_FILE}"
  60.   else
  61.     echo "\"${KEYCHAIN_FILE}\" does not exist!"
  62.     return 3
  63.   fi
  64.  
  65.   # Check target directory:
  66.   if [ ! -d "${DEST_DIR}" ]
  67.   then
  68.     echo "Backup directory \"${DEST_DIR}\" does not exist!"
  69.     return 2
  70.   fi
  71.  
  72.   # Check whether SSH to first  host without password is possible:
  73.   if ( ! ssh -p${SSHPORT} -o 'BatchMode yes' -qq ${USER}@${HOP1} exit 0 )
  74.   then
  75.     echo "No SSH access to host ${HOP1}"
  76.     return 4
  77.   fi
  78.  
  79.   # define tunnel vars:
  80.   local TUNNEL="${TUNNELPORT}:${LOCALHOST}:${TUNNELPORT}"
  81.    
  82.   # setup tunnel:
  83.   ${SSH} -p${SSHPORT} -t -A -L ${TUNNEL} ${USER}@${HOP1} ${SSH} -p${SSHPORT} -t -A -L ${TUNNEL} ${HOP2} ${SSH} -p${SSHPORT} -t -A -L ${TUNNELPORT}:${HOP4}:${SSHPORT} ${HOP3} "sleep ${SLEEPYTIME}" > /dev/null 2>&1 &
  84.  
  85.   # sleep for a while
  86.   sleep 5
  87.  
  88.   # check if tunnel is alive:
  89.   echo "QUIT" | nc 127.0.0.1 ${TUNNELPORT} | grep -q 'SSH-2.0-OpenSSH'
  90.   if [ ${?} != 0 ]
  91.   then
  92.     echo "Tunnel instable or nonexistent... exiting backup script! or check ssh output with netcat!"
  93.     exit 1
  94.   fi
  95.  
  96. #  # start script on machine to rsync homedirs to local homedir:
  97. #    ${SSH} ${RUSER}@${LOCALHOST} -p ${TUNNELPORT} "/root/scripts/rsync-to-disk"
  98. #  
  99. #  # exit code checken:
  100. #  if [ ${?} !=  0 ]
  101. #  then
  102. #   echo "running Syncscript through ssh-tunnel on ${HOP4} failed!"
  103. #   exit 1
  104. #  fi
  105.  
  106.   # ALL DIRS BACKUPPEN
  107.  
  108.   # for loopje:
  109.   for SOURCEDIR in ${SOURCEDIRS}
  110.   do
  111.      # Check target directory:
  112.      if [ ! -d ${DEST_DIR}/${SOURCEDIR} ]
  113.      then
  114.        echo "${DEST_DIR}/${SOURCEDIR} does not exist, creating..."
  115.        mkdir -p ${DEST_DIR}/${SOURCEDIR}
  116.        # check exit code
  117.        if [ ${?} != 0 ]
  118.        then
  119.          echo "Failed to create ${DEST_DIR}/${SOURCEDIR}, skipping....."
  120.          continue
  121.        fi
  122.      fi
  123.  
  124.      # rsync dir to backupdir location:
  125.      ${RSYNC} --exclude '.gvfs' -avz -e "${SSH} -p${TUNNELPORT}" ${RUSER}@${LOCALHOST}:/${SOURCEDIR} ${DEST_DIR}/${SOURCEDIR}
  126.  
  127.      # check if succeeded
  128.      if [ ${?} != 0 ]
  129.      then
  130.        echo " offdisk backup of ${SOURCEDIR} on \"${TIME_STAMP}\" has failed "
  131.        continue
  132.      fi
  133.   done
  134.  
  135.  
  136.   return 0
  137. }
  138.  
  139. # Shows usage.
  140. function fShowUsage()
  141. {
  142.   echo "Usage: ${SCRIPT_PATH}"
  143.   return 0
  144. }
  145.  
  146.  
  147. # Start the program:
  148. fMain "${@}"
  149.  
  150. # Exit with previous return code:
  151. exit "${?}"
Advertisement
Add Comment
Please, Sign In to add comment