March 2007

You are currently browsing the monthly archive for March 2007.

YARS

Yet another ruby script

This time, taken from Rails Cookbook by O’Reilly. Checking in your spankin’-new Rails app to and SVN repository: http://tamaratemple.com/pub/ruby/svnizerails.rb. (It’s a little more complicated than it seems; hence the script.)

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • email
  • Reddit
  • Slashdot
  • StumbleUpon
  • Technorati
  • Twitter

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)
[code lang="ruby"]

  1. !/usr/bin/env ruby
  2. Created by Tamara Temple on 2007-03-16.
  3. Copyright (c) 2007. All rights reserved.

require 'FileUtils'
include FileUtils::Verbose

LIBRARY_DIRS =

["./Library/Application Support",
"./Library/Keychains",
",/Library/Mail",
"./Library/Mozilla",
"./Library/Preferences",
"./Library/Safari",
"./Library/StickiesDatabase",
"./Library/Workflows"]

def getBackDir(timestamp)

prefix = "/Volumes/BootBackup/Users"
suffix = timestamp.strftime("%Y%m%d.%H%M%S")
backupdir = prefix + suffix
print "** backupdir: ", backupdir, "\n" if $DEBUG
backupdir

end

def getUserList(parentdir)

userlist = Dir.entries(parentdir)

if (!userlist.kind_of?(Array)) then raise "Internal Error: Dir.entries did not return an array." end

userlist.delete(".") # remove the working directory from the list (this will not affect

  1. the directory on the disk)
userlist.delete("..") # remove the parent directory from the list
userlist.delete(".localized") # remove an extraneous file from the /Users directory
print "** userlist: ", userlist.join(", "),"\n" if $DEBUG
userlist # return the userlist

end

def getFileList()

files = Dir.entries('.')

if (!files.kind_of?(Array)) then raise "Internal Error: Dir.entries did not return an array." end

files.delete(".") # remove the reference to the directory itself. (note this will

  1. not remove the directory - files is an array not the directory)
files.delete("..") # remove the parent directory listing
files.delete(".Trash") # don't backup the trash
files.delete("Library") # remove the Library directory as we don't want it all in
  1. the backup
print "** files: ", files.join(", "),"\n" if $DEBUG

begin

libdirs = Dir.entries("./Library")

rescue Errno::ENOENT

libdirs = []

ensure

if (!libdirs.kind_of?(Array)) then raise "Internal Error: Dir.entries did not return an array." end

end

  1. Remove extraneous entries
libdirs.delete(".")
libdirs.delete("..")
libdirs.delete(".localized")
libdirs.map! {|l| "./Library/" + l} # Need to add in the path to the
  1. Library subdirectories
print "** libdirs: ", libdirs.join(", "),"\n" if $DEBUG
libdirs = libdirs & LIBRARY_DIRS # get the intersection of library
  1. directories with the set we want
  2. to keep in backup

files = files + libdirs
files.map! {|f| "'#{f}'"} # need to quote file names for find() command
  1. in backupUsers() procedure
print "** files: ", files.join(", "),"\n" if $DEBUG

files # return the file list

end

def backupUsers(userlist,backupdir)

for user in userlist

mkdir("#{backupdir}/#{user}") unless $DEBUG
print "** mkdir #{backupdir}/#{user}","\n" if $DEBUG

cd(user) do # change directories in a block, at the end of the block, direcotry will go back to previous working directoryfiles = getFileList()

begin

`find #{files.join(" ")} -print | cpio -pdv #{backupdir}/#{user}` unless $DEBUG
print "** find:", files.join(", ")," | cpio: ", "#{backupdir}/#{user}","\n" if $DEBUG

rescue

warn "Error with backing up #{user}: #{$!}\nContinuing with next user."

end

end

end

end

backupdir = getBackDir(Time.now)
mkdir(backupdir)

cd("/Users") do # change directories in a block, at the end of the block,

  1. directory will go back to the previous working directory

userlist = getUserList("/Users")
backupUsers(userlist,backupdir)

end

[/code]

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • email
  • Reddit
  • Slashdot
  • StumbleUpon
  • Technorati
  • Twitter