From f360acf0fab1d941bc7de8bec3d22aabd64f49fe Mon Sep 17 00:00:00 2001 From: Silvio Rhatto Date: Wed, 4 Jan 2012 14:32:21 -0200 Subject: Enhanced backup restoration --- lib/hydra/backup | 25 ++++++++- lib/hydra/misc | 9 ++++ share/hydractl/backup-restore | 2 +- share/hydractl/backup-restore-gitosis | 2 +- share/hydractl/backup-restore-site | 97 ++++++++++++++++++++++++++++------- share/hydractl/backup-restore-svn | 2 +- share/hydractl/backup-site | 21 ++++++-- 7 files changed, 131 insertions(+), 27 deletions(-) diff --git a/lib/hydra/backup b/lib/hydra/backup index a92782e..f98aa0f 100644 --- a/lib/hydra/backup +++ b/lib/hydra/backup @@ -1,7 +1,27 @@ #!/bin/bash -# Setup backup environment -function hydra_backup_environment { +# Setup environment for backup restored from localhost +function hydra_backup_environment_local { + SITE="$1" + BACKUPDIR="/var/sites/backups/site/$SITE" + + if [ -z "$SITE" ]; then + hydra_action_usage + exit 1 + fi + + if [ ! -z "$DATE" ]; then + RESTOREDIR="$BACKUPDIR/$DATE" + else + RESTOREDIR="$BACKUPDIR/`date +%Y%m%d`" + fi + + DB_DUMP_BASE="$RESTOREDIR" + mkdir -p $RESTOREDIR +} + +# Setup environment for backup restored from remote source +function hydra_backup_environment_remote { NODE="$1" METHOD="$2" BACKUP_ROOT="/var/backups/remote" @@ -49,5 +69,6 @@ function hydra_backup_environment { exit 1 fi + DB_DUMP_BASE="$RESTOREDIR/var/backups/mysql/sqldump/" mkdir -p $RESTOREDIR } diff --git a/lib/hydra/misc b/lib/hydra/misc index 77d610d..66fa1bc 100644 --- a/lib/hydra/misc +++ b/lib/hydra/misc @@ -71,3 +71,12 @@ function hydra_check_user { grep -qe "^$1:" /etc/passwd } + +# Check for a group +function hydra_check_group { + if [ -z "$1" ]; then + return 1 + fi + + grep -qe "^$1:" /etc/group +} diff --git a/share/hydractl/backup-restore b/share/hydractl/backup-restore index acd3be4..160ed5e 100755 --- a/share/hydractl/backup-restore +++ b/share/hydractl/backup-restore @@ -9,5 +9,5 @@ source $APP_BASE/lib/hydra/functions || exit 1 hydra_config_load -hydra_backup_environment $* +hydra_backup_environment_remote $* duplicity restore file:///$BACKUPDIR/ $RESTOREDIR/ diff --git a/share/hydractl/backup-restore-gitosis b/share/hydractl/backup-restore-gitosis index d430693..7da4a05 100755 --- a/share/hydractl/backup-restore-gitosis +++ b/share/hydractl/backup-restore-gitosis @@ -8,7 +8,7 @@ source $APP_BASE/lib/hydra/functions || exit 1 hydra_config_load -hydra_backup_environment $* +hydra_backup_environment_remote $* # Check if folder exist on the backup # Check if there's already a site folder and backup it diff --git a/share/hydractl/backup-restore-site b/share/hydractl/backup-restore-site index 39353e9..4604755 100755 --- a/share/hydractl/backup-restore-site +++ b/share/hydractl/backup-restore-site @@ -1,34 +1,52 @@ #!/bin/bash # -# Restore a website from backup +# Restore a website from backup. # -# TODO: moin -# TODO: check for site user -# TODO: support restoration from /var/sites/backups # Load. source $APP_BASE/lib/hydra/functions || exit 1 hydra_config_load +# Basic parameters. SITE="$2" SITES="/var/sites" FOLDER="$SITES/$SITE" WWW="/var/www/data" +# Syntax check. if [ -z "$SITE" ]; then hydra_action_usage exit 1 fi -hydra_backup_environment $1 restore +# Check restore stratety. +if [ "$1" == "localhost" ]; then + hydra_backup_environment_local $2 restore +else + hydra_backup_environment_remote $1 restore +fi + +# Set site user. +if hydra_check_user $SITE; then + $SITE_USER="$SITE" +else + $SITE_USER="root" +fi -# Check if folder exist on the backup +# Set site group. +if hydra_check_group $SITE; then + $SITE_GROUP="$SITE" +else + $SITE_GROUP="root" +fi + +# Check if folder exist on the backup. if [ ! -d "$RESTOREDIR/$FOLDER" ]; then echo "Folder $FOLDER does not exist at restored backup $RESTOREDIR" exit 1 fi -# Check if there's already a site folder and backup it +# Check if there's already a site folder and backup it. if [ -e "$FOLDER" ]; then echo "Folder $FOLDER already exists, backing it up first..." hydractl backup-site $SITE @@ -41,45 +59,69 @@ if [ -e "$FOLDER" ]; then fi fi +# Local backups are stored compressed +if [ "$1" == "localhost" ]; then + if [ -e "$RESTOREDIR/$SITE.tar.bz2" ]; then + ( cd $RESTOREDIR && tar xvf $SITE.tar.bz2 ) + fi +fi + # Copy site folder from backup -echo "Copying site $SITE from backup $RESTOREDIR..." -cp -a $RESTOREDIR/$FOLDER $FOLDER +if [ -d "$RESTOREDIR/$FOLDER" ]; then + echo "Copying site $SITE from backup $RESTOREDIR..." + cp -a $RESTOREDIR/$FOLDER $FOLDER +fi + +# Deleted uncompressed local backup +if [ "$1" == "localhost" ]; then + rm -rf $RESTOREDIR/$FOLDER +fi # Fix permissions chown -R root.root $FOLDER # Trac if [ -e "$FOLDER/trac" ]; then - ( cd $FOLDER/trac && chown -R $SITE.$SITE attachments conf db auth plugins .egg-cache ) + ( cd $FOLDER/trac && chown -R $SITE_USER.$SITE_GROUP attachments conf db auth plugins .egg-cache ) fi # PmWiki if [ -e "$FOLDER/wiki" ]; then - ( cd $FOLDER/wiki && chown -R $SITE.$SITE wiki.d uploads ) + ( cd $FOLDER/wiki && chown -R $SITE_USER.$SITE_GROUP wiki.d uploads ) chown $SITE.root $FOLDER/wiki/local/config.php chmod 660 $FOLDER/wiki/local/config.php fi # Site if [ -e "$FOLDER/site" ]; then - chown -R $SITE.$SITE $FOLDER/site + chown -R $SITE_USER.$SITE_GROUP $FOLDER/site fi # Drupal if [ -e "$FOLDER/drupal" ]; then SERIES="$3" - chown -R $SITE.$SITE $FOLDER/drupal/files - chown root.$SITE $FOLDER/drupal/settings.php + chown -R $SITE_USER.$SITE_GROUP $FOLDER/drupal/files + chown root.$SITE_GROUP $FOLDER/drupal/settings.php chmod 640 $FOLDER/drupal/settings.php + if [ -e "/etc/apache2/sites-available/$SITE" ]; then + SERVER_ALIAS="`grep ServerAlias /etc/apache2/sites-available/$SITE | sed -e 's/ServerAlias//'`" + fi + if [ ! -z "$SERIES" ]; then - if [ -e "$WWW/drupal-$SERIES" ]; then + if [ -e "$WWW/drupal-$SERIES/sites" ]; then echo "Creating basic drupal symlinks..." ( cd $WWW/drupal-$SERIES/sites ln -sf $FOLDER/drupal $SITE ln -sf $FOLDER/drupal $SITE.`facter domain` + + for server_alias in $SERVER_ALIAS; do + if [ ! -e "$server_alias" ]; then + ln -sf $FOLDER/drupal $server_alias + fi + done ) else echo "No drupal $SERIES found in the system, so no symlink handling" @@ -89,13 +131,25 @@ if [ -e "$FOLDER/drupal" ]; then fi fi +# Check if there are database dumps +if [ -f "$DB_DUMP_BASE/$SITE.sql.gz" ]; then + DB_DUMP_EXT=".gz" + DB_DUMP_EXTRACT="gunzip" +elif [ -f "$DB_DUMP_BASE/$SITE.sql.bz2" ]; then + DB_DUMP_EXT=".bz2" + DB_DUMP_EXTRACT="bunzip" +elif [ -f "$DB_DUMP_BASE/$SITE.sql" ]; then + DB_DUMP_EXT="" + DB_DUMP_EXTRACT="true" +fi + # Restore database -if [ -f "$RESTOREDIR/var/backups/mysql/sqldump/$SITE.sql.gz" ]; then +if [ ! -z "$DB_DUMP_EXT" ]; then echo "Restoring database $SITE..." BASEDIR="$FOLDER" hydra_set_tmpfile $SITE -d - cp $RESTOREDIR/var/backups/mysql/sqldump/$SITE.sql.gz $TMPWORK - ( cd $TMPWORK && gunzip $SITE.sql.gz ) + cp $DB_DUMP_BASE/$SITE.sql$DB_DUMP_EXT $TMPWORK + ( cd $TMPWORK && $DB_DUMP_EXTRACT $SITE.sql$DB_DUMP_EXT ) hydra_truncate_database $SITE mysql $SITE < $TMPWORK/$SITE.sql hydra_unset_tmpfile $TMPWORK @@ -115,3 +169,10 @@ if [ -e "$FOLDER/ikiwiki" ]; then echo "Either $file or git repository not found for $SITE ikiwiki instance" fi fi + +# Moin +if [ -e "$FOLDER/moin" ]; then + echo "Restoring moin for $SITE..." + chown -R root.root $FOLDER/moin/ + chown -R $SITE_USER.$SITE_GROUP $FOLDER/moin/{cgi-bin,data,underlay} +fi diff --git a/share/hydractl/backup-restore-svn b/share/hydractl/backup-restore-svn index 91c0988..e4b1660 100755 --- a/share/hydractl/backup-restore-svn +++ b/share/hydractl/backup-restore-svn @@ -8,7 +8,7 @@ source $APP_BASE/lib/hydra/functions || exit 1 hydra_config_load -hydra_backup_environment $* +hydra_backup_environment_remote $* # Check if folder exist on the backup # Check if there's already a site folder and backup it diff --git a/share/hydractl/backup-site b/share/hydractl/backup-site index dedf0eb..7dd9acf 100755 --- a/share/hydractl/backup-site +++ b/share/hydractl/backup-site @@ -2,7 +2,6 @@ # # Pack a website. # -# TODO: check for backup user/group # Arguments SITES="/var/sites" @@ -17,6 +16,20 @@ if [ -z "$SITE" ]; then exit 1 fi +# Set backups user. +if hydra_check_user backups; then + $BACKUPS_USER="backups" +else + $BACKUPS_USER="root" +fi + +# Set backups group. +if hydra_check_group backups; then + $BACKUPS_GROUP="backups" +else + $BACKUPS_GROUP="root" +fi + # Create folder mkdir -p $DEST cd $DEST @@ -25,7 +38,7 @@ cd $DEST if [ ! -e "$SITES/backups/site/$SITE/.htpasswd" ]; then touch $SITES/backups/site/$SITE/.htpasswd chmod 640 $SITES/backups/site/$SITE/.htpasswd - chown root.backups $SITES/backups/site/$SITE/.htpasswd + chown root.$BACKUPS_GROUP $SITES/backups/site/$SITE/.htpasswd fi # Access setup @@ -44,7 +57,7 @@ if [ -d "$SITES/$SITE" ]; then tar jcvf $PACK $SITES/$SITE md5sum $PACK > $PACK.md5 sha1sum $PACK > $PACK.sha1 - chown root.backups $PACK* + chown root.$BACKUPS_GROUP $PACK* chmod 640 $PACK* echo "Saved $DEST/$PACK" fi @@ -56,7 +69,7 @@ if [ -d "/var/lib/mysql/$SITE" ]; then bzip2 $SITE.sql md5sum $SITE.sql.bz2 > $SITE.sql.bz2.md5 sha1sum $SITE.sql.bz2 > $SITE.sql.bz2.sha1 - chown root.backups $SITE.sql* + chown root.$BACKUPS_GROUP $SITE.sql* chmod 640 $SITE.sql* echo "Saved $DEST/$SITE.sql.bz2" else -- cgit v1.2.3