Created
July 13, 2012 17:58
-
-
Save paulmooring/3106302 to your computer and use it in GitHub Desktop.
Private Chef restore script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Author:: Paul Mooring (<[email protected]>) | |
# Copyright:: Copyright (c) 2012 Opscode, Inc. | |
# License:: Apache License, Version 2.0 | |
# | |
# 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. | |
## Default values ## | |
verb=0 | |
drbd_res='pc0' | |
drbd_dev='/dev/drbd0' | |
data_dir='/var/opt/opscode/drbd/data' | |
bkup_file='/opt/opscode/private_chef_backups/data/backup.tar.gz' | |
sql_bkup='/opt/opscode/private_chef_backups/sql/backup.sql' | |
pg_data='/var/opt/opscode/drbd/data/postgresql' | |
pg_user='opscode-pgsql' | |
##################### | |
help_msg(){ | |
cat << EOF | |
usage: ${0} [options] | |
Options: | |
-q Quiet | |
-v Verbose | |
-h Help | |
-d <dir> Chef data directory | |
-r <res> Chef DRBD resource | |
-b <dev> DRBD block device | |
-f <file> Backup file | |
-s <file> SQL backup file | |
-u <user> Postgres user name | |
-x <dir> Postgres data directory | |
EOF | |
} | |
debug(){ | |
if [ -z $2 ] ; then | |
lvl=0 | |
else | |
lvl=$2 | |
fi | |
if [ $verb -gt $lvl ] ; then | |
echo $1 | |
fi | |
} | |
verify_file(){ | |
if [ -${2} $1 ] ; then | |
debug "$1 is ok" 2 | |
else | |
echo "test -${2} $1 failed." 1>&2 | |
exit 4 | |
fi | |
} | |
sanity_check(){ | |
debug "Checking DRBD resource and device..." | |
debug "Running: drbdadm dstate $drbd_res" 2 | |
drbdadm dstate $drbd_res ; pipe_stat=$? | |
if [ $pipe_stat -ne 0 ] ; then | |
echo "DRBD resource $drbd_res not ready" 1>&2 | |
exit $pipe_stat | |
elif [ -b $drbd_dev ] ; then | |
echo "$drbd_dev is not a block device" 1>&2 | |
exit 2 | |
fi | |
debug "Verify files exist..." | |
verify_file /opt/opscode/embedded/bin/psql x | |
verify_file $sql_bkup f | |
verify_file $data_dir d | |
verify_file $bckup_file f | |
verify_file $sql_bckup f | |
} | |
mount_drbd(){ | |
debug "Mounting DRBD device" | |
debug "Running: mount $drbd_dev $data_dir" 2 | |
mount $drbd_dev $data_dir ; pipe_stat=$? | |
if [ $pipe_stat -ne 0 ] ; then | |
echo "Failed to mount DRBD device" 1>&2 | |
exit $pipe_stat | |
fi | |
debug "$drbd_dev mounted on $data_dir" 2 | |
} | |
clear_old_data(){ | |
debug "Clearing old data..." | |
debug "Running: rm -rf ${data_dir}/*" 2 | |
rm -rf ${data_dir}/* | |
} | |
restore_db(){ | |
debug "Restore postgres database" | |
debug "Starting postgres" 2 | |
private-chef-ctl postgres start | |
pipe_stat=1 | |
debug "Waiting for Postgres to come up." 2 | |
while [ $pipe_stat -ne 0 ] ; do | |
su -l $pg_user -c "/opt/opscode/embedded/bin/pg_ctl status -D $pg_data" ; pipe_stat=$? | |
sleep 1 | |
done | |
debug "Running: su -l $pg_user -c '/opt/opscode/embedded/bin/psql -f $sql_bkup -U opscode_chef'" 2 | |
su -l $pg_user -c "/opt/opscode/embedded/bin/psql -f $sql_bkup -U opscode_chef" | |
} | |
restore_backup(){ | |
debug "Restoring backup" | |
debug "Running: tar xzvf $bkup_file -C $data_dir" 2 | |
tar xzvf $bkup_file -C $data_dir ; pipe_stat=$? | |
if [ $pipe_stat -ne 0 ] ; then | |
echo "Failed to restore backup" 1>&2 | |
exit $pipe_stat | |
fi | |
} | |
while getopts ":vqhd:r:b:f:s:u:x:" opt; do | |
case $opt in | |
h) | |
help_msg | |
exit 0 | |
;; | |
v) | |
verb=$(expr $verb + 1) | |
;; | |
q) | |
verb=$(expr $verb - 1) | |
;; | |
d) | |
data_dir=${OPTARG} | |
;; | |
r) | |
drbd_res=${OPTARG} | |
;; | |
b) | |
drbd_dev=${OPTARG} | |
;; | |
f) | |
bkup_file=${OPTARG} | |
;; | |
s) | |
sql_bkup=${OPTARG} | |
;; | |
u) | |
pg_user=${OPTARG} | |
;; | |
x) | |
pg_data=${OPTARG} | |
;; | |
\?) | |
echo "Invalid option: -$OPTARG" >&2 | |
help_msg | |
exit 1 | |
;; | |
:) | |
echo "Option -$OPTARG requires an argument." >&2 | |
help_msg | |
exit 1 | |
;; | |
esac | |
done | |
sanity_check | |
mount_drbd | |
clear_old_data | |
restore_backup | |
restore_db | |
private-chef-ctl reconfigure | |
private-chef-ctl start |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment