diff --git a/CHANGELOG.md b/CHANGELOG.md index 3da9d5c..ea0dd5e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,34 +1,28 @@ -s3_asset_backup CHANGELOG -========================= +# s3_asset_backup CHANGELOG + This file is used to list changes made in each version of the s3_asset_backup cookbook. -0.1.0 +0.3.0 ----- -- [your_name] - Initial release of s3_asset_backup +- Fixed tests, updated databasse dependency -0.2.0 +0.2.4 ----- -- Significant mods to the configuration of asstes to be backed up. -- Added option to backup multiple databases using a name prefix ending in '*' +- Changed default backup prefix to '%Y-%m-%d' (e.g. "2015-11-04") because it makes more sense. -0.2.1 +0.2.3 ----- -- Removed use_inline_resources from provider base class because it somehow prevents the generation of actual base-defined resources when the node is built. +- Fixed syslog logging. Replaced the success_message resource attribute with a good fixed message, and rewrote the mysql script to work ok when there are duplicated databases and tables. 0.2.2 ----- - Fixed greivous error where the config.yml file was written with % as wildcard but the script file expected *. -0.2.3 ------ -- Fixed syslog logging. Replaced the success_message resource attribute with a good fixed message, and rewrote the mysql script to work ok when there are duplicated databases and tables. - -0.2.4 ------ -- Changed default backup prefix to '%Y-%m-%d' (e.g. "2015-11-04") because it makes more sense. +## 0.2.0 +- Significant mods to the configuration of asstes to be backed up. +- Added option to backup multiple databases using a name prefix ending in '*' -- - - -Check the [Markdown Syntax Guide](http://daringfireball.net/projects/markdown/syntax) for help with Markdown. +## 0.1.0 +- [your_name] - Initial release of s3_asset_backup -The [Github Flavored Markdown page](http://github.github.com/github-flavored-markdown/) describes the differences between markdown on github and standard markdown. diff --git a/files/default/s3_mysql_restore.rb b/files/default/s3_mysql_restore.rb new file mode 100644 index 0000000..f0487ef --- /dev/null +++ b/files/default/s3_mysql_restore.rb @@ -0,0 +1,47 @@ +require 'aws-sdk' +require 'open3' +require 'syslog' +require 'yaml' + +TEMP_TGZ_FILE = 'temp/tgz' + +def run_system_command(command) + stdout, stderr, exit_status = Open3.capture3(command) + unless exit_status.success? + puts [stdout, stderr].join("\n") + raise "Command failed: #{command}" + end + stdout +end + +mysql_user, mysql_pw, restore_prefix = ARGV +unless mysql_user && mysql_pw + puts "Restore selected selected sql dump file from S3 storage." + puts "usage: ruby #{__FILE__} mysql_user mysql_password " + exit +end +restore_prefix ||= Time.now.strftime(s3['time_prefix']) + +config = YAML.load_file('config.yml') +s3 = config['s3'] +objects_saved = 0 +s3_client = Aws::S3::Client.new(access_key_id: s3['access_key_id'], secret_access_key: s3['secret_access_key'], region: s3['region']) +selected_backup_files = Aws::S3::Bucket.new(s3['bucket'], client: s3_client).objects(prefix: restore_prefix) + +puts "Ready to restore #{selected_backup_files.size} sql files from bucket #{s3['bucket']} with prefix '#{restore_prefix}'." +puts "[Enter] to start." +gets + +selected_backup_files.each do |backup_file| + tarfile = backup_file.key.split('/').last + sqlfile = tarfile.split('.tgz').first + dbname = sqlfile.split('.sql').first + backup_file.get({response_target: tarfile}) + run_system_command "tar -xvzf " + tarfile + run_system_command "mysql -u #{mysql_user} -p#{mysql_password} -e 'drop database if exists `#{dbname}`'" + run_system_command "mysql -u #{mysql_user} -p#{mysql_password} -e 'create database `#{dbname}`'" + run_system_command "cat sqlfile | mysql --database=#{dbname}" + puts "Loaded #{sqlfile} (#{File.new(sqlfile).size} bytes) to database '#{dbname}'" + File.delete tarfile + File.delete sqlfile +end diff --git a/files/default/test.rb b/files/default/test.rb new file mode 100644 index 0000000..9ca99b2 --- /dev/null +++ b/files/default/test.rb @@ -0,0 +1,5 @@ +ARGV.each do|a| + puts "Argument: #{a}" +end +puts "zero: #{ARGV[0]}" +puts "zero is null?: #{ARGV[0].nil?}" diff --git a/metadata.rb b/metadata.rb index eeb8090..893c2f4 100644 --- a/metadata.rb +++ b/metadata.rb @@ -4,7 +4,7 @@ license 'All rights reserved' description 'Installs/Configures s3_asset_backup' long_description IO.read(File.join(File.dirname(__FILE__), 'README.md')) -version '0.2.4' +version '0.3.0' depends 'ruby', '~>0.9' -depends 'database', '~> 4.0' +depends 'database', '~> 6.0' diff --git a/test/fixtures/cookbooks/test/spec/recipes/s3_file_backup_resource_spec.rb b/test/fixtures/cookbooks/test/spec/recipes/s3_file_backup_resource_spec.rb index d3a6c37..695d537 100644 --- a/test/fixtures/cookbooks/test/spec/recipes/s3_file_backup_resource_spec.rb +++ b/test/fixtures/cookbooks/test/spec/recipes/s3_file_backup_resource_spec.rb @@ -48,6 +48,23 @@ end describe "the config.yml file" do + let(:expected_content) do + [ + "s3:", + " region: us-east-1", + " access_key_id: my_access_key_id", + " secret_access_key: my_secret_access_key", + " bucket: my_bucket", + " time_prefix: '%Y-%m-%d'", + "log:", + " ident: s3_file_backup", + "backup_groups:", + " default:", + " - /some/file", + " a_prefix:", + " - /yet_another/file" + ] + end it "is created" do expect(subject).to create_file("/home/my_backup/config.yml") end @@ -58,24 +75,13 @@ end it "has the right content" do # each element, but not testing line order :-/ - [ - "s3:\n" + - " region: us-east-1\n" + - " access_key_id: my_access_key_id\n" + - " secret_access_key: my_secret_access_key\n" + - " bucket: my_bucket\n" + - " time_prefix: '%d-%b-%Y'", - "log:\n" + - " ident: s3_file_backup\n" + - "backup_groups:\n" + - " default:\n" + - " - /some/file\n" + - " a_prefix:\n" + - " - /yet_another/file\n" - ].each do |fragment| + expected_content.each do |fragment| expect(subject).to render_file("/home/my_backup/config.yml").with_content fragment end end + it "is correct on the whole" do + expect(subject).to render_file("/home/my_backup/config.yml").with_content expected_content.join("\n") + end end describe "the cron job" do diff --git a/test/fixtures/cookbooks/test/spec/recipes/s3_mysql_backup_resource_spec.rb b/test/fixtures/cookbooks/test/spec/recipes/s3_mysql_backup_resource_spec.rb index a82666c..7bdd5ec 100644 --- a/test/fixtures/cookbooks/test/spec/recipes/s3_mysql_backup_resource_spec.rb +++ b/test/fixtures/cookbooks/test/spec/recipes/s3_mysql_backup_resource_spec.rb @@ -48,6 +48,23 @@ end describe "the config.yml file" do + let(:expected_content) do + [ + "s3:", + " region: us-east-1", + " access_key_id: my_access_key_id", + " secret_access_key: my_secret_access_key", + " bucket: my_bucket", + " time_prefix: '%Y-%m-%d'", + "log:", + " ident: s3_mysql_backup", + "backup_groups:", + " default:", + " - db1", + " catalogs:", + " - db2 table1 table2" + ] + end it "is created" do expect(subject).to create_file("/home/my_backup/config.yml") end @@ -58,24 +75,13 @@ end it "has the right content" do # each element, but not testing line order :-/ - [ - "s3:\n" + - " region: us-east-1\n" + - " access_key_id: my_access_key_id\n" + - " secret_access_key: my_secret_access_key\n" + - " bucket: my_bucket\n" + - " time_prefix: '%d-%b-%Y'", - "log:\n" + - " ident: s3_mysql_backup\n" + - "backup_groups:\n" + - " default:\n" + - " - db1\n" + - " catalogs:\n" + - " - db2 table1 table2\n" - ].each do |fragment| + expected_content.each do |fragment| expect(subject).to render_file("/home/my_backup/config.yml").with_content fragment end end + it "is correct on the whole" do + expect(subject).to render_file("/home/my_backup/config.yml").with_content expected_content.join("\n") + end end describe 'the mysql_database_user' do