Advertisement
cd62131

Incremental Backup

Nov 19th, 2013
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.00 KB | None | 0 0
  1. #!/bin/bash
  2. typeset -g BACKUP_STORAGE='/FullPathToYourStorage'
  3. typeset -g PREFIX='backup_'
  4. typeset -g TIME_STAMP=$(date -I)
  5. typeset -g BACKUP_DIR=${BACKUP_STORAGE}/${PREFIX}${TIME_STAMP}
  6. typeset -g PREV
  7. mkdir --parent --verbose ${BACKUP_STORAGE}
  8. if [[ -d ${BACKUP_DIR} ]]
  9. then
  10.   echo 'already updated'
  11.   exit 1
  12. fi
  13.  
  14. # find least backup
  15. typeset -i time_diff=$(date +'%s') # seconds since 1970-01-01 UTC
  16. typeset -i current_time=$((time_diff))
  17. for dir in ${BACKUP_STORAGE}/${PREFIX}*
  18. do
  19.   prev_time_stamp=$(echo $dir | sed "s;${BACKUP_STORAGE}/${PREFIX};;")
  20.   typeset -i prev_since_utc=$(($(date --date=${prev_time_stamp} +'%s')))
  21.   if (( current_time - prev_since_utc < time_diff ))
  22.   then
  23.     time_diff=$((current_time - prev_since_utc))
  24.     PREV=$dir
  25.   fi
  26. done
  27.  
  28. # new backup or differential backup
  29. if [[ -z ${PREV} ]]
  30. then
  31.   echo 'new backup'
  32.   rsync --archive --verbose $1 ${BACKUP_DIR}
  33. else
  34.   echo 'differential backup'
  35.   rsync --archive --verbose --link-dest=${PREV} $1 ${BACKUP_DIR}
  36. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement