Skip to content

Commit

Permalink
Fixed tests, updated databasse dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
flatrocks committed Nov 28, 2016
1 parent 68b62ea commit 22b4bb2
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 51 deletions.
32 changes: 13 additions & 19 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
47 changes: 47 additions & 0 deletions files/default/s3_mysql_restore.rb
Original file line number Diff line number Diff line change
@@ -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 <restore_prefix, default = current backup prefix>"
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
5 changes: 5 additions & 0 deletions files/default/test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ARGV.each do|a|
puts "Argument: #{a}"
end
puts "zero: #{ARGV[0]}"
puts "zero is null?: #{ARGV[0].nil?}"
4 changes: 2 additions & 2 deletions metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 22b4bb2

Please sign in to comment.