Commit 1a555b8832267edbead0c65812914d3e498fb07a
Committed by
Earth Ugat
1 parent
1f8f44d7
Add needed recipe reload_from_s3 and fix derived attribute pitfalls
Showing
7 changed files
with
129 additions
and
21 deletions
| ... | ... | @@ -72,6 +72,18 @@ Ubuntu 14.04 |
| 72 | 72 | <td>The backup cronjob 'day of week' value</td> |
| 73 | 73 | <td><tt>'*'</tt></td> |
| 74 | 74 | </tr> |
| 75 | + <tr> | |
| 76 | + <td><tt>['cfe-mariadb']['reload']['aws_access_key_id']</tt></td> | |
| 77 | + <td>String</td> | |
| 78 | + <td>If not using EC2 roles, enter AWS creds here</td> | |
| 79 | + <td><tt>nil</tt></td> | |
| 80 | + </tr> | |
| 81 | + <tr> | |
| 82 | + <td><tt>['cfe-mariadb']['reload']['aws_secret_access_key']</tt></td> | |
| 83 | + <td>String</td> | |
| 84 | + <td>If not using EC2 roles, enter AWS creds here</td> | |
| 85 | + <td><tt>nil</tt></td> | |
| 86 | + </tr> | |
| 75 | 87 | </table> |
| 76 | 88 | |
| 77 | 89 | ## Usage |
| ... | ... | @@ -100,6 +112,12 @@ This will install the backup script and also enable a cronjob to regularly run t |
| 100 | 112 | ] |
| 101 | 113 | } |
| 102 | 114 | ``` |
| 115 | + | |
| 116 | +### cfe-mariadb::reload_from_s3 | |
| 117 | + | |
| 118 | +Kind of the reverse of `backup2s3`. Download a tarball of a MySQL dump file from an S3 bucket, then load it up into a database. Do this for every database given in `node['cfe-mariadb']['db_map']`. | |
| 119 | + | |
| 120 | +This recipe assumes the node is using an EC2 role that can access the given S3 bucket. Otherwise, enter the AWS credentials in `node.default['cfe-mariadb']['reload']['aws_access_key_id']` and `node.default['cfe-mariadb']['reload']['aws_secret_access_key']`. | |
| 103 | 121 | ## License and Authors |
| 104 | 122 | |
| 105 | 123 | Author:: Earth U. (<sysadmin@chromedia.com>) | ... | ... |
| ... | ... | @@ -30,20 +30,13 @@ |
| 30 | 30 | # :collate => 'latin1_swedish_ci' |
| 31 | 31 | # } |
| 32 | 32 | # } |
| 33 | -default['cfe-mariadb']['db_map'] = { | |
| 34 | - 'example_db' => { | |
| 35 | - :db_user => 'example_user', | |
| 36 | - :db_pass => 'secret', | |
| 37 | - :bak_filename => 'example_db.sql', | |
| 38 | - :bak_maxcopies => 30 | |
| 39 | - } | |
| 40 | -} | |
| 33 | +default['cfe-mariadb']['db_map'] = {} | |
| 41 | 34 | |
| 42 | 35 | # Make sure to set replication on only when necessary |
| 43 | 36 | default['cfe-mariadb']['replication'] = false |
| 44 | 37 | |
| 45 | -default['cfe-mariadb']['backup']['s3_region'] = 'us-east-1' | |
| 46 | -default['cfe-mariadb']['backup']['s3_bucket'] = 'example-bucket' | |
| 38 | +default['cfe-mariadb']['s3_region'] = 'us-east-1' | |
| 39 | +default['cfe-mariadb']['s3_bucket'] = 'example-bucket' | |
| 47 | 40 | |
| 48 | 41 | if node['platform'] == 'ubuntu' and node['platform_version'].to_f == 14.04 |
| 49 | 42 | default['cfe-mariadb']['backup']['aws_bin'] = '/usr/local/bin/aws' |
| ... | ... | @@ -53,10 +46,11 @@ else |
| 53 | 46 | default['cfe-mariadb']['backup']['aws_bin'] = '/usr/local/bin/aws' |
| 54 | 47 | default['cfe-mariadb']['backup']['mysqldump_bin'] = '/usr/bin/mysqldump' |
| 55 | 48 | end |
| 56 | -# Path to directory where the backup script should be placed | |
| 57 | -default['cfe-mariadb']['backup']['script_dir'] = ::File.join( | |
| 58 | - node['mariadb']['configuration']['path'], 'scripts' | |
| 59 | -) | |
| 49 | +# Path to directory where the backup script should be placed. | |
| 50 | +# Uncomment to set custom locations. | |
| 51 | +#default['cfe-mariadb']['backup']['script_dir'] = ::File.join( | |
| 52 | +# node['mariadb']['configuration']['path'], 'scripts' | |
| 53 | +#) | |
| 60 | 54 | |
| 61 | 55 | default['cfe-mariadb']['backup']['cron']['min'] = '0' |
| 62 | 56 | default['cfe-mariadb']['backup']['cron']['hour'] = '0' |
| ... | ... | @@ -67,9 +61,19 @@ default['cfe-mariadb']['backup']['cron']['mailto'] = "''" # Empty single quotes |
| 67 | 61 | |
| 68 | 62 | # Basic options for logrotate |
| 69 | 63 | default['cfe-mariadb']['backup']['logrotate']['conf_dir'] = '/etc/logrotate.d' |
| 70 | -default['cfe-mariadb']['backup']['logrotate']['options'] = [ | |
| 71 | - 'weekly', 'rotate 12', 'missingok', 'compress', 'notifempty' | |
| 72 | -] | |
| 64 | +default['cfe-mariadb']['backup']['logrotate']['options'] = %w{ | |
| 65 | + weekly | |
| 66 | + rotate\ 12 | |
| 67 | + missingok | |
| 68 | + compress | |
| 69 | + notifempty | |
| 70 | +} | |
| 71 | + | |
| 72 | +# Optional attributes for recipe 'reload_from_s3' | |
| 73 | +# Recommend: use EC2 instances with proper S3 access roles and | |
| 74 | +# leave these attributes unset: | |
| 75 | +#default['cfe-mariadb']['reload']['aws_access_key_id'] = 'MYKEYID' | |
| 76 | +#default['cfe-mariadb']['reload']['aws_secret_access_key'] = 'MYSECRETKEY' | |
| 73 | 77 | |
| 74 | 78 | default['mariadb']['server_root_password'] = 'secretpassword' |
| 75 | 79 | default['mariadb']['mysqld']['bind_address'] = '127.0.0.1' | ... | ... |
| ... | ... | @@ -4,7 +4,7 @@ maintainer_email 'sysadmin@chromedia.com' |
| 4 | 4 | license 'Apache License' |
| 5 | 5 | description 'Simplifies setup of MariaDB in Chromedia.' |
| 6 | 6 | long_description IO.read(File.join(File.dirname(__FILE__), 'README.md')) |
| 7 | -version '0.1.0' | |
| 7 | +version '0.1.1' | |
| 8 | 8 | |
| 9 | 9 | { |
| 10 | 10 | 'mariadb' => '0.2.12', | ... | ... |
| ... | ... | @@ -22,6 +22,11 @@ |
| 22 | 22 | # and uploads them to a S3 bucket. |
| 23 | 23 | # Also sets up the cron job to regularly run this script. |
| 24 | 24 | |
| 25 | +# Set derived attributes inside recipe | |
| 26 | +node.default['cfe-mariadb']['backup']['script_dir'] = | |
| 27 | + ::File.join(node['mariadb']['configuration']['path'], 'scripts') unless | |
| 28 | + node['cfe-mariadb']['backup']['script_dir'] | |
| 29 | + | |
| 25 | 30 | include_recipe 'awscli' |
| 26 | 31 | |
| 27 | 32 | md = node['cfe-mariadb'] |
| ... | ... | @@ -35,8 +40,8 @@ template "#{mdb['script_dir']}/backup_db_to_s3" do |
| 35 | 40 | :db_map => md['db_map'], |
| 36 | 41 | :db_ip => node['mariadb']['mysqld']['bind_address'], |
| 37 | 42 | :db_port => node['mariadb']['mysqld']['port'], |
| 38 | - :s3_region => mdb['s3_region'], | |
| 39 | - :s3_bucket => mdb['s3_bucket'], | |
| 43 | + :s3_region => md['s3_region'], | |
| 44 | + :s3_bucket => md['s3_bucket'], | |
| 40 | 45 | :aws_bin => mdb['aws_bin'], |
| 41 | 46 | :mysqldump_bin => mdb['mysqldump_bin'] |
| 42 | 47 | ) | ... | ... |
recipes/reload_from_s3.rb
0 → 100644
| 1 | +# | |
| 2 | +# Author:: Earth U (<sysadmin@chromedia.com>) | |
| 3 | +# Cookbook Name:: cfe-mariadb | |
| 4 | +# Recipe:: reload_from_s3 | |
| 5 | +# | |
| 6 | +# Copyright 2016, Chromedia Far East, Inc. | |
| 7 | +# | |
| 8 | +# Licensed under the Apache License, Version 2.0 (the "License"); | |
| 9 | +# you may not use this file except in compliance with the License. | |
| 10 | +# You may obtain a copy of the License at | |
| 11 | +# | |
| 12 | +# http://www.apache.org/licenses/LICENSE-2.0 | |
| 13 | +# | |
| 14 | +# Unless required by applicable law or agreed to in writing, software | |
| 15 | +# distributed under the License is distributed on an "AS IS" BASIS, | |
| 16 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 17 | +# See the License for the specific language governing permissions and | |
| 18 | +# limitations under the License. | |
| 19 | +# | |
| 20 | + | |
| 21 | +# Download a tarball of a MySQL dump from an S3 bucket, | |
| 22 | +# then load it up into a (preferably empty) database. | |
| 23 | + | |
| 24 | +include_recipe 'awscli' | |
| 25 | + | |
| 26 | +tmp_dir = ::File.join(Chef::Config[:file_cache_path], 'db_dumps') | |
| 27 | +manual_creds = node['cfe-mariadb'].has_key?('reload') && | |
| 28 | + node['cfe-mariadb']['reload'].has_key?('aws_access_key_id') | |
| 29 | + | |
| 30 | +node['cfe-mariadb']['db_map'].each do |dbx| | |
| 31 | + | |
| 32 | + if dbx.is_a?(Array) | |
| 33 | + dbx_name = dbx[0] | |
| 34 | + dbx = dbx[1] | |
| 35 | + else | |
| 36 | + dbx_name = dbx[:db_name] | |
| 37 | + end | |
| 38 | + | |
| 39 | + filesql = "#{tmp_dir}/#{dbx[:bak_filename]}" | |
| 40 | + filetgz = "#{filesql}.tar.gz" | |
| 41 | + | |
| 42 | + awscli_s3_file filetgz do | |
| 43 | + region node['cfe-mariadb']['s3_region'] | |
| 44 | + bucket node['cfe-mariadb']['s3_bucket'] | |
| 45 | + key "#{dbx[:bak_filename]}.tar.gz" | |
| 46 | + if manual_creds | |
| 47 | + aws_access_key_id node['cfe-mariadb']['reload']['aws_access_key_id'] | |
| 48 | + aws_secret_access_key node['cfe-mariadb']['reload']['aws_secret_access_key'] | |
| 49 | + end | |
| 50 | + only_if "test -d #{tmp_dir} || mkdir -p #{tmp_dir}" | |
| 51 | + notifies :run, "execute[untar_#{filetgz}]", :immediately | |
| 52 | + end | |
| 53 | + | |
| 54 | + execute "untar_#{filetgz}" do | |
| 55 | + command "tar -xzf #{filetgz} -C #{tmp_dir}/" | |
| 56 | + notifies :delete, "file[#{filetgz}]" | |
| 57 | + notifies :run, "execute[reload_#{filesql}]", :immediately | |
| 58 | + action :nothing | |
| 59 | + end | |
| 60 | + | |
| 61 | + execute "reload_#{filesql}" do | |
| 62 | + command "mysql -h #{node['mariadb']['mysqld']['bind_address']} "\ | |
| 63 | + "-P #{node['mariadb']['mysqld']['port']} -u #{dbx[:db_user]} "\ | |
| 64 | + "-p'#{dbx[:db_pass]}' -D #{dbx_name} < #{filesql}" | |
| 65 | + notifies :delete, "file[#{filesql}]" | |
| 66 | + sensitive true | |
| 67 | + action :nothing | |
| 68 | + end | |
| 69 | + | |
| 70 | + file filetgz do | |
| 71 | + action :nothing | |
| 72 | + end | |
| 73 | + | |
| 74 | + file filesql do | |
| 75 | + action :nothing | |
| 76 | + end | |
| 77 | +end | ... | ... |