Class: Xen::Backup

Inherits:
Object
  • Object
show all
Includes:
Parentable
Defined in:
lib/xen/backup.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Parentable

#slice

Constructor Details

#initialize(*args) ⇒ Backup

Returns a new instance of Backup.



51
52
53
54
55
# File 'lib/xen/backup.rb', line 51

def initialize(*args)
  options = args.extract_options!
  @name = options[:name]
  @version = options[:version]
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/xen/backup.rb', line 5

def name
  @name
end

#versionObject

Returns the value of attribute version.



5
6
7
# File 'lib/xen/backup.rb', line 5

def version
  @version
end

Class Method Details

.create(*args) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/xen/backup.rb', line 7

def self.create(*args)
  options = args.extract_options!
  name = args.first
  
  # options = {}
  # name = 'foo' # XXX replace with real value
  version = options[:version] || Time.now.strftime('%Y%m%d')
  backup_dir = options[:backup_dir] || Xen::BACKUP_DIR
  backup_file_ext = options[:backup_file_ext] || Xen::BACKUP_FILE_EXT
  archive_name="#{name}-#{version}#{backup_file_ext}"
        
  slice = Xen::Slice.find(name) # XXX test for failure
  if slice.running?
    slice.stop
    sleep 10
    restart_slice = true
  end

  temp_mount = `mktemp -d -p /mnt #{name}-XXXXX`.chomp # XXX test for failure
  `mount #{slice.root_disk.path} #{temp_mount}` # XXX test for failure
  
  

  FileUtils.mkdir_p backup_dir
  
  # Creating archive at backup_dir/archive_name ...
  excludes_file = File.join(File.dirname(__FILE__),'..','templates','exclude_from_backups')
  temp_tarball = `mktemp -p #{backup_dir} #{name}-XXXXX`.chomp # XXX test for failure
  `tar --create --exclude-from=#{excludes_file} --directory #{temp_mount} --file #{temp_tarball} . && mv #{temp_tarball} #{backup_dir}/#{archive_name}`
  
  # Unmounting image
  `umount #{temp_mount}`
  Dir.delete(temp_mount)

  # Creating symlink from new backup to filename without version number
  last_backup = "#{backup_dir}/#{name}#{backup_file_ext}"
  File.delete(last_backup) if File.symlink?(last_backup)
  `ln -sf #{backup_dir}/#{archive_name} #{last_backup}`
  
  slice.start if restart_slice == true
  
  new(:name => name, :version => version)
end

.find(*args) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'lib/xen/backup.rb', line 57

def self.find(*args)
  # return all
  slice = args.first
  Dir.glob("#{Xen::BACKUP_DIR}/*-*#{Xen::BACKUP_FILE_EXT}").collect { |file|
    if match = File.basename(file, Xen::BACKUP_FILE_EXT).match(/(#{ slice || '.*' })-(.*)/)
      new(:name => match[1], :version => match[2])
    end
  }.compact
end

Instance Method Details

#filenameObject



67
68
69
# File 'lib/xen/backup.rb', line 67

def filename
  "#{@name}-#{@version}#{Xen::BACKUP_FILE_EXT}"
end

#fullpathObject



71
72
73
# File 'lib/xen/backup.rb', line 71

def fullpath
  File.join(Xen::BACKUP_DIR, filename)
end

#sizeObject



75
76
77
# File 'lib/xen/backup.rb', line 75

def size
  File.size(fullpath)
end