Skip to content

Commit

Permalink
nc-restore: restore to volume in docker container
Browse files Browse the repository at this point in the history
  • Loading branch information
nachoparker committed Sep 23, 2018
1 parent 3a1b974 commit f80ee23
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 17 deletions.
4 changes: 3 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@

[v0.60.0](https://github.com/nextcloud/nextcloudpi/commit/3930c82) (2018-09-22) add nc-previews
[v0.60.1](https://github.com/nextcloud/nextcloudpi/commit/2b80190) (2018-07-31) nc-restore: restore to volume in docker container

[v0.60.0, master](https://github.com/nextcloud/nextcloudpi/commit/e973397) (2018-09-22) add nc-previews

[v0.59.20](https://github.com/nextcloud/nextcloudpi/commit/4457485) (2018-09-21) autoupdate: log everything to ncp.log

Expand Down
10 changes: 6 additions & 4 deletions etc/ncp-config.d/nc-backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ DATADIR=$( $OCC config:system:get datadirectory ) || {
exit 1;
}
cleanup(){ local RET=$?; rm -f "${DBBACKUP}" ; $OCC maintenance:mode --off; exit $RET; }
fail() { local RET=$?; echo "Abort..." ; rm -f "${DBBACKUP}" "${DESTFILE}"; $OCC maintenance:mode --off; exit $RET; }
cleanup(){ local RET=$?; rm -f "${DBBACKUP}" ;[[ -f /.docker-image ]] && mv /data/nextcloud /data/app; $OCC maintenance:mode --off; exit $RET; }
fail() { local RET=$?; echo "Abort..." ; rm -f "${DBBACKUP}" "${DESTFILE}";[[ -f /.docker-image ]] && mv /data/nextcloud /data/app; $OCC maintenance:mode --off; exit $RET; }
trap cleanup EXIT
trap fail INT TERM HUP ERR
Expand All @@ -60,8 +60,10 @@ FREE=$( df "$DESTDIR" | tail -1 | awk '{ print $4 }' )
}
# database
cd /var/www || exit 1
$OCC maintenance:mode --on
[[ -f /.docker-image ]] && mv /data/app /data/nextcloud
[[ -f /.docker-image ]] && BASEDIR=/data || BASEDIR=/var/www
cd "$BASEDIR" || exit 1
echo "backup database..."
mysqldump -u root --single-transaction nextcloud > "$DBBACKUP"
Expand All @@ -73,7 +75,7 @@ tar --exclude "nextcloud/data/*/files/*" \
--exclude "nextcloud/data/{access,error,nextcloud}.log" \
--exclude "nextcloud/data/access.log" \
--exclude "nextcloud/data/ncp-update-backups/" \
-cf "$DESTFILE" "$DBBACKUP" nextcloud/ \
-cf "$DESTFILE" "$DBBACKUP" "nextcloud"/ \
|| {
echo "error generating backup"
exit 1
Expand Down
25 changes: 14 additions & 11 deletions etc/ncp-config.d/nc-restore.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ DBPASSWD="$( grep password /root/.my.cnf | sed 's|password=||' )"
DIR="$( cd "$( dirname "$BACKUPFILE" )" &>/dev/null && pwd )" #abspath
[[ $# -eq 0 ]] && { echo "missing first argument" ; exit 1; }
[[ -f "$BACKUPFILE" ]] || { echo "$BACKUPFILE not found" ; exit 1; }
[[ "$DIR" =~ "/var/www/nextcloud" ]] && { echo "Refusing to restore from /var/www/nextcloud"; exit 1; }
[[ -d /var/www/nextcloud ]] && { echo "INFO: overwriting old instance"; }
[[ -f /.docker-image ]] && NCDIR=/data/app || NCDIR=/var/www/nextcloud
[[ $# -eq 0 ]] && { echo "missing first argument" ; exit 1; }
[[ -f "$BACKUPFILE" ]] || { echo "$BACKUPFILE not found" ; exit 1; }
[[ "$DIR" =~ "$NCDIR" ]] && { echo "Refusing to restore from $NCDIR"; exit 1; }
TMPDIR="$( mktemp -d "$( dirname "$BACKUPFILE" )"/ncp-restore.XXXXXX )" || { echo "Failed to create temp dir" >&2; exit 1; }
TMPDIR="$( cd "$TMPDIR" &>/dev/null && pwd )" || { echo "$TMPDIR not found"; exit 1; } #abspath
Expand Down Expand Up @@ -66,8 +67,8 @@ tar -xf "$BACKUPFILE" -C "$TMPDIR" || exit 1
## RESTORE FILES
echo "restore files..."
rm -rf /var/www/nextcloud
mv "$TMPDIR"/nextcloud /var/www || { echo "Error restoring base files"; exit 1; }
rm -rf "$NCDIR"
mv -T "$TMPDIR"/nextcloud "$NCDIR" || { echo "Error restoring base files"; exit 1; }
# update NC database password to this instance
sed -i "s|'dbpassword' =>.*|'dbpassword' => '$DBPASSWD',|" /var/www/nextcloud/config/config.php
Expand Down Expand Up @@ -96,14 +97,14 @@ mysql -u root nextcloud < "$TMPDIR"/nextcloud-sqlbkp_*.bak || { echo "Error res
## RESTORE DATADIR
cd /var/www/nextcloud
cd "$NCDIR"
### INCLUDEDATA=yes situation
NUMFILES=$(( 2 + COMPRESSED ))
if [[ $( ls "$TMPDIR" | wc -l ) -eq $NUMFILES ]]; then
DATADIR=$( grep datadirectory /var/www/nextcloud/config/config.php | awk '{ print $3 }' | grep -oP "[^']*[^']" | head -1 )
DATADIR=$( grep datadirectory "$NCDIR"/config/config.php | awk '{ print $3 }' | grep -oP "[^']*[^']" | head -1 )
[[ "$DATADIR" == "" ]] && { echo "Error reading data directory"; exit 1; }
echo "restore datadir to $DATADIR..."
Expand All @@ -129,7 +130,7 @@ if [[ $( ls "$TMPDIR" | wc -l ) -eq $NUMFILES ]]; then
else
echo "no datadir found in backup"
DATADIR=/var/www/nextcloud/data
DATADIR="$NCDIR"/data
sudo -u www-data php occ maintenance:mode --off
sudo -u www-data php occ files:scan --all
Expand All @@ -149,8 +150,10 @@ sed -i "s|^;\?upload_tmp_dir =.*$|upload_tmp_dir = $DATADIR/tmp|" /etc/php/7.0/f
sed -i "s|^;\?sys_temp_dir =.*$|sys_temp_dir = $DATADIR/tmp|" /etc/php/7.0/fpm/php.ini
# update fail2ban logpath
sed -i "s|logpath =.*|logpath = $DATADIR/nextcloud.log|" /etc/fail2ban/jail.conf
pgrep fail2ban &>/dev/null && service fail2ban restart
[[ ! -f /.docker-image ]] && {
sed -i "s|logpath =.*|logpath = $DATADIR/nextcloud.log|" /etc/fail2ban/jail.conf
pgrep fail2ban &>/dev/null && service fail2ban restart
}
# refresh nextcloud trusted domains
bash /usr/local/bin/nextcloud-domain.sh
Expand Down
8 changes: 7 additions & 1 deletion update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,15 @@ EOF
sudo -u www-data php /var/www/nextcloud/occ config:system:set mail_smtpmode --value="sendmail"
}

# update nc-restore
cd "$CONFDIR" &>/dev/null
install_script nc-backup.sh
install_script nc-restore.sh
cd - &>/dev/null

# install preview generator
sudo -u www-data php /var/www/nextcloud/occ app:install previewgenerator
sudo -u www-data php /var/www/nextcloud/occ app:enable previewgenerator

} # end - only live updates

exit 0
Expand Down

0 comments on commit f80ee23

Please sign in to comment.