diff options
Diffstat (limited to 'etc')
-rw-r--r-- | etc/nextcloudpi-config.d/nc-restore.sh | 88 |
1 files changed, 47 insertions, 41 deletions
diff --git a/etc/nextcloudpi-config.d/nc-restore.sh b/etc/nextcloudpi-config.d/nc-restore.sh index 7e3fee31..fc2033d7 100644 --- a/etc/nextcloudpi-config.d/nc-restore.sh +++ b/etc/nextcloudpi-config.d/nc-restore.sh @@ -17,7 +17,6 @@ BACKUPFILE_=/media/USBdrive/nextcloud-bkp_xxxxxxxx.tar -BASEDIR=/var/www DBADMIN=ncadmin DESCRIPTION="Restore a previously backuped NC instance" @@ -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; @@ -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 @@ -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" } |