bedrock-capistrano-mysql 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 39d7193472b5304f1d39c09d61c3cf9153b3aca6
4
- data.tar.gz: 1b4b058fd3809b1957e5a9e0704bcbd806c11bdb
3
+ metadata.gz: 8c884389597d8f5e65ee9b39266cada0e0c34cd4
4
+ data.tar.gz: 180bcda175b002d7cbd674c7e1a106934cc8f940
5
5
  SHA512:
6
- metadata.gz: 689e9445004dd470c356605c5cf49a0c2b2f720a6d02bd31c5b1d2e14b3f56cad79d350064198c7ed344c75e39010d6a68102027ae43c7e8338fa44d8514bcf9
7
- data.tar.gz: 4e0d43765f0f657aed1ffa4430d87e3a15b6803b973c094534af1cfab2b94049781fe9ed988dd809918e013c9626ab99ac82b09e3be828d28d120518e815f1ce
6
+ metadata.gz: dafaef49fa10f0c7cbf564010cb01d2798691a3894dd4a6d3338f374363a7f84be593dec759125edf40e28eb20e554d08fa0271204a47378b56a9c86a5bfdd47
7
+ data.tar.gz: b83b079fbb34c6e224b1abd96a4a06dc37041449d8f3f3f6b7fcb95bc967f447cb00e9b4c4cc986192dca4bb5fe8c0439e376a2dc3bc7256815ae717e2885d66
data/.gitignore CHANGED
@@ -1,2 +1,4 @@
1
1
  # Mac OS X
2
2
  .DS_Store
3
+
4
+ *.gem
data/README.md CHANGED
@@ -16,6 +16,8 @@ Or install it yourself as:
16
16
 
17
17
  $ gem install bedrock-capistrano-mysql
18
18
 
19
+ You also need to add the line VAGRANT_PATH to your local .env file, pointing to your local `bedrock-ansible` project. You also to keep an .env-file for each environment. Meaning you need .env for your local environment, and one for each additional environment you want to work with – i.e. .env.staging, .env.production and so on...
20
+
19
21
  ## Usage
20
22
 
21
23
  Require the module in your `Capfile`:
@@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__)
3
3
 
4
4
  Gem::Specification.new do |spec|
5
5
  spec.name = 'bedrock-capistrano-mysql'
6
- spec.version = '0.0.1'
6
+ spec.version = '0.0.2'
7
7
  spec.authors = ['Fredrik Sundström']
8
8
  spec.email = ['fredrik.sundstrom@norkay.se']
9
9
  spec.description = %q{MySQL tasks for roots/bedrock, using Capistrano 3.x}
@@ -1,15 +1,102 @@
1
1
  require 'dotenv'
2
+ require 'fileutils'
2
3
 
3
4
  namespace(:mysql) do
4
- desc "Backup MySQL Database"
5
- task :backup do
6
- on roles(:all) do
7
- timestamp = Time.now.strftime('%Y%m%d%H%M%S')
8
- text = capture "cat #{shared_path}/.env"
9
- ENV2 = Dotenv::Parser.call(text)
10
- execute "mkdir -p #{shared_path}/db_backups"
11
- execute "mysqldump -h #{ENV2['DB_HOST']} -u#{ENV2['DB_USER']} -p#{ENV2['DB_PASSWORD']} #{ENV2['DB_NAME']} > #{shared_path}/db_backups/#{timestamp}.sql"
12
- execute "(cd #{shared_path}/db_backups; ls -1tr | head -n -5 | xargs -d '\\n' rm)" # Keep only five latest
13
- end
5
+ task :before do
6
+ on roles(:db) do
7
+ TIMESTAMP = Time.now.strftime('%Y%m%d%H%M%S')
8
+ text = capture "cat #{shared_path}/.env"
9
+ REMOTE_ENV = Dotenv::Parser.call(text)
10
+ file = File.open(".env")
11
+ text = file.read
12
+ LOCAL_ENV = Dotenv::Parser.call(text)
14
13
  end
14
+ end
15
+ task :backup => :before
16
+ task :download => :before
17
+ task :change_url => :before
18
+ task :import => :before
19
+ task :sync => [:backup, :download, :change_url, :import]
20
+
21
+ desc "Backup MySQL Database"
22
+ task :backup do
23
+ on roles(:db) do
24
+ create_dir("#{shared_path}/db_backups")
25
+ create_backup(
26
+ REMOTE_ENV['DB_HOST'],
27
+ REMOTE_ENV['DB_USER'],
28
+ REMOTE_ENV['DB_PASSWORD'],
29
+ REMOTE_ENV['DB_NAME'],
30
+ TIMESTAMP
31
+ )
32
+ remove_old_backups("#{shared_path}/db_backups")
33
+ end
34
+ end
35
+
36
+ desc "Download the last backuped MySQL Database"
37
+ task :download do
38
+ on roles(:db) do
39
+ filename = get_latest_backup_filename("#{shared_path}/db_backups")
40
+ download! "#{shared_path}/db_backups/#{filename}", "etc/db_backups/remote.sql"
41
+ end
42
+ end
43
+
44
+ desc "Change the urls in the MySQL backup file"
45
+ task :change_url do
46
+ on roles(:db) do
47
+ change_urls_in_file("etc/db_backups/remote.sql", REMOTE_ENV['WP_HOME'], LOCAL_ENV['WP_HOME'])
48
+ puts "Changed urls in file!"
49
+ end
50
+ end
51
+
52
+ desc "Clean and import the remote MySQL database to the local one"
53
+ task :import do
54
+ on roles(:db) do
55
+ import_local_mysql_database("etc/db_backups/remote.sql")
56
+ puts "Database imported!"
57
+ end
58
+ end
59
+ end
60
+
61
+ private
62
+
63
+ def create_dir(path)
64
+ execute "mkdir -p #{path}"
65
+ end
66
+
67
+ def create_backup(host, user, password, name, timestamp)
68
+ execute "mysqldump -h #{host} -u#{user} -p#{password} #{name} > #{shared_path}/db_backups/#{timestamp}.sql"
69
+ end
70
+
71
+ def remove_old_backups(path)
72
+ execute "(cd #{path}; ls -1tr | head -n -5 | xargs -d '\\n' rm)" # Keep only five latest
73
+ end
74
+
75
+ def get_latest_backup_filename(path)
76
+ filename = capture "cd #{path}; ls -t | awk '{printf(\"%s\", $0); exit}'"
77
+ filename
78
+ end
79
+
80
+ def change_urls_in_file(filename, replace, with)
81
+ text = File.read(filename)
82
+ new_contents = text.gsub(replace, with)
83
+ File.open(filename, "w") {|file| file.puts new_contents }
84
+ end
85
+
86
+ def local_mysql_credentials
87
+ credential_params = ""
88
+
89
+ credential_params << " -u #{LOCAL_ENV['DB_USER']} " if LOCAL_ENV['DB_USER']
90
+ credential_params << " -p'#{LOCAL_ENV['DB_PASSWORD']}' " if LOCAL_ENV['DB_USER']
91
+ credential_params << " -h #{LOCAL_ENV['DB_HOST']} " if LOCAL_ENV['DB_HOST']
92
+
93
+ credential_params
94
+ end
95
+
96
+ def local_mysql_database
97
+ LOCAL_ENV['DB_NAME']
98
+ end
99
+
100
+ def import_local_mysql_database(filename)
101
+ system "cd ..; cd #{LOCAL_ENV['VAGRANT_PATH']}; vagrant ssh -c 'mysql #{local_mysql_credentials} -D #{local_mysql_database} < /srv/www/#{fetch(:application)}/current/#{filename}'"
15
102
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bedrock-capistrano-mysql
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fredrik Sundström
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-18 00:00:00.000000000 Z
11
+ date: 2015-05-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: capistrano