Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
"backupdb.rb" is the main recipe inside of a larger "backupdb" role
#
# Cookbook Name:: smashrun
# Recipe:: backupdb
#
# Copyright 2010, Smashrun, Inc.
# Author:: Steven Craig <support@smashrun.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# http://msdn.microsoft.com/en-us/library/ms175477(v=SQL.90).aspx
# http://msdn.microsoft.com/en-us/library/ms190217(v=SQL.90).aspx
# http://msdn.microsoft.com/en-us/library/ms190440(v=SQL.90).aspx
# translog truncation
# http://msdn.microsoft.com/en-US/library/ms189085(v=SQL.90).aspx
# shrinking the translog file
# http://msdn.microsoft.com/en-US/library/ms178037(v=SQL.90).aspx
log("begin backupdb.rb") { level :debug }
log("running backupdb.rb") { level :info }
["full", "trans"].each do |backuptype|
unless File.exists?("#{node[:backupdb][:sqlscripts]}\\#{backuptype}-#{node[:backupdb][:basesql]}.log")
# ensure necessary dirs are setup and available
["#{node[:backupdb][:tempdir]}", "#{node[:backupdb][:sqlscripts]}", "#{node[:backupdb][:backupdir]}"].each do |dir|
# log("create #{dir} directory if necessary") { level :debug }
directory "#{dir}" do
action :create
not_if { File.exists?("#{dir}") }
recursive true
end
end
# organize backups
log("begin organize backups") { level :debug }
# running_database holds which databases run on which database hosts
# database_info holds location and file information specific to each database
running_database = []
database_info = []
# gather necessary information
search(:running_database, "id:#{node[:hostname]}") do |host|
running_database << host["database"].split(",")
running_database.flatten!
running_database.each do |dbname|
search(:database_info, "id:#{dbname}") do |db|
dbinfo = { "id" => db["id"],
"dbname" => db["id"],
"backupdrive" => db["backupdrive"],
"backupdir" => db["backupdir"],
"collation" => db["collation"],
"comment" => db["comment"],
"datadrive" => db["datadrive"],
"datadir" => db["datadir"],
"datafile" => db["datafile"],
"datainitsize" => db["datainitsize"],
"datamaxsize" => db["datamaxsize"],
"owner" => db["owner"],
"restoredrive" => db["restoredrive"],
"restoredir" => db["restoredir"],
"transdrive" => db["transdrive"],
"transdir" => db["transdir"],
"transfile" => db["transfile"],
"transinitsize" => db["transinitsize"],
"transmaxsize" => db["transmaxsize"]
}
database_info << dbinfo
end
end
# log(database_info.inspect) {level :debug}
# run backupdb sql script
execute "#{backuptype}-#{node[:createdb][:executesql_bat]}" do
cwd "#{node[:backupdb][:sqlscripts]}"
command "#{node[:backupdb][:sqlscripts]}\\#{node[:createdb][:executesql_bat]} SQL-SAPASSWD #{backuptype}-#{node[:backupdb][:basesql]} #{backuptype}-#{node[:backupdb][:basesql]}"
not_if { File.exists?("#{node[:backupdb][:sqlscripts]}\\#{backuptype}-#{node[:backupdb][:basesql]}.log") }
timeout 300
end
# templated batch file to execute transact-sql backup statements
log("create #{node[:backupdb][:sqlscripts]}\\#{node[:createdb][:executesql_bat]} if necessary") { level :debug }
template "#{node[:backupdb][:sqlscripts]}\\#{node[:createdb][:executesql_bat]}" do
source "#{node[:createdb][:executesql_template]}"
variables({
:author_name => "#{node[:backupdb][:author_name]}",
:author_email => "#{node[:backupdb][:author_email]}",
:working_dir => "#{node[:backupdb][:sqlscripts]}",
:sqlcmd_path => "#{node[:createdb][:sqlcmd_path]}",
:sqlsausername => "#{node[:createdb][:sqlsausername]}",
:executesql_bat => "#{node[:createdb][:executesql_bat]}",
:executesql_template => "#{node[:createdb][:executesql_template]}"
})
notifies :run, resources(:execute => "#{backuptype}-#{node[:createdb][:executesql_bat]}")
end
# ensure transact-sql scripts to backupdb are current and execute backupdb if necessary
templated = nil
begin
templated = resources(:template => "backupdb-#{backuptype}-#{node[:backupdb][:basesql]}.erb")
rescue Chef::Exceptions::ResourceNotFound
templated = template "#{node[:backupdb][:sqlscripts]}\\#{backuptype}-#{node[:backupdb][:basesql]}" do
source "backupdb-#{backuptype}-#{node[:backupdb][:basesql]}.erb"
cookbook "smashrun"
backup false
variables({
:database => database_info,
:author_name => "#{node[:backupdb][:author_name]}",
:author_email => "#{node[:backupdb][:author_email]}",
:basesql => "#{backuptype}-#{node[:backupdb][:basesql]}",
:basesql_template => "backupdb-#{backuptype}-#{node[:backupdb][:basesql]}.erb"
})
notifies :run, resources(:execute => "#{backuptype}-#{node[:createdb][:executesql_bat]}"), :immediately
end
end
end
end
end
log("end backupdb.rb") { level :info }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment