Skip to content

Instantly share code, notes, and snippets.

@herbetom
Last active May 3, 2019 20:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save herbetom/b4c59dc87aa8a42008fc5a18d75dac09 to your computer and use it in GitHub Desktop.
Save herbetom/b4c59dc87aa8a42008fc5a18d75dac09 to your computer and use it in GitHub Desktop.
This script can be run before ElkarBackup does the backup of a nextcloud-data direcory. It stores the SQL Dump within the data directory.
#!/bin/bash
# inspired by https://github.com/elkarbackup/elkarbackup-scripts/tree/master/backup-mysql
# access to the nexecloud-DB from the user doing the backup via mysqldump must be granted somehow
URL=`echo $ELKARBACKUP_URL | cut -d ":" -f1` # user@serverip
USER="${URL%@*}" # user
HOST="${URL#*@}" # host
DIR=`echo $ELKARBACKUP_URL | cut -d ":" -f2` # path
DIR=$DIR"/sqlbkp" # path inside the main backup folder
TMP=/tmp/ebmysqldump
# which DB should be backed up
DB="nextcloud"
SSHPARAMS="-i /var/lib/elkarbackup/.ssh/id_rsa -o StrictHostKeyChecking=no $ELKARBACKUP_SSH_ARGS"
if [ "$ELKARBACKUP_LEVEL" != "JOB" ]
then
echo "Only allowed at job level" >&2
exit 1
fi
if [ "$ELKARBACKUP_EVENT" == "PRE" ]
then
# If backup directory doesn't exist, create it
TEST=`ssh $SSHPARAMS $USER@$HOST "test -d $DIR && echo $?"`
if [ ! ${TEST} ]; then
echo "[INFO] Backup directory $DIR doesn't exist. Creating..."
ssh $SSHPARAMS $USER@$HOST "mkdir -p $DIR"
fi
# If tmp directory doesn't exist, create it
TEST=`ssh $SSHPARAMS $USER@$HOST "test -d $TMP && echo $?"`
if [ ! ${TEST} ]; then
echo "[INFO] TMP directory $TMP doesn't exist. Creating..."
ssh $SSHPARAMS $USER@$HOST "mkdir -p $TMP"
fi
# If .htaccess doesn't exist, create it
TEST=`ssh $SSHPARAMS $USER@$HOST "test -f $DIR/.htaccess && echo $?"`
if [ ! ${TEST} ]; then
ssh $SSHPARAMS $USER@$HOST "echo \"# line below if for Apache 2.4
<ifModule mod_authz_core.c>
Require all denied
</ifModule>
# line below if for Apache 2.2
<ifModule !mod_authz_core.c>
deny from all
Satisfy All
</ifModule>
# section for Apache 2.2 and 2.4
<ifModule mod_autoindex.c>
IndexIgnore *
</ifModule>\" > $DIR/.htaccess"
fi
# enable Nextcloud Maintenance Mode
ssh $SSHPARAMS $USER@$HOST "sudo -u www-data php /var/www/nextcloud/occ maintenance:mode --on"
# Dump it!
ssh $SSHPARAMS $USER@$HOST "mysqldump --force --opt --databases $DB --single-transaction --quick --lock-tables=FALSE > $TMP/$DB.sql"
# If we already have an old version...
TEST=`ssh $SSHPARAMS $USER@$HOST "test -f $DIR/$DB.sql && echo $?"`
if [ ${TEST} ]; then
# Diff
#echo "making a diff"
TEST=`ssh $SSHPARAMS $USER@$HOST "diff -q <(cat $TMP/$DB.sql|head -n -1) <(cat $DIR/$DB.sql|head -n -1) > /dev/null && echo $?"`
#echo "diff result: [$TEST]"
# If Diff = false, copy tmp dump file
if [ ! ${TEST} ]; then
ssh $SSHPARAMS $USER@$HOST "cp $TMP/$DB.sql $DIR/$DB.sql"
echo "[$DB.sql] Changes detected. New dump saved."
else
echo "[$DB.sql] No changes detected. Nothing to save."
fi
else
echo "[$DB.sql] First dump created!"
ssh $SSHPARAMS $USER@$HOST "cp $TMP/$DB.sql $DIR/$DB.sql"
fi
else
# disable Nextcloud Maintenance Mode
ssh $SSHPARAMS $USER@$HOST "sudo -u www-data php /var/www/nextcloud/occ maintenance:mode --off"
echo "";
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment