Writing ruby

Over the weekend, I wrote a small utility in ruby to backup my /Users directory to my external backup drive. Here it is: (script is downloadable here)

  1. #!/usr/bin/ruby
  2. #
  3. #  Created by Tamara Temple on 2007-03-16.
  4. #  Copyright (c) 2007. All rights reserved.
  5.  
  6. require 'FileUtils'
  7. include FileUtils::Verbose
  8.  
  9. LIBRARY_DIRS = 
  10.   ["./Library/Application Support",
  11.     "./Library/Keychains",
  12.     ",/Library/Mail",
  13.     "./Library/Mozilla",
  14.     "./Library/Preferences",
  15.     "./Library/Safari",
  16.     "./Library/StickiesDatabase",
  17.     "./Library/Workflows"]
  18.  
  19.  
  20. def getBackDir(timestamp)
  21.   prefix = "/Volumes/BootBackup/Users"
  22.   suffix = timestamp.strftime("%Y%m%d.%H%M%S")
  23.   backupdir = prefix + suffix
  24.   print "** backupdir: ", backupdir, "\n" if $DEBUG
  25.   backupdir
  26.  
  27. end
  28.  
  29. def getUserList(parentdir)
  30.  
  31.   userlist = Dir.entries(parentdir)
  32.  
  33.   if (!userlist.kind_of?(Array)) then raise "Internal Error: Dir.entries did not return an array." end
  34.  
  35.   userlist.delete(".") # remove the working directory from the list (this will not affect the directory on the disk)
  36.   userlist.delete("..") # remove the parent directory from the list
  37.   userlist.delete(".localized") # remove an extraneous file from the /Users directory
  38.   print "** userlist: ", userlist.join(", "),"\n" if $DEBUG 
  39.   userlist # return the userlist
  40.  
  41. end
  42.  
  43.  
  44. def getFileList()
  45.  
  46.   files = Dir.entries('.')
  47.  
  48.   if (!files.kind_of?(Array)) then raise "Internal Error: Dir.entries did not return an array." end
  49.  
  50.   files.delete(".") # remove the reference to the directory itself. (note this will not remove the directory -  files is an array not the directory)
  51.   files.delete("..") # remove the parent directory listing 
  52.   files.delete(".Trash") # don't backup the trash
  53.   files.delete("Library") # remove the Library directory as we don't want it all in the backup
  54.   print "** files: ", files.join(", "),"\n" if $DEBUG
  55.  
  56.   begin
  57.     libdirs = Dir.entries("./Library")
  58.   rescue Errno::ENOENT
  59.     libdirs = []
  60.   ensure
  61.     if (!libdirs.kind_of?(Array)) then raise "Internal Error: Dir.entries did not return an array." end
  62.   end
  63.  
  64.   # Remove extraneous entries
  65.   libdirs.delete(".")
  66.   libdirs.delete("..")
  67.   libdirs.delete(".localized")
  68.   libdirs.map! {|l| "./Library/" + l} # Need to add in the path to the Library subdirectories
  69.   print "** libdirs: ", libdirs.join(", "),"\n" if $DEBUG
  70.   libdirs = libdirs & LIBRARY_DIRS # get the intersection of library directories with the set we want to keep in backup
  71.  
  72.   files = files + libdirs
  73.   files.map! {|f| "'#{f}'"} # need to quote file names for find() command in backupUsers() procedure
  74.   print "** files: ", files.join(", "),"\n" if $DEBUG
  75.  
  76.   files # return the file list
  77. end
  78.  
  79. def backupUsers(userlist,backupdir)
  80.   for user in userlist
  81.     mkdir("#{backupdir}/#{user}") unless $DEBUG
  82.     print "** mkdir #{backupdir}/#{user}","\n" if $DEBUG
  83.     cd(user) do # change directories in a block, at the end of the block, direcotry will go back to previous working directory
  84.  
  85.       files = getFileList()
  86.  
  87.       begin
  88.         `find #{files.join(" ")} -print | cpio -pdv #{backupdir}/#{user}` unless $DEBUG
  89.         print "** find:", files.join(", ")," | cpio: ", "#{backupdir}/#{user}","\n" if $DEBUG
  90.       rescue
  91.         warn "Error with backing up #{user}: #{$!}\nContinuing with next user."
  92.       end
  93.  
  94.     end
  95.  
  96.   end
  97. end
  98.  
  99. backupdir = getBackDir(Time.now)
  100. mkdir(backupdir)
  101.  
  102. cd("/Users") do # change directories in a block, at the end of the block, directory will go back to the previous working directory
  103.   userlist = getUserList("/Users")
  104.   backupUsers(userlist,backupdir)
  105. end

Leave a Reply

Your email address will not be published. Required fields are marked *

*


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" cssfile="">