Home / ... / Scripts / Backup Scripts / Backing Up Your Files!
Page options

Backing Up Your Files!



Backup Your Files With This Bash Script!

  
    
(BackingUpYourFiles.shDownloads
  1. #!/usr/bin/bash
  2. #
  3. # Description: A simple backup script. Archive a directory,
  4. #              hang on to a finite number of archives.
  5. #
  6. # Author: Tom Arnold
  7. #
  8. # License: Do whatever you want. :)
  9. #
  10. # Installation: If you want this to run as a cron job, put
  11. #               it somewhere like '/etc/cron.daily' or
  12. #               '/etc/cron.weekly'. Mileage may vary.
  13. #
  14. #               If you just want to run it manual, execute
  15. #               the script with something like 'sh /path/to/script.sh'.
  16. #
  17. #               Don't forget to customize in the configuration section
  18. #               below before you run it. :)
  19. ##
  20.  
  21. ########################################################
  22. # Configuration                                        #
  23. #                                                      #
  24. # This section consists of variable definitions.       #
  25. # When you define a variable in BASH, you don't use    #
  26. # the '$' sign in front of it. If you reference it     #
  27. # later though, you'll need it (for example, comparing #
  28. # two variables in an if-statement.                    #
  29. ########################################################
  30.  
  31. # The directory in which backups are to be archived.
  32. # Ex. '/home/tom/backups'
  33. DEST='dest'
  34.  
  35. # The directory to backup.
  36. # Ex. '/home/tom/important_stuff'
  37. SRC='src'
  38.  
  39. # Set the archive compression.
  40. # Choices are 'gzip', and 'bzip2'.
  41. #
  42. # 'gzip' is faster, 'bzip2' has better compression.
  43. CMPRS='gzip'
  44.  
  45. # A prefix to use for the archive files.
  46. NAME='test'
  47.  
  48. # Number of backups to keep.
  49. NUM='5'
  50.  
  51. ######################################################
  52. # Making the backup.                                 #
  53. #                                                    #
  54. # In this section we will build up a command to run. #
  55. # By the end of this section, the variable $CMD      #
  56. # should be equal to something like                  #
  57. # 'tar cjf test-10-12-07.tar.bz2'.                   #
  58. ######################################################
  59.  
  60. # Run the UNIX date program, and create a new variable
  61. # named 'DATE' with a value equal to whatever UNIX date
  62. # returned (something like '10/12/07').
  63. DATE=$(date +%x)
  64.  
  65. # Slashes aren't going to look very nice in our filename,
  66. # use a "regular expression" (aka regex) to replace them
  67. # with dashes.
  68. #
  69. # Don't worry about this part too much, regex's are certainly
  70. # useful, but not required for BASH programming.
  71. DATE=${DATE//\//\-}
  72.  
  73. # Append the date we just got onto the 'NAME' variable.
  74. NAME+=-$DATE
  75.  
  76. # This variable is where we're going to build our command.
  77. CMD='tar c'
  78.  
  79. # If compression is set to "gzip", use the 'z' option.
  80. # Otherwise use the 'j' option ("bzip2").
  81. #
  82. # Also, append the rest of the stuff to our command.
  83. if [ $CMPRS == 'gzip' ];
  84. then
  85.     CMD+='zf '"$DEST"/"$NAME"'.tar.gz '"$SRC"
  86. else
  87.     CMD+='jf '"$DEST"/"$NAME"'.tar.bz2 '"$SRC"
  88. fi
  89.  
  90. # Run the command (make the backup).
  91. $CMD
  92.  
  93. #####################################################
  94. # Cleanup.                                          #
  95. #                                                   #
  96. # The point of this script is to automate things.   #
  97. # If we keep creating new archives, we're going to  #
  98. # run out of hard disk space eventually. Therefore, #
  99. # we need to do some garbage collection. :)         #
  100. #####################################################
  101.  
  102. # Iterate through our archive directory.
  103. # Count up how many files we have, and
  104. # collect names and dates.
  105. #
  106. # Note: This part uses arrays. If you have no prior
  107. #       programming experience, these may trip you
  108. #       up. I suggest googling a tutorial on them
  109. #       (C, C++, Java, they're all pretty similar).
  110. N=0
  111.  
  112. for FILE2 in $DEST/*
  113. do
  114.   FNAMES[$N]=$FILE2
  115.  
  116.   # This uses the UNIX stat program.
  117.   # Again, you don't really need to know about this.
  118.   DATES[$N]=$(stat -c %Y "$FILE2")
  119.  
  120.   # Increase our file counter by one.
  121.   let N+=1
  122. done
  123.  
  124. # If we're not over our backup limit, just call it quits.
  125. # Otherwise sort our filenames by date, and delete the oldest
  126. # until we're under our limit again.
  127. K=0
  128. if [ $N -le $NUM ];
  129. then
  130.     #echo "Under the limit, not deleting anything."
  131.     exit
  132. else
  133.     # Sorting time!
  134.     let K=0 # An index variable.
  135.  
  136.     # While the index ('K') is less than the number of filenames
  137.     # we collected, do this loop.
  138.     while [ $K -lt $N ];
  139.     do
  140.       # Index and value of the maximum.
  141.       let MI=0
  142.       let MAX=0
  143.  
  144.       # Another index.
  145.       let l=0
  146.  
  147.       # Iterate through our files and find the oldest.
  148.       while [ $l -lt $N ];
  149.       do
  150.         if [ ${DATES[$l]} -gt $MAX ];
  151.         then
  152.             let MAX=${DATES[$l]}
  153.             let MI=$l
  154.         fi
  155.  
  156.         # Increment 'l'
  157.         let l+=1
  158.       done
  159.  
  160.       # Add the max from that time arround to a new
  161.       # array. Set the old value to something silly.
  162.       FNAMES2[$K]=${FNAMES[$MI]}
  163.       DATES2[$K]=${DATES[$MI]}
  164.       DATES[$MI]=-10
  165.  
  166.       # Increment 'K'.
  167.       let K+=1
  168.     done
  169. fi
  170.  
  171. # Count up to the number of backups we want to keep,
  172. # then start deleting.
  173. W=0
  174. while [ $W -lt $N ];
  175. do
  176.     if [ $W -ge $NUM ];
  177.     then
  178.         #echo "Deleting "${FNAMES2[$W]}"
  179.         rm "${FNAMES2[$W]}"
  180.     fi
  181.  
  182.     let W+=1
  183. done

     

    Post a comment

    Your Name or E-mail ID (mandatory)

     

    No Attachments Found. Click here to upload new file.




     RSS of this page

    Author: mustafa_h   Version: 1.5   Last Edited By: mustafa_h   Modified: 06 Apr 2010



    Sign in