diff options
| author | mo khan <mo@mokhan.ca> | 2015-12-23 22:04:17 -0700 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2015-12-23 22:04:17 -0700 |
| commit | 9128f9314a4f723c5c37a2dc220dd752aa4d4464 (patch) | |
| tree | 48b8399096aea168f7350997b1bbeb085110d30a | |
| parent | 6f43e9640de5a43982b8f6a124276db0c08c84d3 (diff) | |
sync pg backups to s3.
| -rw-r--r-- | attributes/default.rb | 1 | ||||
| -rw-r--r-- | libraries/aws.rb | 26 | ||||
| -rw-r--r-- | recipes/aws.rb | 20 | ||||
| -rw-r--r-- | recipes/postgres.rb | 9 | ||||
| -rw-r--r-- | recipes/rails.rb | 2 | ||||
| -rw-r--r-- | templates/pg_backup.config.erb | 1 | ||||
| -rwxr-xr-x | templates/pg_backup_rotated.sh.erb | 4 |
7 files changed, 41 insertions, 22 deletions
diff --git a/attributes/default.rb b/attributes/default.rb index 0738b14..1b84dfc 100644 --- a/attributes/default.rb +++ b/attributes/default.rb @@ -50,6 +50,7 @@ default['stronglifters']['nginx']['blacklisted_ips'] = [] default['stronglifters']['nginx']['domain'] = 'www.example.com' default['stronglifters']['ruby_version'] = '2.2.3' default['stronglifters']['username'] = 'rails' +default['stronglifters']['s3_backup_path'] = 's3://bucket/backups/' pg_connection_string = if node['postgres'].nil? == false diff --git a/libraries/aws.rb b/libraries/aws.rb new file mode 100644 index 0000000..f26a61d --- /dev/null +++ b/libraries/aws.rb @@ -0,0 +1,26 @@ +class Chef + class Recipe + def aws_cli(username) + package "python-pip" + execute "pip install awscli" + + home = node['etc']['passwd'][username]['dir'] + directory "#{home}/.aws/" do + owner username + group username + mode "0755" + recursive true + action :create + end + + configuration = node["stronglifters"]["aws"] + template "#{home}/.aws/config" do + variables(configuration) + end + + template "#{home}/.aws/credentials" do + variables(configuration) + end + end + end +end diff --git a/recipes/aws.rb b/recipes/aws.rb deleted file mode 100644 index 54045d5..0000000 --- a/recipes/aws.rb +++ /dev/null @@ -1,20 +0,0 @@ -package "python-pip" -execute "pip install awscli" - -username = node["stronglifters"]["username"] -directory "/home/#{username}/.aws/" do - owner username - group username - mode "0755" - recursive true - action :create -end - -configuration = node["stronglifters"]["aws"] -template "/home/#{username}/.aws/config" do - variables(configuration) -end - -template "/home/#{username}/.aws/credentials" do - variables(configuration) -end diff --git a/recipes/postgres.rb b/recipes/postgres.rb index 849738a..886ab62 100644 --- a/recipes/postgres.rb +++ b/recipes/postgres.rb @@ -48,14 +48,21 @@ end template "/etc/postgresql/pg_backup.config" do user "postgres" group "postgres" - variables(backup_dir: "#{backups_dir}/") + variables({ + backup_dir: "#{backups_dir}/", + s3_backup_path: node['stronglifters']['s3_backup_path'], + }) end file "/var/lib/postgresql/.pgpass" do content "localhost:5432:*:postgres:#{node["postgresql"]["password"]["postgres"]}" + group "postgres" mode "0600" + user "postgres" end +aws_cli("postgres") + cron 'pg_backups' do action :create command "#{backups_dir}/pg_backup_rotated.sh -c /etc/postgresql/pg_backup.config" diff --git a/recipes/rails.rb b/recipes/rails.rb index c16598b..04659f0 100644 --- a/recipes/rails.rb +++ b/recipes/rails.rb @@ -1,5 +1,5 @@ include_recipe "stronglifters::user" -include_recipe "stronglifters::aws" +aws_cli(node["stronglifters"]["username"]) root_path = node["stronglifters"]["root_path"] shared_path = File.join(root_path, "shared") diff --git a/templates/pg_backup.config.erb b/templates/pg_backup.config.erb index fd5660a..4380f07 100644 --- a/templates/pg_backup.config.erb +++ b/templates/pg_backup.config.erb @@ -2,6 +2,7 @@ BACKUP_USER= HOSTNAME="localhost" USERNAME="postgres" BACKUP_DIR=<%= @backup_dir %> +S3_PATH=<%= @s3_path %> SCHEMA_ONLY_LIST="" ENABLE_CUSTOM_BACKUPS=yes ENABLE_PLAIN_BACKUPS=yes diff --git a/templates/pg_backup_rotated.sh.erb b/templates/pg_backup_rotated.sh.erb index 9776b52..b1e3cb9 100755 --- a/templates/pg_backup_rotated.sh.erb +++ b/templates/pg_backup_rotated.sh.erb @@ -131,3 +131,7 @@ fi find $BACKUP_DIR -maxdepth 1 -mtime +$DAYS_TO_KEEP -name "*-daily" -exec rm -rf '{}' ';' perform_backups "-daily" + +if [ -z $S3_PATH ] ; then + aws s3 sync $BACKUP_DIR $S3_PATH +fi |
