Commit 9128f93

mo khan <mo@mokhan.ca>
2015-12-24 05:04:17
sync pg backups to s3.
1 parent 6f43e96
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
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
recipes/aws.rb
@@ -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
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"
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")
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
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