forked from frankh/docker-compose-mailbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestore.rb
executable file
·105 lines (95 loc) · 3.68 KB
/
restore.rb
1
2
3
4
5
6
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/ruby
require 'optparse'
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: ./restore.rb [options]"
opts.on("-vm", "--vmail-backup", "vmail backup file") do |vm|
options[:vmail_backup] = vm
end
opts.on("-rc", "--roundcube-backup", "roundcube backup file") do |rc|
options[:roundcube_backup] = rc
end
opts.on("-va", "--vimbadmin-backup", "vimbadmin backup file") do |va|
options[:vimbadmin_backup] = va
end
opts.on("-h", "--help", "print this help message") do |vb|
puts opts
exit
end
end.parse!
vmail_backup = options[:vmail_backup]
roundcube_backup = options[:roundcube_backup]
vimbadmin_backup = options[:vimbadmin_backup]
unless vmail_backup
vmail_backups = Dir["data/backup/vmail_backup*.tar.gz"].sort.reverse
if vmail_backups.length == 0
STDERR.puts "No vmail backups found, provide .tar.gz backup manually with -vm"
exit 1
end
puts "Select which vmail backup to use (or specify manually with --vmail-backup)"
vmail_backups.first(5).each_with_index do |filename, i|
puts " #{i+1}: #{filename}"
end
choice = STDIN.gets.chomp.to_i
if choice > vmail_backups.first(5).length || choice < 1
puts "Invalid selection - not skipping vmail restore"
end
vmail_backup = vmail_backups[choice-1]
end
unless roundcube_backup
roundcube_backups = Dir["data/backup/db_roundcube_backup*.sql"].sort.reverse
if roundcube_backups.length == 0
STDERR.puts "No roundcube backups found, provide .sql backup manually with -rc"
exit 1
end
puts "Select which roundcube backup to use (or specify manually with --roundcube-backup)"
roundcube_backups.first(5).each_with_index do |filename, i|
puts " #{i+1}: #{filename}"
end
choice = STDIN.gets.chomp.to_i
if choice > roundcube_backups.first(5).length || choice < 1
puts "Invalid selection - not skipping roundcube restore"
roundcube_backups = {}
end
roundcube_backup = roundcube_backups[choice-1]
end
unless vimbadmin_backup
vimbadmin_backups = Dir["data/backup/db_vimbadmin_backup*.sql"].sort.reverse
if vimbadmin_backups.length == 0
STDERR.puts "No vimbadmin backups found, provide .sql backup manually with -va"
exit 1
end
puts "Select which vimbadmin backup to use (or specify manually with --vimbadmin-backup)"
vimbadmin_backups.first(5).each_with_index do |filename, i|
puts " #{i+1}: #{filename}"
end
choice = STDIN.gets.chomp.to_i
if choice > vimbadmin_backups.first(5).length || choice < 1
puts "Invalid selection - not skipping vimbadmin restore"
vimbadmin_backups = {}
end
vimbadmin_backup = vimbadmin_backups[choice-1]
end
puts "Using vmail backup \"#{vmail_backup}\""
puts "Using roundcube backup \"#{roundcube_backup}\""
puts "Using vimbadmin backup \"#{vimbadmin_backup}\""
storage_box = `docker-compose ps storagebackup | grep storagebackup | cut -d' ' -f1`.chomp
exit 1 unless storage_box
puts "Found storage box '#{storage_box}'"
print "\nReady to restore, continue? <y/N>"
exit 1 unless STDIN.gets.chomp.downcase == "y"
puts "(1/4) Restoring mail files..."
`docker exec -i #{storage_box} tar -xzv -C /var/vmail < #{vmail_backup}`
`docker exec -i #{storage_box} rm /var/vmail/\\*/\\*/\\*.cache`
`docker exec -i #{storage_box} rm /var/vmail/\\*/\\*/\\*.index`
puts "(2/4) Fixing permissions..."
`docker exec -i #{storage_box} chown -R vmail:vmail /var/vmail`
if roundcube_backup
puts "(3/4) Restoring roundcube DB..."
`docker exec -i #{storage_box} mysql -u roundcube -ppassword -h mysql roundcube < #{roundcube_backup} 2>/dev/null`
end
if vimbadmin_backup
puts "(4/4) Restoring vimbadmin DB..."
`docker exec -i #{storage_box} mysql -u vimbadmin -ppassword -h mysql vimbadmin < #{vimbadmin_backup} 2>/dev/null`
end
puts "Done!"