-
Notifications
You must be signed in to change notification settings - Fork 59
/
sync.rb
197 lines (150 loc) · 7.29 KB
/
sync.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
require 'pathname'
before "genesis:up:db" do
set(:confirmed) do
logger.important <<-WARN
========================================================================
WARNING: You are about to destroy & override the "#{stage}" database!
========================================================================
WARN
answer = Capistrano::CLI.ui.ask " Are you sure you want to continue? (YES) "
if answer === 'YES' then true else false end
end
unless fetch(:confirmed)
logger.info "\Aborted!"
exit
end
end
before "genesis:up:files" do
set(:confirmed) do
logger.important <<-WARN
========================================================
WARNING: You are about to override "#{stage}" files!
========================================================
WARN
answer = Capistrano::CLI.ui.ask " Are you sure you want to continue? (YES) "
if answer === 'YES' then true else false end
end
unless fetch(:confirmed)
logger.info "\Aborted!"
exit
end
end
before "genesis:up:mirror" do
set(:confirmed) do
logger.important <<-WARN
========================================================
WARNING: You are about to DESTRUCTIVELY override "#{stage}" files!
========================================================
WARN
answer = Capistrano::CLI.ui.ask " Are you sure you want to continue? (YES) "
if answer === 'YES' then true else false end
end
unless fetch(:confirmed)
logger.info "\Aborted!"
exit
end
end
namespace :genesis do
namespace :down do
desc "Downloads both remote database & syncs remote files into Vagrant"
task :default do
db
files
end
desc "Downloads remote database into Vagrant"
task :db, :roles => :db, :once => true do
find_and_execute_task "genesis:backup:db"
# Rake::Task["namespace:task"].invoke
system "gzip -d #{local_backup_dir}/#{backup_name}.gz"
system "vagrant up"
system "vagrant ssh local -c 'cd /vagrant && mysql -uroot < #{local_backup_dir}/#{backup_name}'"
system "rm -f #{local_backup_dir}/#{backup_name}"
end
desc "Downloads remote files to Vagrant"
task :files, :roles => :web do
set :excludes, "--exclude '#{rsync_exclude.join('\' --exclude \'')}'"
ssh = "-e \"ssh -i #{ssh_options[:keys][0]}\"" unless ssh_options.keys.empty?
find_servers_for_task(current_task).each do |current_server|
system "chmod 600 #{ssh_options[:keys][0]}" unless ssh_options.keys.empty?
system "rsync #{ssh} -avvru --delete --copy-links #{excludes} --progress #{'--dry-run' if dry_run} #{user}@#{current_server}:#{remote_web}/ #{local_web}/"
end
end
desc "Downloads limited dirs to Vagrant"
task :limited, :roles => :web do
set :excludes, "--exclude '#{rsync_exclude.join('\' --exclude \'')}'"
ssh = "-e \"ssh -i #{ssh_options[:keys][0]}\"" unless ssh_options.keys.empty?
find_servers_for_task(current_task).each do |current_server|
system "chmod 600 #{ssh_options[:keys][0]}" unless ssh_options.keys.empty?
rsync_limited.each do |key|
system "rsync #{ssh} -avvru --delete --copy-links #{excludes} --progress #{'--dry-run' if dry_run} #{user}@#{current_server}:#{remote_web}/#{key}/ #{local_web}/#{key}/"
end
end
end
end
namespace :up do
desc "Uploads Vagrant database & local files into production"
task :default do
db
files
end
desc "Uploads Vagrant database into remote"
task :db, :roles => :db, :once => true do
find_and_execute_task "genesis:backup:db"
sleep(1)
set :backup_dir, "#{deploy_to}/backups"
set :backup_name, DateTime.now.strftime("#{db_name}.%Y-%m-%d.%H%M%S.sql")
set :backup_path, "#{backup_dir}/#{backup_name}"
system "vagrant up"
system "vagrant ssh local -c 'cd /vagrant && mysqldump -u\"#{db_user}\" -p\"#{db_password.gsub('$', '\$')}\" --opt --databases \"#{db_name}\" | gzip --rsyncable > #{backup_name}.gz'"
run "mkdir -p #{backup_dir}"
top.upload "#{backup_name}.gz", "#{backup_path}.gz", :via => :scp
run "gzip -d #{backup_path}.gz"
system "rm -f #{backup_name}.gz"
run "sudo mysql -uroot < #{backup_path}"
end
desc "Uploads local project files to remote"
task :files, :roles => :web do
set :excludes, "--exclude '#{rsync_exclude.join('\' --exclude \'')}'"
find_servers_for_task(current_task).each do |current_server|
system "chmod 600 #{ssh_options[:keys][0]}" unless ssh_options.keys.empty?
system "rsync -e \"ssh -i #{ssh_options[:keys][0]}\" -avvru --keep-dirlinks #{excludes} --progress #{'--dry-run' if dry_run} #{local_web}/ #{user}@#{current_server}:#{remote_web}/"
end
end
desc "Destructively syncs local project files to remote"
task :mirror, :roles => :web do
set :excludes, "--exclude '#{rsync_exclude.join('\' --exclude \'')}'"
find_servers_for_task(current_task).each do |current_server|
system "chmod 600 #{ssh_options[:keys][0]}" unless ssh_options.keys.empty?
system "rsync -e \"ssh -i #{ssh_options[:keys][0]}\" -avvru --delete --keep-dirlinks #{excludes} --progress #{'--dry-run' if dry_run} #{local_web}/ #{user}@#{current_server}:#{remote_web}/"
end
end
desc "Uploads limited dirs to remote"
task :limited, :roles => :web do
set :excludes, "--exclude '#{rsync_exclude.join('\' --exclude \'')}'"
find_servers_for_task(current_task).each do |current_server|
system "chmod 600 #{ssh_options[:keys][0]}" unless ssh_options.keys.empty?
rsync_limited.each do |key|
system "rsync -e \"ssh -i #{ssh_options[:keys][0]}\" -avvru --keep-dirlinks #{excludes} --progress #{'--dry-run' if dry_run} #{local_web}/#{key}/ #{user}@#{current_server}:#{remote_web}/#{key}/"
end
end
end
end
namespace :backup do
desc "Downloads remote database into the backups folder"
task :default do
db
end
desc "Downloads remote database into the backups folder"
task :db, :roles => :db, :once => true do
set :backup_dir, "#{deploy_to}"
set :local_backup_dir, "backups"
set :backup_name, DateTime.now.strftime("#{db_name}.%Y-%m-%d.%H%M%S.sql")
set :backup_path, "#{backup_dir}/#{backup_name}"
run "mkdir -p #{local_backup_dir}"
run "mysqldump -u'#{db_user}' -p'#{db_password}' -h'#{db_host}' --opt --databases '#{db_name}' | gzip --rsyncable > #{backup_path}.gz"
system "mkdir -p #{local_backup_dir}"
download "#{backup_path}.gz", "#{local_backup_dir}/#{backup_name}.gz", :via => :scp
run "rm -f #{backup_path}.gz"
end
end
end