aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/hydra/misc11
-rwxr-xr-xshare/hydractl/backup-restore-site8
-rwxr-xr-xshare/hydractl/backup-site61
-rwxr-xr-xshare/hydractl/puppet-reset-stored6
4 files changed, 67 insertions, 19 deletions
diff --git a/lib/hydra/misc b/lib/hydra/misc
index 90dd4b8..9bcc9df 100644
--- a/lib/hydra/misc
+++ b/lib/hydra/misc
@@ -1,6 +1,6 @@
#!/bin/bash
-# Set needed environment variables and do basic checks.
+# Set needed environment variables and do basic checks
function hydra_set_env {
export CONFIG="$HOME/.hydra/config"
export ACTION="$1"
@@ -26,7 +26,7 @@ function hydra_user_input {
fi
}
-# Install a package.
+# Install a package
function hydra_install_package {
if [ -z "$1" ]; then
return
@@ -38,3 +38,10 @@ function hydra_install_package {
DEBIAN_FRONTEND=noninteractive apt-get install $1 -y
fi
}
+
+# Truncate a database
+function hydra_truncate_database {
+ if [ ! -z "$1" ]; then
+ mysql puppet -e "drop database $1; create database $1;"
+ fi
+}
diff --git a/share/hydractl/backup-restore-site b/share/hydractl/backup-restore-site
index 16ebf93..d02fb79 100755
--- a/share/hydractl/backup-restore-site
+++ b/share/hydractl/backup-restore-site
@@ -60,7 +60,6 @@ fi
# Site
if [ -e "$FOLDER/site" ]; then
chown -R $SITE.$SITE $FOLDER/site
- echo "Databases should be manually restored"
fi
# Drupal
@@ -71,3 +70,10 @@ if [ -e "$FOLDER/drupal" ]; then
chown root.$SITE $FOLDER/drupal/settings.php
chmod 640 $FOLDER/drupal/settings.php
fi
+
+# Restore database
+if [ -f "$RESTOREDIR/var/backups/mysql/sqldump/$SITE.sql.gz" ]; then
+ hydra_truncate_database $SITE
+else
+ echo "Databases should be manually restored"
+fi
diff --git a/share/hydractl/backup-site b/share/hydractl/backup-site
index 53119da..79bc4ec 100755
--- a/share/hydractl/backup-site
+++ b/share/hydractl/backup-site
@@ -2,31 +2,62 @@
#
# Pack a website.
#
-# TODO: put on $SITES/backups/site/$SITE, with htaccess and correct
-# ownership/permission.
-#
-# TODO: optionally backup database before packing
-#
-# TODO: add backup files in a dated folder
# Arguments
SITES="/var/sites"
SITE="$1"
PACK="$1.tar.bz2"
+DATE="`date +%Y%m%d`"
+DEST="$SITES/backups/site/$SITE/$DATE"
+# Syntax check
if [ -z "$SITE" ]; then
hydra_action_usage
exit 1
-elif [ ! -d "$SITES/$SITE" ]; then
- echo "No such site $SITE"
- exit 1
fi
-mkdir -p $SITES/backups/$SITE
-cd $SITES/backups/$SITE
+# Create folder
+mkdir -p $DEST
+cd $DEST
-tar jcvf $PACK $SITES/$SITE
-md5sum $PACK > $PACK.md5
-sha1sum $PACK > $PACK.sha1
+# Password setup
+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
+fi
-echo "Saved $SITES/backups/$SITE/$PACK"
+# Access setup
+if [ ! -e "$SITES/backups/site/$SITE/.htaccess" ]; then
+ echo > $SITES/backups/site/$SITE/.htaccess <<EOF
+AuthType Basic
+AuthName "Backup $SITE"
+AuthUserFile $SITES/backups/site/$SITE/.htpasswd
+Require valid-user
+EOF
+fi
+
+# Backup site
+if [ ! -d "$SITES/$SITE" ]; then
+ echo "Backing up site folder..."
+ tar jcvf $PACK $SITES/$SITE
+ md5sum $PACK > $PACK.md5
+ sha1sum $PACK > $PACK.sha1
+ chown root.backups $PACK*
+ chmod 640 $PACK*
+ echo "Saved $DEST/$PACK"
+fi
+
+# Backup database
+if [ -d "/var/lib/mysql/$SITE" ]; then
+ echo "Backing up database $SITE..."
+ mysqldump $SITE > $SITE.sql
+ bzip2 $SITE.sql
+ md5sum $SITE.sql.bz2 > $SITE.sql.bz2.md5
+ sh1sum $SITE.sql.bz2 > $SITE.sql.bz2.sha1
+ chown root.backups $SITE.sql*
+ chmod 640 $SITE.sql*
+ echo "Saved $DEST/$SITE.sql.bz2"
+else
+ echo "Database not found, please backup it manually"
+fi
diff --git a/share/hydractl/puppet-reset-stored b/share/hydractl/puppet-reset-stored
index 48a3dc1..090917f 100755
--- a/share/hydractl/puppet-reset-stored
+++ b/share/hydractl/puppet-reset-stored
@@ -3,4 +3,8 @@
# Reset puppet stored configs
#
-mysql puppet -e "drop database puppet ; create database puppet;"
+# Load.
+source $APP_BASE/lib/hydra/functions || exit 1
+hydra_config_load
+
+hydra_truncate_database puppet