Skip to content

Instantly share code, notes, and snippets.

@alfanick
Created December 28, 2009 20:05
Show Gist options
  • Save alfanick/264890 to your computer and use it in GitHub Desktop.
Save alfanick/264890 to your computer and use it in GitHub Desktop.
Simple script which helps backuping Lightroom catalog
#!/usr/local/bin/ruby
require 'fileutils'
require 'ftools'
MKISOFS_BIN = '/opt/local/bin/mkisofs'
module Lightroom
class Backup
attr_accessor :catalog
attr_accessor :output
attr_accessor :interactive
attr_accessor :size
attr_accessor :counter
def initialize(catalog, output)
@catalog = catalog
@output = output
@interactive = false
@size = 4700
@files_index = Hash.new
@counter = 1
end
def do!
iso do |files|
log "Backuping \#%d CD! %d files" % [@counter, files.size]
create_iso files, @counter
@counter += 1
end
puts "Done! Created %d CDs" % (@counter-1)
end
def Backup.rm_mark(catalog)
puts "Clearing markings"
Dir.glob(File.join(catalog, "**", ".*.lrbackup")).each do |file|
File.delete file
end
end
private
def iso
list = Array.new
max_size = @size
Dir.glob(File.join(@catalog, "20*", "**", "*")).each do |file|
next unless File.file? file
if size(file) >= @size
STDERR.puts "Impossible to backup - %s" % file
next
end
if size(file) >= max_size
log "Creating CD with %d files (%d megabytes)" % [list.size, @size-max_size]
yield list
list.clear
max_size = @size
end
if size(file) < max_size and not marked(file)
list += [file]
max_size -= size(file)
end
end
end
def create_iso files, number
tmp_dir = File.join(@output, "tmp_iso")
FileUtils.rm_r(tmp_dir) if File.exists? tmp_dir
iso_file = File.join(@output, "backup_#{number}.iso")
files.each do |f|
File.makedirs(File.join(tmp_dir, File.dirname(f).sub(@catalog,'/') ))
File.symlink(f, File.join(File.join(tmp_dir, File.dirname(f).sub(@catalog,'/') ), File.basename(f)))
end
command = "#{MKISOFS_BIN} -quiet -posix-L -o '#{iso_file}' -V 'Backup #{number}' -R -J '#{tmp_dir}/'"
puts "Executing `#{command}`"
system command
puts $?
index(files, @counter) if $? == 0
FileUtils.rm_r(tmp_dir)
end
protected
def mark(files)
files2 = files.map {|f| f.sub(File.basename(f), ".#{File.basename(f)}.lrbackup") }
FileUtils.touch files2
end
def marked(file)
File.exists? file.sub(File.basename(file), ".#{File.basename(file)}.lrbackup")
end
def index(files, number)
mark files
File.open(File.join(@output, "index.txt"), "a") do |f|
f.puts "CD \#%d" % number
files.each do |file|
f.puts "\t #{file}"
end
end
end
def ask_done
if @interactive
puts "Record ISO now"
STDIN.gets
end
true
end
def size(file)
File.size(file) / 1024.0 / 1024.0
end
def log(message)
puts message if @interactive
end
end
end
catalog_path = ARGV[0]
isos_path = Dir.pwd
backup_task = Lightroom::Backup.new(catalog_path, isos_path)
if ARGV[1]
backup_task.counter = ARGV[1].to_i
end
Lightroom::Backup.rm_mark(catalog_path) if backup_task.counter == 1
backup_task.size = 650
backup_task.interactive = true
backup_task.do!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment