Skip to content

Commit

Permalink
nc-restore: restore compressed backups
Browse files Browse the repository at this point in the history
  • Loading branch information
nacho committed Jan 5, 2018
1 parent 10fd204 commit a4f27a2
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 42 deletions.
4 changes: 3 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@

[v0.44.5](https://github.com/nextcloud/nextcloudpi/commit/0ab0c5a) (2018-01-04) nc-backup: compress backups and refactoring
[v0.44.6](https://github.com/nextcloud/nextcloudpi/commit/1b20f3d) (2018-01-05) nc-restore: restore compressed backups

[v0.44.5](https://github.com/nextcloud/nextcloudpi/commit/10fd204) (2018-01-04) nc-backup: compress backups and refactoring

[v0.44.4 ](https://github.com/nextcloud/nextcloudpi/commit/56576da) (2018-01-03) nc-restore: update redis password

Expand Down
88 changes: 47 additions & 41 deletions etc/nextcloudpi-config.d/nc-restore.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@


BACKUPFILE_=/media/USBdrive/nextcloud-bkp_xxxxxxxx.tar
BASEDIR=/var/www
DBADMIN=ncadmin
DESCRIPTION="Restore a previously backuped NC instance"

Expand All @@ -33,28 +32,41 @@ configure()
{
local DBPASSWD=$( grep password /root/.my.cnf | cut -d= -f2 )

[ -f $BACKUPFILE_ ] || { echo -e "$BACKUPFILE_ not found"; return 1; }
[ -d $BASEDIR ] || { echo -e "$BASEDIR not found"; return 1; }
[ -d $BASEDIR/nextcloud ] && { echo -e "INFO: overwriting old instance"; }
[ -f $BACKUPFILE_ ] || { echo "$BACKUPFILE_ not found"; return 1; }
[ -d /var/www/nextcloud ] && { echo "INFO: overwriting old instance" ; }

local TMPDIR="$( dirname $BACKUPFILE_ )/$( basename ${BACKUPFILE_}-tmp )"
rm -rf "$TMPDIR" && mkdir -p "$TMPDIR"

echo -e "extracting backup file $BACKUPFILE_..."
# EXTRACT FILES
[[ "$BACKUPFILE_" =~ ".tar.gz" ]] && {
echo "decompressing backup file $BACKUPFILE_..."
tar -xzf "$BACKUPFILE_" -C "$TMPDIR" || return 1
BACKUPFILE_="$( ls "$TMPDIR"/*.tar 2>/dev/null )"
[[ -f "$BACKUPFILE_" ]] || { echo "$BACKUPFILE_ not found"; return 1; }
}

echo "extracting backup file $BACKUPFILE_..."
tar -xf "$BACKUPFILE_" -C "$TMPDIR" || return 1

## RESTORE FILES

echo -e "restore files..."
rm -rf $BASEDIR/nextcloud
mv "$TMPDIR"/nextcloud $BASEDIR || { echo -e "Error restoring base files"; return 1; }
echo "restore files..."
rm -rf /var/www/nextcloud
mv "$TMPDIR"/nextcloud /var/www || { echo "Error restoring base files"; return 1; }

# update NC database password to this instance
sed -i "s|'dbpassword' =>.*|'dbpassword' => '$DBPASSWD',|" $BASEDIR/nextcloud/config/config.php
sed -i "s|'dbpassword' =>.*|'dbpassword' => '$DBPASSWD',|" /var/www/nextcloud/config/config.php

# update redis credentials
local REDISPASS="$( grep "^requirepass" /etc/redis/redis.conf | cut -f2 -d' ' )"
[[ "$REDISPASS" != "" ]] && \
sed -i "s|'password'.*|'password' => '$REDISPASS',|" /var/www/nextcloud/config/config.php
service redis restart

## RE-CREATE DATABASE TABLE

echo -e "restore database..."
echo "restore database..."
mysql -u root <<EOF
DROP DATABASE IF EXISTS nextcloud;
CREATE DATABASE nextcloud;
Expand All @@ -64,57 +76,46 @@ 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 "Error configuring nextcloud database"; return 1; }

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

## RESTORE DATADIR

cd $BASEDIR/nextcloud
cd /var/www/nextcloud

### INCLUDEDATA=yes situation

if [[ $( ls "$TMPDIR" | wc -l ) == 3 ]]; then

local DATADIR=$( grep datadirectory /var/www/nextcloud/config/config.php | awk '{ print $3 }' | grep -oP "[^']*[^']" | head -1 )
[[ "$DATADIR" == "" ]] && { echo "Error reading data directory"; return 1; }

# INCLUDEDATA=yes situation
echo "restore datadir to $DATADIR..."

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..."
test -e "$DATADIR" && {
[[ -e "$DATADIR" ]] && {
echo "backing up existing $DATADIR"
mv "$DATADIR" "$DATADIR-$( date "+%m-%d-%y" )"
}

mkdir -p "$( dirname "$DATADIR" )"
mv "$TMPDIR/$( basename "$DATADIR" )" "$DATADIR"

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

# update fail2ban logpath
sed -i "s|logpath =.*|logpath = $DATADIR/nextcloud.log|" /etc/fail2ban/jail.conf

sudo -u www-data php occ maintenance:mode --off

# INCLUDEDATA=no situation
### INCLUDEDATA=no situation

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

# 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

# update fail2ban logpath
sed -i "s|logpath =.*|logpath = /var/www/nextcloud/data/nextcloud.log|" /etc/fail2ban/jail.conf

# update redis credentials
local REDISPASS="$( grep "^requirepass" /etc/redis/redis.conf | cut -f2 -d' ' )"
[[ "$REDISPASS" != "" ]] && \
sed -i "s|'password'.*|'password' => '$REDISPASS',|" config/config.php
echo "no datadir found in backup"
local DATADIR=/var/www/nextcloud/data

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

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

# cache needs to be cleaned as of NC 12

bash -c " sleep 3
systemctl stop php7.0-fpm
systemctl stop mysqld
Expand All @@ -124,7 +125,12 @@ EOF
" &>/dev/null &
fi

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

# update fail2ban logpath
sed -i "s|logpath =.*|logpath = $DATADIR/nextcloud.log|" /etc/fail2ban/jail.conf
pgrep fail2ban &>/dev/null && service fail2ban restart

rm -r "$TMPDIR"
}
Expand Down

0 comments on commit a4f27a2

Please sign in to comment.