Class: WpBackup::Backup

Inherits:
Object
  • Object
show all
Defined in:
lib/wp_backup/backup.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Backup

Returns a new instance of Backup.



4
5
6
# File 'lib/wp_backup/backup.rb', line 4

def initialize(config)
  @config = config
end

Instance Method Details

#create(backup_db = true, backup_site = true) ⇒ Object



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
# File 'lib/wp_backup/backup.rb', line 8

def create(backup_db=true, backup_site=true)
  timestamp = Time.now.utc.strftime("%Y%m%d%H%M%S")
  root_dir = "/tmp/wp-backup-#{timestamp}"
  package_file = "/tmp/wp-backup-#{timestamp}.tar.gz"

  begin
    dirs = []

    if backup_site
      site_dir = File.join(root_dir, 'site')
      FileUtils.mkdir_p(site_dir)
      site_file = @config.site.dump_to(File.join(root_dir, 'site', 'site.tar'))
      dirs << 'site'
    end

    if backup_db
      db_dir = File.join(root_dir, 'db')
      FileUtils.mkdir_p(db_dir)
      db_file = @config.database.dump_to(File.join(db_dir, 'db.sql'))
      dirs << 'db'
    end

    `cd #{root_dir} && tar -czf #{package_file} #{dirs.join(' ')}`

    @config.s3.store(package_file)
  ensure
    `rm -rf #{root_dir} #{package_file}`
  end

  timestamp
end

#restore(key) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/wp_backup/backup.rb', line 40

def restore(key)
  timestamp = Time.now.utc.strftime("%Y%m%d%H%M%S")
  root_dir = "/tmp/wp-restore-#{timestamp}"

  FileUtils.mkdir_p(root_dir)

  begin
    File.open(File.join(root_dir, 'restore.tar.gz'), 'wb') do |file|
      file.write(@config.s3.read(key))
    end

    `cd #{root_dir} && tar -xzf restore.tar.gz`

    db_file = File.join(root_dir, 'db', 'db.sql')
    if File.exists?(db_file)
      @config.database.restore_from(db_file)
    end

    site_file = File.join(root_dir, 'site', 'site.tar')
    if File.exists?(site_file)
      @config.site.restore_from(site_file)
    end
  ensure
    `rm -rf #{root_dir}`
  end
end