   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
 1http://mustafa-h.wiki.zoho.com/
 181.
       
 182.
          let W+=1
 183.
      done 