Commit b1c36c6ab0f5b0324a98c2f562ac60fd3ceeabac
Committed by
Earth Ugat
1 parent
20fb182a
Bump to v0.2.1
Showing
6 changed files
with
100 additions
and
15 deletions
| ... | ... | @@ -115,7 +115,7 @@ This will install the backup script and also enable a cronjob to regularly run t |
| 115 | 115 | |
| 116 | 116 | ### cfe-mariadb::reload_from_s3 |
| 117 | 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']`. | |
| 118 | +Kind of the reverse of `backup2s3`. Download a gzip 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 | 119 | |
| 120 | 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']`. |
| 121 | 121 | ## License and Authors | ... | ... |
| ... | ... | @@ -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.2.0' | |
| 7 | +version '0.2.1' | |
| 8 | 8 | |
| 9 | 9 | { |
| 10 | 10 | 'mariadb' => '0.3.1', | ... | ... |
| ... | ... | @@ -18,7 +18,7 @@ |
| 18 | 18 | # limitations under the License. |
| 19 | 19 | # |
| 20 | 20 | |
| 21 | -# Download a tarball of a MySQL dump from an S3 bucket, | |
| 21 | +# Download the gzip of a MySQL dump from an S3 bucket, | |
| 22 | 22 | # then load it up into a (preferably empty) database. |
| 23 | 23 | |
| 24 | 24 | include_recipe 'awscli' |
| ... | ... | @@ -37,25 +37,24 @@ node['cfe-mariadb']['db_map'].each do |dbx| |
| 37 | 37 | end |
| 38 | 38 | |
| 39 | 39 | filesql = "#{tmp_dir}/#{dbx[:bak_filename]}" |
| 40 | - filetgz = "#{filesql}.tar.gz" | |
| 40 | + filegz = "#{filesql}.gz" | |
| 41 | 41 | |
| 42 | - awscli_s3_file filetgz do | |
| 42 | + awscli_s3_file filegz do | |
| 43 | 43 | region node['cfe-mariadb']['s3_region'] |
| 44 | 44 | bucket node['cfe-mariadb']['s3_bucket'] |
| 45 | - key "#{dbx[:bak_filename]}.tar.gz" | |
| 45 | + key "#{dbx[:bak_filename]}.gz" | |
| 46 | 46 | if manual_creds |
| 47 | 47 | aws_access_key_id node['cfe-mariadb']['reload']['aws_access_key_id'] |
| 48 | 48 | aws_secret_access_key node['cfe-mariadb']['reload']['aws_secret_access_key'] |
| 49 | 49 | end |
| 50 | 50 | only_if "test -d #{tmp_dir} || mkdir -p #{tmp_dir}" |
| 51 | - notifies :run, "execute[untar_#{filetgz}]", :immediately | |
| 51 | + notifies :run, "execute[unpack_#{filegz}]", :immediately | |
| 52 | 52 | end |
| 53 | 53 | |
| 54 | - execute "untar_#{filetgz}" do | |
| 55 | - command "tar -xzf #{filetgz} -C #{tmp_dir}/" | |
| 56 | - notifies :delete, "file[#{filetgz}]" | |
| 54 | + execute "unpack_#{filegz}" do | |
| 55 | + command "gzip -d #{filegz}" | |
| 57 | 56 | notifies :run, "execute[reload_#{filesql}]", :immediately |
| 58 | - action :nothing | |
| 57 | + action :nothing | |
| 59 | 58 | end |
| 60 | 59 | |
| 61 | 60 | execute "reload_#{filesql}" do |
| ... | ... | @@ -67,10 +66,6 @@ node['cfe-mariadb']['db_map'].each do |dbx| |
| 67 | 66 | action :nothing |
| 68 | 67 | end |
| 69 | 68 | |
| 70 | - file filetgz do | |
| 71 | - action :nothing | |
| 72 | - end | |
| 73 | - | |
| 74 | 69 | file filesql do |
| 75 | 70 | action :nothing |
| 76 | 71 | end | ... | ... |
recipes/reload_tar_from_s3.rb
0 → 100644
| 1 | +# | |
| 2 | +# Author:: Earth U (<sysadmin @ chromedia.com>) | |
| 3 | +# Cookbook Name:: cfe-mariadb | |
| 4 | +# Recipe:: reload_tar_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 | +# This recipe is just here for backward-compatibility reasons. | |
| 22 | +# The previous backups scripts create .tar.gz files, instead of .gz, so | |
| 23 | +# this recipe is here to get .tar.gz backups. | |
| 24 | + | |
| 25 | +# Download a tarball of a MySQL dump from an S3 bucket, | |
| 26 | +# then load it up into a (preferably empty) database. | |
| 27 | + | |
| 28 | +include_recipe 'awscli' | |
| 29 | + | |
| 30 | +tmp_dir = ::File.join(Chef::Config[:file_cache_path], 'db_dumps') | |
| 31 | +manual_creds = node['cfe-mariadb'].has_key?('reload') && | |
| 32 | + node['cfe-mariadb']['reload'].has_key?('aws_access_key_id') | |
| 33 | + | |
| 34 | +node['cfe-mariadb']['db_map'].each do |dbx| | |
| 35 | + | |
| 36 | + if dbx.is_a?(Array) | |
| 37 | + dbx_name = dbx[0] | |
| 38 | + dbx = dbx[1] | |
| 39 | + else | |
| 40 | + dbx_name = dbx[:db_name] | |
| 41 | + end | |
| 42 | + | |
| 43 | + filesql = "#{tmp_dir}/#{dbx[:bak_filename]}" | |
| 44 | + filetgz = "#{filesql}.tar.gz" | |
| 45 | + | |
| 46 | + awscli_s3_file filetgz do | |
| 47 | + region node['cfe-mariadb']['s3_region'] | |
| 48 | + bucket node['cfe-mariadb']['s3_bucket'] | |
| 49 | + key "#{dbx[:bak_filename]}.tar.gz" | |
| 50 | + if manual_creds | |
| 51 | + aws_access_key_id node['cfe-mariadb']['reload']['aws_access_key_id'] | |
| 52 | + aws_secret_access_key node['cfe-mariadb']['reload']['aws_secret_access_key'] | |
| 53 | + end | |
| 54 | + only_if "test -d #{tmp_dir} || mkdir -p #{tmp_dir}" | |
| 55 | + notifies :run, "execute[untar_#{filetgz}]", :immediately | |
| 56 | + end | |
| 57 | + | |
| 58 | + execute "untar_#{filetgz}" do | |
| 59 | + command "tar -xzf #{filetgz} -C #{tmp_dir}/" | |
| 60 | + notifies :delete, "file[#{filetgz}]" | |
| 61 | + notifies :run, "execute[reload_#{filesql}]", :immediately | |
| 62 | + action :nothing | |
| 63 | + end | |
| 64 | + | |
| 65 | + execute "reload_#{filesql}" do | |
| 66 | + command "mysql -h #{node['mariadb']['mysqld']['bind_address']} "\ | |
| 67 | + "-P #{node['mariadb']['mysqld']['port']} -u #{dbx[:db_user]} "\ | |
| 68 | + "-p'#{dbx[:db_pass]}' -D #{dbx_name} < #{filesql}" | |
| 69 | + notifies :delete, "file[#{filesql}]" | |
| 70 | + sensitive true | |
| 71 | + action :nothing | |
| 72 | + end | |
| 73 | + | |
| 74 | + file filetgz do | |
| 75 | + action :nothing | |
| 76 | + end | |
| 77 | + | |
| 78 | + file filesql do | |
| 79 | + action :nothing | |
| 80 | + end | |
| 81 | +end | ... | ... |
| ... | ... | @@ -6,6 +6,10 @@ |
| 6 | 6 | |
| 7 | 7 | set -e |
| 8 | 8 | |
| 9 | +suffix=.backup_db_to_s3 | |
| 10 | +[ -f /tmp/*"$suffix" ] && exit 200 | |
| 11 | +tmp_file=$( mktemp --suffix "$suffix" ) | |
| 12 | + | |
| 9 | 13 | <% bak_dir = "#{Chef::Config[:file_cache_path]}/backup_db_to_s3" -%> |
| 10 | 14 | bak_dir=<%= bak_dir %> |
| 11 | 15 | db_host=<%= @db_ip %> |
| ... | ... | @@ -113,4 +117,5 @@ increment_backup_names <%= db[:bak_filename] %> <%= db[:bak_maxcopies] %> |
| 113 | 117 | upload_to_s3 <%= db[:bak_filename] %> |
| 114 | 118 | |
| 115 | 119 | <% end -%> |
| 120 | +rm "$tmp_file" | |
| 116 | 121 | echo "$(date) : Done." | ... | ... |