#! /usr/local/bin/ruby
# Options/Variables
BASE_DIR = "/root/scripts/backup/"
DIRS_TO_BACKUP = ['/root', '/usr', '/var']
MOUNT_DEVICE = "/dev/ad3s1e"
MOUNT_DIR = "/backup"
LOCKFILE = BASE_DIR + "backup.rb.lock"
ROTATE_LOGS = false
MAX_LOG_AGE = 26
#===============
require 'rubygems'
require 'open4'
module StartupRoutine
def self.do_lockfile
if File.exist?(LOCKFILE)
puts "Lockfile exists: " + LOCKFILE
puts "Exiting."
puts
exit
else
File.open(LOCKFILE, "w+") do |file|
file.write(Process.pid)
end
end
end
def self.do_mounts
pid, stdin, stdout, stderr = Open4::popen4("bash")
stdin.puts "mount #{MOUNT_DEVICE} #{MOUNT_DIR}"
stdin.close
ignored, status = Process::waitpid2 pid
if status.exitstatus != 0
puts "Error mounting..."
puts "stdout: #{stdout.read.strip}"
puts "stderr: #{stderr.read.strip}"
puts "Exiting."
puts
exit
else
puts "Mounted #{MOUNT_DEVICE} on #{MOUNT_DIR}..."
end
end
end
module BackupRoutine
def self.do_backup
puts "This is the main program..."
end
end
module TearDownRoutine
def self.remove_mounts
pid, stdin, stdout, stderr = Open4::popen4("bash")
stdin.puts "umount #{MOUNT_DIR}"
stdin.close
ignored, status = Process::waitpid2 pid
if status.exitstatus != 0
puts "Error removing mount(s)..."
puts "stdout: #{stdout.read.strip}"
puts "stderr: #{stderr.read.strip}"
puts "Exiting."
puts
exit
else
puts "Removed mount #{MOUNT_DEVICE} from #{MOUNT_DIR}..."
end
end
def self.remove_lockfile
File.delete(LOCKFILE)
end
end
# Run it
StartupRoutine::do_lockfile
StartupRoutine::do_mounts
BackupRoutine::do_backup
TearDownRoutine::remove_mounts
TearDownRoutine::remove_lockfile
Refactorings
No refactoring yet !
Adam
January 30, 2009, January 30, 2009 05:49, permalink
Just a thought about how you might better handle mounting.
module Backup
class Mountpoint
attr_reader :device, :directory
def self.attach(device, directory, &block)
new(device, directory).attach(&block)
end
def initialize(device, directory)
@device, @directory, @mounted = device, directory, false
end
def mount
@mounted ||= system(%{mount "#{device}" "#{directory}"})
end
def unmount
@mounted &&= !system(%{unmount "#{directory}"})
end
def attach
mount
yield(self)
unmount
end
end
end
Backup::Mountpoint.attach('/dev/ad3s1e', '/backup') do |mountpoint|
# Perform backup routine here
end
dthomas53
January 31, 2009, January 31, 2009 00:25, permalink
I can already see this is cleaner, more reusable, and avoids the need for rubygems and open4. Much thanks, Adam. If I can ever figure out the regular expression part of my backup routine I'll post it.
Thanks again. :)
shawn
February 1, 2009, February 01, 2009 10:48, permalink
this is kinda unrelated but your sheband is wrong, it has an extra space that will make it not run
#!/usr/bin/ruby #!/usr/bin/env ruby
#!/usr/local/bin/ruby
shawn
February 1, 2009, February 01, 2009 10:49, permalink
this is kinda unrelated but your sheband is wrong, it has an extra space that will make it not run
#!/usr/bin/ruby #!/usr/bin/env ruby
#!/usr/local/bin/ruby
I started writing this backup script to learn Ruby; though I haven't written any of the backup functionality yet, I wanted to know if I'm going about it wrong thus far, or anyone has any pointers. So far I've just written a quick lockfile check/creation, and the mounting portion. Thanks in advance!