Skip to content

Commit

Permalink
backup/restore: include datadir
Browse files Browse the repository at this point in the history
  • Loading branch information
nacho committed Aug 10, 2017
1 parent 0d48f1e commit 3babcac
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 17 deletions.
23 changes: 18 additions & 5 deletions etc/nextcloudpi-config.d/nc-backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,42 @@


DESTDIR_=/media/USBdrive
BASEDIR_=/var/www
INCLUDEDATA_=no
DESCRIPTION="Backup this NC instance to a file"

DESTFILE=$DESTDIR_/nextcloud-bkp_`date +"%Y%m%d"`.tar
DBBACKUP=nextcloud-sqlbkp_`date +"%Y%m%d"`.bak
BASEDIR=/var/www

configure()
{
cd $BASEDIR_/nextcloud
local DATADIR
DATADIR=$( cd $BASEDIR/nextcloud; sudo -u www-data php occ config:system:get datadirectory ) || {
echo -e "Error reading data directory. Is NextCloud running and configured?";
return 1;
}

cd $BASEDIR/nextcloud
sudo -u www-data php occ maintenance:mode --on

cd $BASEDIR_
cd $BASEDIR
echo -e "backup database..."
mysqldump -u root --single-transaction nextcloud > $DBBACKUP

[[ "$INCLUDEDATA_" == "yes" ]] && echo -e "backup datadir... "
echo -e "backup files..."
mkdir -p $DESTDIR_
tar -cf $DESTFILE $DBBACKUP nextcloud/ && \
tar -cf $DESTFILE $DATAFILE $DBBACKUP nextcloud/ --exclude "nextcloud/data/*/files/*" && \
echo -e "backup $DESTFILE generated" || \
echo -e "error generating backup"
rm $DBBACKUP

cd $BASEDIR_/nextcloud
[[ "$INCLUDEDATA_" == "yes" ]] && {
tar -rf $DESTFILE -C $DATADIR/.. $( basename $DATADIR ) || \
echo -e "error generating data backup"
}

cd $BASEDIR/nextcloud
sudo -u www-data php occ maintenance:mode --off
}

Expand Down
59 changes: 47 additions & 12 deletions etc/nextcloudpi-config.d/nc-restore.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,18 @@ configure()
[ -d $BASEDIR_ ] || { echo -e "$BASEDIR_ not found"; return 1; }
[ -d $BASEDIR_/nextcloud ] && { echo -e "WARNING: overwriting old instance"; }

cd $BASEDIR_/nextcloud
sudo -u www-data php occ maintenance:mode --on
local TMPDIR="$( dirname $BACKUPFILE_ )/$( basename ${BACKUPFILE_}-tmp )"
rm -rf "$TMPDIR" && mkdir -p "$TMPDIR"
tar -xf "$BACKUPFILE_" -C "$TMPDIR" || return 1

## RESTORE FILES

# RESTORE FILES
echo -e "restore files..."
cd $BASEDIR_
rm -rf nextcloud
tar -xf $BACKUPFILE_ || return 1
rm -rf $BASEDIR_/nextcloud
mv "$TMPDIR"/nextcloud $BASEDIR_

## RE-CREATE DATABASE TABLE

# RE-CREATE DATABASE TABLE
local DBPASSWD=$( grep password /root/.my.cnf | cut -d= -f2 )
echo -e "restore database..."
mysql -u root -p$DBPASSWD <<EOF
Expand All @@ -64,15 +66,48 @@ CREATE USER '$DBADMIN_'@'localhost' IDENTIFIED BY '$DBPASSWD';
GRANT ALL PRIVILEGES ON nextcloud.* TO $DBADMIN_@localhost;
EXIT
EOF
[ $? -ne 0 ] && { echo -e "error configuring nextcloud database"; return 1; }
[ $? -ne 0 ] && { echo -e "Error configuring nextcloud database"; return 1; }

mysql -u root -p$DBPASSWD nextcloud < nextcloud-sqlbkp_*.bak || { echo -e "error restoring nextcloud database"; return 1; }
mysql -u root -p$DBPASSWD nextcloud < "$TMPDIR"/nextcloud-sqlbkp_*.bak || { echo -e "Error restoring nextcloud database"; return 1; }

# Just in case we moved the opcache dir
sed -i "s|^opcache.file_cache=.*|opcache.file_cache=$BASEDIR_/nextcloud/data/.opcache|" /etc/php/7.0/mods-available/opcache.ini
## RESTORE DATADIR

cd $BASEDIR_/nextcloud
sudo -u www-data php occ maintenance:mode --off

# INCLUDEDATA=yes situation

if [[ $( ls "$TMPDIR" | wc -l ) == 2 ]]; then
local DATADIR=$( grep datadirectory $BASEDIR_/nextcloud/config/config.php | awk '{ print $3 }' | grep -oP "[^']*[^']" | head -1 )
[[ "$DATADIR" == "" ]] && { echo -e "Error reading data directory"; return 1; }
echo -e "restore datadir to $DATADIR..."
rm -rf "$DATADIR"
mkdir -p "$( dirname "$DATADIR" )"
mv "$TMPDIR/$( basename "$DATADIR" )" "$DATADIR"
sudo -u www-data php occ maintenance:mode --off

# INCLUDEDATA=no situation

else
echo -e "no datadir found in backup"
sed -i "s|'datadirectory' =>.*|'datadirectory' => '/var/www/nextcloud/data',|" "config/config.php"

sudo -u www-data php occ maintenance:mode --off
sudo -u www-data php occ files:scan --all

# cache needs to be cleaned as of NC 12
(
sleep 3
systemctl stop php7.0-fpm
systemctl stop mysqld
sleep 0.5
systemctl start php7.0-fpm
systemctl start mysqld
) &>/dev/null &
fi
rm -r "$TMPDIR"

# Just in case we moved the opcache dir
sed -i "s|^opcache.file_cache=.*|opcache.file_cache=$BASEDIR_/nextcloud/data/.opcache|" /etc/php/7.0/mods-available/opcache.ini
}

install() { :; }
Expand Down
1 change: 1 addition & 0 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ def test_user_login(self):
driver.find_element_by_id("user").send_keys("test_user1")
driver.find_element_by_id("password").send_keys("ownyourbits")
driver.find_element_by_id("submit").click()
self.assertNotIn("Wrong password", driver.page_source)

time.sleep(60) # first run can be really slow on QEMU
wait = WebDriverWait(driver, 210)
Expand Down

0 comments on commit 3babcac

Please sign in to comment.