backup2s3.rb 1.11 KB
#
# Cookbook Name:: cfe-mariadb
# Recipe:: backup2s3
#
# Copyright 2016, Chromedia
#
# All rights reserved - Do Not Redistribute
#

# Sets up the shell script that backs up the databases
# and uploads them to a S3 bucket.
# Also sets up the cron job to regularly run this script.

# TODO: set up logrotate
include_recipe 'awscli'

md   = node['cfe-mariadb']
mdb  = md['backup']
mdbc = mdb['cron']

template "#{scr_dir}/backup_db_to_s3" do
  only_if "test -d #{mdb['script_dir']} || mkdir -p #{mdb['script_dir']}"
  variables(
    :db_map        => md['db_map'],
    :db_ip         => node['mariadb']['mysqld']['bind_address'],
    :db_port       => node['mariadb']['mysqld']['port'],
    :s3_region     => mdb['s3_region'],
    :s3_bucket     => mdb['s3_bucket'],
    :aws_bin       => mdb['aws_bin'],
    :mysqldump_bin => mdb['mysqldump_bin']
  )
end

cron 'backup_db_to_s3' do
  command "bash #{scr_dir}/backup_db_to_s3"
  minute  mdbc['min']
  hour    mdbc['hour']
  day     mdbc['day']
  month   mdbc['mon']
  weekday mdbc['wday']
  mailto  mdbc['mailto']
  path    '/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin'
end