-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysql_backup.rb
77 lines (52 loc) · 2.11 KB
/
mysql_backup.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
# encoding: utf-8
require 'mail'
require 'open3'
#create the db dump Files
def make_db_dump
dump_production = 'mysqldump --user=backup-user --password=password --databases db_name > /db_backup/db_name.sql &&'
dump_production << 'mysqldump --user=backup-user --password=password --databases db_name > /db_backup/db_name.sql &&'
dump_production << 'mysqldump --user=backup-user --password=password --databases db_name > /db_backup/db_name.sql &&'
dump_production << 'mysqldump --user=backup-user --password=password --databases db_name > /db_backup/db_name.sql'
stdin, stdout, stderr = Open3.popen3(dump_production)
error_msg = stderr.readlines
send_mail(error_msg) unless error_msg.blank?
compress
end
def compress
compress_production = "cd db_backup;"
compress_production << "tar -zcf zip/db_name.sql.tar.gz db_name.sql &&"
compress_production << "tar -zcf zip/db_name.sql.tar.gz db_name.sql &&"
compress_production << "tar -zcf zip/db_name.sql.tar.gz db_name.sql &&"
compress_production << "tar -zcf zip/db_name.sql.tar.gz db_name.sql"
stdin, stdout, stderr = Open3.popen3(compress_production)
error_msg = stderr.readlines
send_mail(error_msg) unless error_msg.blank?
send_to_backup_srv
end
def send_to_backup_srv
gentoo_server = "user@host:/target/folder"
send_at_home = "rsync -urlpgt /db_backup/zip #{gentoo_server}"
stdin, stdout, stderr = Open3.popen3(send_at_home)
error_msg = stderr.readlines
send_mail(error_msg) unless error_msg.blank?
end
def send_mail(error_msg)
options = { :address => "your_mail_host",
:port => 25,
:domain => "your_mail_domain",
:authentication => :login,
:user_name => "[email protected]",
:password => "password",
:openssl_verify_mode => 'none' }
Mail.defaults do
delivery_method :smtp, options
end
mail = Mail.new do
from '[email protected]'
to '[email protected]'
subject 'DB Backup Failed'
body error_msg
end
mail.deliver!
end
make_db_dump