Guest User

Untitled

a guest
Oct 30th, 2011
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. #!/bin/sh
  2. # Author: Brice Burgess - [email protected]
  3. # multi_backup.sh -- backup to a local drive using rsync.
  4. # Uses hard-link rotation to keep multiple backups.
  5.  
  6. # Directories to backup. Seperate with a space. Exclude trailing slash!
  7. SOURCES="/home/gig"
  8.  
  9. # Directory to backup to. This is where your backup(s) will be stored. No spaces in names!
  10. # :: NOTICE :: -> Make sure this directory is empty or contains ONLY backups created by
  11. # this script and NOTHING else. Exclude trailing slash!
  12. TARGET="/tmp/shares/Backups/LinuxLap"
  13.  
  14. # Set the number of backups to keep (greater than 1). Ensure you have adaquate space.
  15. ROTATIONS=2
  16.  
  17. # Your EXCLUDE_FILE tells rsync what NOT to backup. Leave it unchanged if you want
  18. # to backup all files in your SOURCES. If performing a FULL SYSTEM BACKUP, ie.
  19. # Your SOURCES is set to "/", you will need to make use of EXCLUDE_FILE.
  20. # The file should contain directories and filenames, one per line.
  21. # A good example would be:
  22. # /proc
  23. # /tmp
  24. # *.SOME_KIND_OF_FILE
  25. EXCLUDE_FILE="/home/gig/gitclones/*
  26. /home/gig/github/*
  27. /home/gig/.emacs
  28. /home/gig/.emacs.d
  29. /home/gig/.awesome.ctl.0=
  30. /home/gig/.rxvt-unicode-gigNote=
  31. /home/gig/.pine-debug*
  32. /home/gig/.serverauth.*
  33. /home/gig/.zsh/history
  34. /home/gig/.irb_history
  35. /home/gig/.myXLog"
  36.  
  37. # Comment out the following line to disable verbose output
  38. VERBOSE="-v"
  39.  
  40. #######################################
  41. ########DO_NOT_EDIT_BELOW_THIS_POINT#########
  42. #######################################
  43.  
  44. # Set name (date) of backup.
  45. BACKUP_DATE="`date +%F_%H-%M`"
  46.  
  47. if [ ! -x $TARGET ]; then
  48. echo "Backup target does not exist or you don't have permission!"
  49. echo "Exiting..."
  50. exit 2
  51. fi
  52.  
  53. if [ ! $ROTATIONS -gt 1 ]; then
  54. echo "You must set ROTATIONS to a number greater than 1!"
  55. echo "Exiting..."
  56. exit 2
  57. fi
  58.  
  59. #### BEGIN ROTATION SECTION ####
  60.  
  61. BACKUP_NUMBER=1
  62. # incrementor used to determine current number of backups
  63.  
  64. # list all backups in reverse (newest first) order, set name of oldest backup to $backup
  65. # if the retention number has been reached.
  66. for backup in `ls -dXr $TARGET/*/`; do
  67. if [ $BACKUP_NUMBER -eq 1 ]; then
  68. NEWEST_BACKUP="$backup"
  69. fi
  70.  
  71. if [ $BACKUP_NUMBER -eq $ROTATIONS ]; then
  72. OLDEST_BACKUP="$backup"
  73. break
  74. fi
  75.  
  76. let "BACKUP_NUMBER=$BACKUP_NUMBER+1"
  77. done
  78.  
  79. # Check if $OLDEST_BACKUP has been found. If so, rotate. If not, create new directory for this backup.
  80. if [ $OLDEST_BACKUP ]; then
  81. # Set oldest backup to current one
  82. mv $OLDEST_BACKUP $TARGET/$BACKUP_DATE
  83. else
  84. mkdir $TARGET/$BACKUP_DATE
  85. fi
  86.  
  87. # Update current backup using hard links from the most recent backup
  88. if [ $NEWEST_BACKUP ]; then
  89. cp -al $NEWEST_BACKUP. $TARGET/$BACKUP_DATE
  90. fi
  91. #### END ROTATION SECTION ####
  92.  
  93.  
  94. # Check to see if rotation section created backup destination directory
  95. if [ ! -d $TARGET/$BACKUP_DATE ]; then
  96. echo "Backup destination not available. Make sure you have write permission in TARGET!"
  97. echo "Exiting..."
  98. exit 2
  99. fi
  100.  
  101. echo "Verifying Sources..."
  102. for source in $SOURCES; do
  103. echo "Checking $source..."
  104. if [ ! -x $source ]; then
  105. echo "Error with $source!"
  106. echo "Directory either does not exist, or you do not have proper permissions."
  107. exit 2
  108. fi
  109. done
  110.  
  111. if [ -f $EXCLUDE_FILE ]; then
  112. EXCLUDE="--exclude-from=$EXCLUDE_FILE"
  113. fi
  114.  
  115. echo "Sources verified. Running rsync..."
  116. for source in $SOURCES; do
  117.  
  118. # Create directories in $TARGET to mimick source directory hiearchy
  119. if [ ! -d $TARGET/$BACKUP_DATE/$source ]; then
  120. mkdir -p $TARGET/$BACKUP_DATE/$source
  121. fi
  122.  
  123. rsync $VERBOSE --exclude=$TARGET/ $EXCLUDE -a --delete $source/ $TARGET/$BACKUP_DATE/$source/
  124.  
  125. done
  126.  
  127. exit 0
Advertisement
Add Comment
Please, Sign In to add comment