Class: Capistrano::Former03::Backup

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

Instance Method Summary collapse

Constructor Details

#initializeBackup

Returns a new instance of Backup.



6
7
# File 'lib/capistrano/former03/backup.rb', line 6

def initialize
end

Instance Method Details

#cleanup(t) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/capistrano/former03/backup.rb', line 9

def cleanup(t)
  # minimum age in days to keep backups
  backup_min_age = fetch(:backup_min_age, 7)
  # minimum backup count to keep
  backup_min_count = fetch(:backup_min_count, 5)

  on release_roles :all do
    backup_dir_path = fetch(:backup_dir_path)
    backups = capture(:ls, '-xtr', backup_dir_path).split
    count = 0

    # Loop through existing backups
    backups.reverse.each do |backup|
      count += 1
      created = DateTime.strptime(backup, '%Y%m%d%H%M%S')
      age = (DateTime.now - created)

      # If backup is older and count is higher than minimums
      if age.to_i > backup_min_age and count > backup_min_count
        execute 'rm', '-rf', File.join(backup_dir_path, backup)
      end
    end
  end
end

#prepare(t) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/capistrano/former03/backup.rb', line 34

def prepare(t)
  t.set(:backup_dir_path, File.join(t.fetch(:deploy_to), t.fetch(:backup_dir, 'backups')))
  on release_roles :all do
    # set destination path
    t.set(
      :backup_dest_path,
      File.join(
        fetch(:backup_dir_path),
        fetch(:release_timestamp,'not_time')
      )
    )
    # ensure directories
    [fetch(:backup_dir_path)].each do |dir|
      if not test("test -d #{dir}")
        execute :mkdir, dir
        execute :chmod, '0700', dir
      end
    end
  end
end