Suspend sites while backing up

This avoids backup verification failures
This commit is contained in:
Bob Mottram 2015-12-22 23:08:15 +00:00
parent 9fd09e55be
commit 91a670f11b
2 changed files with 87 additions and 2 deletions

View File

@ -49,13 +49,37 @@ fi
ADMIN_USERNAME= ADMIN_USERNAME=
ADMIN_NAME= ADMIN_NAME=
# The name of a currently suspended site
# Sites are suspended so that verification should work
SUSPENDED_SITE=
DATABASE_PASSWORD='' DATABASE_PASSWORD=''
if [ -f /root/dbpass ]; then if [ -f /root/dbpass ]; then
DATABASE_PASSWORD=$(cat /root/dbpass) DATABASE_PASSWORD=$(cat /root/dbpass)
fi fi
function suspend_site {
# suspends a given website
SUSPENDED_SITE="$1"
nginx_dissite $SUSPENDED_SITE
service nginx reload
}
function restart_site {
# restarts a given website
if [ ! $SUSPENDED_SITE ]; then
return
fi
nginx_ensite $SUSPENDED_SITE
service nginx reload
SUSPENDED_SITE=
}
function update_domains { function update_domains {
GIT_DOMAIN_NAME='gogs'
if grep -q "Gogs domain" $COMPLETION_FILE; then
GIT_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Gogs domain" | awk -F ':' '{print $2}')
fi
MICROBLOG_DOMAIN_NAME='microblog' MICROBLOG_DOMAIN_NAME='microblog'
if grep -q "GNU Social domain" $COMPLETION_FILE; then if grep -q "GNU Social domain" $COMPLETION_FILE; then
MICROBLOG_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "GNU Social domain" | awk -F ':' '{print $2}') MICROBLOG_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "GNU Social domain" | awk -F ':' '{print $2}')
@ -68,6 +92,10 @@ function update_domains {
if grep -q "Blog domain" $COMPLETION_FILE; then if grep -q "Blog domain" $COMPLETION_FILE; then
FULLBLOG_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Blog domain" | awk -F ':' '{print $2}') FULLBLOG_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Blog domain" | awk -F ':' '{print $2}')
fi fi
OWNCLOUD_DOMAIN_NAME='owncloud'
if grep -q "Owncloud domain" $COMPLETION_FILE; then
OWNCLOUD_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Owncloud domain" | awk -F ':' '{print $2}')
fi
} }
function mount_drive { function mount_drive {
@ -133,6 +161,7 @@ function unmount_drive {
function backup_database { function backup_database {
if [ ${#DATABASE_PASSWORD} -lt 2 ]; then if [ ${#DATABASE_PASSWORD} -lt 2 ]; then
echo $"No MariaDB password was given" echo $"No MariaDB password was given"
restart_site
exit 10 exit 10
fi fi
if [ ! -d $USB_MOUNT/backup/${1} ]; then if [ ! -d $USB_MOUNT/backup/${1} ]; then
@ -152,6 +181,7 @@ function backup_database {
rm -rf /root/temp${1}data rm -rf /root/temp${1}data
umount $USB_MOUNT umount $USB_MOUNT
rm -rf $USB_MOUNT rm -rf $USB_MOUNT
restart_site
exit 5 exit 5
fi fi
} }
@ -163,6 +193,7 @@ function backup_directory_to_usb {
BACKUP_KEY_EXISTS=$(gpg --list-keys "$ADMIN_NAME (backup key)") BACKUP_KEY_EXISTS=$(gpg --list-keys "$ADMIN_NAME (backup key)")
if [ ! "$?" = "0" ]; then if [ ! "$?" = "0" ]; then
echo $"Backup key could not be found" echo $"Backup key could not be found"
restart_site
exit 6 exit 6
fi fi
MY_BACKUP_KEY_ID=$(gpg --list-keys "$ADMIN_NAME (backup key)" | grep 'pub ' | awk -F ' ' '{print $2}' | awk -F '/' '{print $2}') MY_BACKUP_KEY_ID=$(gpg --list-keys "$ADMIN_NAME (backup key)" | grep 'pub ' | awk -F ' ' '{print $2}' | awk -F '/' '{print $2}')
@ -179,6 +210,7 @@ function backup_directory_to_usb {
shred -zu ${1}/* shred -zu ${1}/*
rm -rf ${1} rm -rf ${1}
fi fi
restart_site
exit 71 exit 71
fi fi
obnam forget --keep=30d -r $USB_MOUNT/backup/${2} --encrypt-with $MY_BACKUP_KEY_ID obnam forget --keep=30d -r $USB_MOUNT/backup/${2} --encrypt-with $MY_BACKUP_KEY_ID
@ -189,6 +221,7 @@ function backup_directory_to_usb {
shred -zu ${1}/* shred -zu ${1}/*
rm -rf ${1} rm -rf ${1}
fi fi
restart_site
exit 7 exit 7
fi fi
if [[ ${1} == "/root/temp"* || ${1} == *"tempbackup" ]]; then if [[ ${1} == "/root/temp"* || ${1} == *"tempbackup" ]]; then
@ -326,9 +359,23 @@ function backup_directories {
do do
# if this directory exists then backup the given database # if this directory exists then backup the given database
required_directory=$(echo $dr | awk -F ',' '{print $1}' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//') required_directory=$(echo $dr | awk -F ',' '{print $1}' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
database_name=$(echo $dr | awk -F ',' '{print $2}' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
if [[ "$database_name" == *"hubzilla"* ]]; then
suspend_site ${HUBZILLA_DOMAIN_NAME}
fi
if [[ "$database_name" == *"gnusocial"* ]]; then
suspend_site ${MICROBLOG_DOMAIN_NAME}
fi
if [[ "$database_name" == *"owncloud"* ]]; then
suspend_site ${OWNCLOUD_DOMAIN_NAME}
fi
if [[ "$database_name" == *"gogs"* ]]; then
suspend_site ${GIT_DOMAIN_NAME}
fi
if [[ $required_directory != "none" ]]; then if [[ $required_directory != "none" ]]; then
if [ -d $required_directory ]; then if [ -d $required_directory ]; then
database_name=$(echo $dr | awk -F ',' '{print $2}' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
if [[ $database_name != "none" ]]; then if [[ $database_name != "none" ]]; then
backup_database $database_name backup_database $database_name
fi fi
@ -342,6 +389,8 @@ function backup_directories {
echo $"Backing up $source_directory to $dest_directory" echo $"Backing up $source_directory to $dest_directory"
backup_directory_to_usb $source_directory $dest_directory backup_directory_to_usb $source_directory $dest_directory
fi fi
restart_site
done done
} }

View File

@ -66,10 +66,32 @@ if [ ! -d $SERVER_DIRECTORY/backup ]; then
mkdir -p $SERVER_DIRECTORY/backup mkdir -p $SERVER_DIRECTORY/backup
fi fi
# The name of a currently suspended site
# Sites are suspended so that verification should work
SUSPENDED_SITE=
function suspend_site {
# suspends a given website
SUSPENDED_SITE="$1"
nginx_dissite $SUSPENDED_SITE
service nginx reload
}
function restart_site {
# restarts a given website
if [ ! $SUSPENDED_SITE ]; then
return
fi
nginx_ensite $SUSPENDED_SITE
service nginx reload
SUSPENDED_SITE=
}
function backup_directory_to_friend { function backup_directory_to_friend {
BACKUP_KEY_EXISTS=$(gpg --list-keys "$ADMIN_NAME (backup key)") BACKUP_KEY_EXISTS=$(gpg --list-keys "$ADMIN_NAME (backup key)")
if [ ! "$?" = "0" ]; then if [ ! "$?" = "0" ]; then
echo $"Backup key could not be found" echo $"Backup key could not be found"
restart_site
exit 43382 exit 43382
fi fi
ADMIN_BACKUP_KEY_ID=$(gpg --list-keys "$ADMIN_NAME (backup key)" | grep 'pub ' | awk -F ' ' '{print $2}' | awk -F '/' '{print $2}') ADMIN_BACKUP_KEY_ID=$(gpg --list-keys "$ADMIN_NAME (backup key)" | grep 'pub ' | awk -F ' ' '{print $2}' | awk -F '/' '{print $2}')
@ -86,6 +108,7 @@ function backup_directory_to_friend {
fi fi
# Send a warning email # Send a warning email
echo "Unable to verify ${2}" | mail -s "${PROJECT_NAME} backup to friends" ${ADMIN_EMAIL_ADDRESS} echo "Unable to verify ${2}" | mail -s "${PROJECT_NAME} backup to friends" ${ADMIN_EMAIL_ADDRESS}
restart_site
exit 953 exit 953
fi fi
obnam forget --keep=30d -r $SERVER_DIRECTORY/backup/${2} --encrypt-with ${ADMIN_BACKUP_KEY_ID} obnam forget --keep=30d -r $SERVER_DIRECTORY/backup/${2} --encrypt-with ${ADMIN_BACKUP_KEY_ID}
@ -96,6 +119,7 @@ function backup_directory_to_friend {
fi fi
# Send a warning email # Send a warning email
echo "Unable to backup ${2}" | mail -s "${PROJECT_NAME} backup to friends" ${ADMIN_EMAIL_ADDRESS} echo "Unable to backup ${2}" | mail -s "${PROJECT_NAME} backup to friends" ${ADMIN_EMAIL_ADDRESS}
restart_site
exit 853 exit 853
fi fi
if [[ ${1} == "/root/temp"* || ${1} == *"tempbackup" ]]; then if [[ ${1} == "/root/temp"* || ${1} == *"tempbackup" ]]; then
@ -107,6 +131,7 @@ function backup_directory_to_friend {
function backup_database_to_friend { function backup_database_to_friend {
if [ ${#DATABASE_PASSWORD} -lt 2 ]; then if [ ${#DATABASE_PASSWORD} -lt 2 ]; then
echo $"No MariaDB password was given" echo $"No MariaDB password was given"
restart_site
exit 5783 exit 5783
fi fi
if [ ! -d $SERVER_DIRECTORY/backup/${1} ]; then if [ ! -d $SERVER_DIRECTORY/backup/${1} ]; then
@ -126,6 +151,7 @@ function backup_database_to_friend {
rm -rf /root/temp${1}data rm -rf /root/temp${1}data
# Send a warning email # Send a warning email
echo $"Unable to export ${1} database" | mail -s $"${PROJECT_NAME} backup to friends" $ADMIN_EMAIL_ADDRESS echo $"Unable to export ${1} database" | mail -s $"${PROJECT_NAME} backup to friends" $ADMIN_EMAIL_ADDRESS
restart_site
exit 5738 exit 5738
fi fi
} }
@ -226,10 +252,12 @@ function backup_gnusocial {
if grep -q "GNU Social domain" $COMPLETION_FILE; then if grep -q "GNU Social domain" $COMPLETION_FILE; then
MICROBLOG_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "GNU Social domain" | awk -F ':' '{print $2}') MICROBLOG_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "GNU Social domain" | awk -F ':' '{print $2}')
if [ -d /var/www/${MICROBLOG_DOMAIN_NAME}/htdocs ]; then if [ -d /var/www/${MICROBLOG_DOMAIN_NAME}/htdocs ]; then
suspend_site ${MICROBLOG_DOMAIN_NAME}
backup_database_to_friend gnusocial backup_database_to_friend gnusocial
backup_directory_to_friend /root/tempgnusocialdata gnusocialdata backup_directory_to_friend /root/tempgnusocialdata gnusocialdata
echo $"Backing up GNU social installation" echo $"Backing up GNU social installation"
backup_directory_to_friend /var/www/${MICROBLOG_DOMAIN_NAME}/htdocs gnusocial backup_directory_to_friend /var/www/${MICROBLOG_DOMAIN_NAME}/htdocs gnusocial
restart_site
else else
echo $"GNU Social domain specified but not found in /var/www/${MICROBLOG_DOMAIN_NAME}" echo $"GNU Social domain specified but not found in /var/www/${MICROBLOG_DOMAIN_NAME}"
fi fi
@ -240,10 +268,12 @@ function backup_hubzilla {
if grep -q "Hubzilla domain" $COMPLETION_FILE; then if grep -q "Hubzilla domain" $COMPLETION_FILE; then
HUBZILLA_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Hubzilla domain" | awk -F ':' '{print $2}') HUBZILLA_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Hubzilla domain" | awk -F ':' '{print $2}')
if [ -d /var/www/${HUBZILLA_DOMAIN_NAME} ]; then if [ -d /var/www/${HUBZILLA_DOMAIN_NAME} ]; then
suspend_site ${HUBZILLA_DOMAIN_NAME}
backup_database_to_friend hubzilla backup_database_to_friend hubzilla
backup_directory_to_friend /root/temphubzilladata hubzilladata backup_directory_to_friend /root/temphubzilladata hubzilladata
echo "Backing up Hubzilla installation" echo "Backing up Hubzilla installation"
backup_directory_to_friend /var/www/${HUBZILLA_DOMAIN_NAME}/htdocs hubzilla backup_directory_to_friend /var/www/${HUBZILLA_DOMAIN_NAME}/htdocs hubzilla
restart_site
else else
echo $"Hubzilla domain specified but not found in /var/www/${HUBZILLA_DOMAIN_NAME}" echo $"Hubzilla domain specified but not found in /var/www/${HUBZILLA_DOMAIN_NAME}"
exit 2578 exit 2578
@ -253,16 +283,21 @@ function backup_hubzilla {
function backup_owncloud { function backup_owncloud {
if [ -d /etc/owncloud ]; then if [ -d /etc/owncloud ]; then
OWNCLOUD_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Owncloud domain" | awk -F ':' '{print $2}')
suspend_site ${OWNCLOUD_DOMAIN_NAME}
backup_database_to_friend owncloud backup_database_to_friend owncloud
backup_directory_to_friend /root/tempownclouddata ownclouddata backup_directory_to_friend /root/tempownclouddata ownclouddata
echo $"Backing up Owncloud data" echo $"Backing up Owncloud data"
backup_directory_to_friend /var/lib/owncloud owncloud backup_directory_to_friend /var/lib/owncloud owncloud
backup_directory_to_friend /etc/owncloud owncloud2 backup_directory_to_friend /etc/owncloud owncloud2
restart_site
fi fi
} }
function backup_gogs { function backup_gogs {
if [ -d /home/git/go/src/github.com/gogits ]; then if [ -d /home/git/go/src/github.com/gogits ]; then
GIT_DOMAIN_NAME=$(cat $COMPLETION_FILE | grep "Gogs domain" | awk -F ':' '{print $2}')
suspend_site ${GIT_DOMAIN_NAME}
backup_database_to_friend gogs backup_database_to_friend gogs
backup_directory_to_friend /root/tempgogsdata gogsdata backup_directory_to_friend /root/tempgogsdata gogsdata
echo $"Obtaining Gogs settings backup" echo $"Obtaining Gogs settings backup"
@ -272,6 +307,7 @@ function backup_gogs {
backup_directory_to_friend /home/git/gogs-repositories gogsrepos backup_directory_to_friend /home/git/gogs-repositories gogsrepos
echo $"Obtaining Gogs authorized_keys backup" echo $"Obtaining Gogs authorized_keys backup"
backup_directory_to_friend /home/git/.ssh gogsssh backup_directory_to_friend /home/git/.ssh gogsssh
restart_site
fi fi
} }
@ -537,7 +573,7 @@ do
if [ $REMOTE_SERVER ]; then if [ $REMOTE_SERVER ]; then
REMOTE_DOMAIN=$(echo "${remote_server}" | awk -F ' ' '{print $1}' | awk -F '@' '{print $2}') REMOTE_DOMAIN=$(echo "${remote_server}" | awk -F ' ' '{print $1}' | awk -F '@' '{print $2}')
REMOTE_SSH_PORT=$(echo "${remote_server}" | awk -F ' ' '{print $2}') REMOTE_SSH_PORT=$(echo "${remote_server}" | awk -F ' ' '{print $2}')
REMOTE_DIRECTORY=$(echo "${remote_server}" | awk -F ' ' '{print $3}') REMOTE_DIRECTORY=$(echo "${remote_server}" | awk -F ' ' '{print $3}')
REMOTE_PASSWORD=$(echo "${remote_server}" | awk -F ' ' '{print $4}') REMOTE_PASSWORD=$(echo "${remote_server}" | awk -F ' ' '{print $4}')
NOW=$(date +"%Y-%m-%d %H:%M:%S") NOW=$(date +"%Y-%m-%d %H:%M:%S")
REMOTE_SERVER=$REMOTE_SERVER:$REMOTE_DIRECTORY REMOTE_SERVER=$REMOTE_SERVER:$REMOTE_DIRECTORY