From ecd919432cb4877da8a8fe0341234027ec58f183 Mon Sep 17 00:00:00 2001 From: Thomas Heller Date: Sun, 31 Oct 2021 17:30:46 +0100 Subject: [PATCH 01/10] Add nc-restic-s3-backup, nc-restic-s3-init, nc-restic-s3-restore --- bin/ncp/BACKUPS/nc-restic-s3-backup.sh | 90 +++++++++++++++++++++++ bin/ncp/BACKUPS/nc-restic-s3-init.sh | 63 ++++++++++++++++ bin/ncp/BACKUPS/nc-restic-s3-restore.sh | 82 +++++++++++++++++++++ etc/ncp-config.d/nc-restic-s3-backup.cfg | 32 ++++++++ etc/ncp-config.d/nc-restic-s3-init.cfg | 32 ++++++++ etc/ncp-config.d/nc-restic-s3-restore.cfg | 32 ++++++++ 6 files changed, 331 insertions(+) create mode 100644 bin/ncp/BACKUPS/nc-restic-s3-backup.sh create mode 100644 bin/ncp/BACKUPS/nc-restic-s3-init.sh create mode 100644 bin/ncp/BACKUPS/nc-restic-s3-restore.sh create mode 100644 etc/ncp-config.d/nc-restic-s3-backup.cfg create mode 100644 etc/ncp-config.d/nc-restic-s3-init.cfg create mode 100644 etc/ncp-config.d/nc-restic-s3-restore.cfg diff --git a/bin/ncp/BACKUPS/nc-restic-s3-backup.sh b/bin/ncp/BACKUPS/nc-restic-s3-backup.sh new file mode 100644 index 000000000..114f2144b --- /dev/null +++ b/bin/ncp/BACKUPS/nc-restic-s3-backup.sh @@ -0,0 +1,90 @@ +#!/bin/bash + +# Back up Nextcloud data to S3-compatible storage via restic +# +# Copyleft 2021 by Thomas Heller +# Copyleft 2017 by Ignacio Nunez Hernanz +# GPL licensed (see end of file) * Use at your own risk! +# +# More at: https://ownyourbits.com +# + +BASEDIR=/var/www + +install() +{ + apt-get update + apt-get install --no-install-recommends -y restic +} + +configure() +{ + [[ "$S3_BUCKET_URL" == "" ]] && { + echo "error: please specify S3 bucket URL" + return 1 + } + + [[ "$S3_KEY_ID" == "" ]] && { + echo "error: please specify S3 key ID" + return 2 + } + + [[ "$S3_SECRET_KEY" == "" ]] && { + echo "error: please specify S3 secret key" + return 3 + } + + [[ "$RESTIC_PASSWORD" == "" ]] && { + echo "error: please specify restic password" + return 4 + } + + save_maintenance_mode || { + echo "error: failed to activate Nextcloud maintenance mode" + return 5 + } + + local DATADIR + DATADIR=$( sudo -u www-data php /var/www/nextcloud/occ config:system:get datadirectory ) || { + echo -e "Error reading data directory. Is NextCloud running and configured?" + return 6 + } + + cd "$DATADIR" || { + echo "error: failed to change to data directory $DATADIR" + return 7 + } + + echo "backing up from $DATADIR" + + AWS_ACCESS_KEY_ID="$S3_KEY_ID" AWS_SECRET_ACCESS_KEY="$S3_SECRET_KEY" RESTIC_PASSWORD="$RESTIC_PASSWORD" restic -r "s3:$S3_BUCKET_URL/ncp-backup" --verbose backup . || { + echo "error: restic backup failed" + echo "notice: use nc-maintenance to disable maintenance mode anyway if desired" + return 8 + } + + echo "successfully created backup" + + restore_maintenance_mode || { + echo "error: failed to disabled Nextcloud maintenance mode" + echo "notice: backup has completed anyways" + return 8 + } +} + +# License +# +# This script is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This script is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this script; if not, write to the +# Free Software Foundation, Inc., 59 Temple Place, Suite 330, +# Boston, MA 02111-1307 USA diff --git a/bin/ncp/BACKUPS/nc-restic-s3-init.sh b/bin/ncp/BACKUPS/nc-restic-s3-init.sh new file mode 100644 index 000000000..71ec26cc1 --- /dev/null +++ b/bin/ncp/BACKUPS/nc-restic-s3-init.sh @@ -0,0 +1,63 @@ +#!/bin/bash + +# Prepare back up of Nextcloud data to S3-compatible storage via restic +# +# Copyleft 2021 by Thomas Heller +# Copyleft 2017 by Ignacio Nunez Hernanz +# GPL licensed (see end of file) * Use at your own risk! +# +# More at: https://ownyourbits.com +# + +install() +{ + apt-get update + apt-get install --no-install-recommends -y restic +} + +configure() +{ + [[ "$S3_BUCKET_URL" == "" ]] && { + echo "error: please specify S3 bucket URL" + return 1 + } + + [[ "$S3_KEY_ID" == "" ]] && { + echo "error: please specify S3 key ID" + return 2 + } + + [[ "$S3_SECRET_KEY" == "" ]] && { + echo "error: please specify S3 secret key" + return 3 + } + + [[ "$RESTIC_PASSWORD" == "" ]] && { + echo "error: please specify restic password" + return 4 + } + + AWS_ACCESS_KEY_ID="$S3_KEY_ID" AWS_SECRET_ACCESS_KEY="$S3_SECRET_KEY" RESTIC_PASSWORD="$RESTIC_PASSWORD" restic -r "s3:$S3_BUCKET_URL/ncp-backup" --verbose init || { + echo "error: failed to initialize restic repository" + return 5 + } + + echo "successfully initialized repository" +} + +# License +# +# This script is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This script is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this script; if not, write to the +# Free Software Foundation, Inc., 59 Temple Place, Suite 330, +# Boston, MA 02111-1307 USA diff --git a/bin/ncp/BACKUPS/nc-restic-s3-restore.sh b/bin/ncp/BACKUPS/nc-restic-s3-restore.sh new file mode 100644 index 000000000..cba5aa333 --- /dev/null +++ b/bin/ncp/BACKUPS/nc-restic-s3-restore.sh @@ -0,0 +1,82 @@ +#!/bin/bash + +# Restore Nextcloud data from S3-compatible storage via restic +# +# Copyleft 2021 by Thomas Heller +# Copyleft 2017 by Ignacio Nunez Hernanz +# GPL licensed (see end of file) * Use at your own risk! +# +# More at: https://ownyourbits.com +# + +install() +{ + apt-get update + apt-get install --no-install-recommends -y restic +} + +configure() +{ + [[ "$S3_BUCKET_URL" == "" ]] && { + echo "error: please specify S3 bucket URL" + return 1 + } + + [[ "$S3_KEY_ID" == "" ]] && { + echo "error: please specify S3 key ID" + return 2 + } + + [[ "$S3_SECRET_KEY" == "" ]] && { + echo "error: please specify S3 secret key" + return 3 + } + + [[ "$RESTIC_PASSWORD" == "" ]] && { + echo "error: please specify restic password" + return 4 + } + + save_maintenance_mode || { + echo "error: failed to activate Nextcloud maintenance mode" + return 5 + } + + local DATADIR + DATADIR=$( sudo -u www-data php /var/www/nextcloud/occ config:system:get datadirectory ) || { + echo -e "Error reading data directory. Is NextCloud running and configured?" + return 6 + } + + echo "restoring to $DATADIR" + + AWS_ACCESS_KEY_ID="$S3_KEY_ID" AWS_SECRET_ACCESS_KEY="$S3_SECRET_KEY" RESTIC_PASSWORD="$RESTIC_PASSWORD" restic -r "s3:$S3_BUCKET_URL/ncp-backup" --verbose restore latest --target "$DATADIR" || { + echo "error: restic restore failed" + return 7 + } + + echo "successfully restored backup" + + restore_maintenance_mode || { + echo "error: failed to disabled Nextcloud maintenance mode" + echo "notice: backup has been restored anyways" + return 8 + } +} + +# License +# +# This script is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This script is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this script; if not, write to the +# Free Software Foundation, Inc., 59 Temple Place, Suite 330, +# Boston, MA 02111-1307 USA diff --git a/etc/ncp-config.d/nc-restic-s3-backup.cfg b/etc/ncp-config.d/nc-restic-s3-backup.cfg new file mode 100644 index 000000000..b047e0a69 --- /dev/null +++ b/etc/ncp-config.d/nc-restic-s3-backup.cfg @@ -0,0 +1,32 @@ +{ + "id": "nc-restic-s3-backup", + "name": "nc-restic-s3-backup", + "title": "nc-restic-s3-backup", + "description": "Back up Nextcloud data to S3-compatible storage via restic", + "info": "Note that this backs up only the Nextcloud data directory, not the database.\n\nBefore using this, you may need to prepare a restic repository using nc-restic-s3-init, if you haven't already done so.\n\nPlease enter the S3 bucket access details as well as an encryption password.\nThe password is required to retrieve the data later on!\nNOTE: The password is NOT stored here for security reasons!", + "infotitle": "", + "params": [ + { + "id": "S3_BUCKET_URL", + "name": "S3 bucket URL", + "suggest": "https:///" + }, + { + "id": "S3_KEY_ID", + "name": "S3 key ID", + "suggest": "S3 key ID" + }, + { + "id": "S3_SECRET_KEY", + "name": "S3 secret key", + "suggest": "S3 secrey key", + "type": "password" + }, + { + "id": "RESTIC_PASSWORD", + "name": "restic password", + "suggest": "restic password", + "type": "password" + } + ] +} diff --git a/etc/ncp-config.d/nc-restic-s3-init.cfg b/etc/ncp-config.d/nc-restic-s3-init.cfg new file mode 100644 index 000000000..38ecf6160 --- /dev/null +++ b/etc/ncp-config.d/nc-restic-s3-init.cfg @@ -0,0 +1,32 @@ +{ + "id": "nc-restic-s3-init", + "name": "nc-restic-s3-init", + "title": "nc-restic-s3-init", + "description": "Prepare back up of Nextcloud data to S3-compatible storage via restic", + "info": "Prepare a restic repository for use with nc-restic-s3, if you haven't already done so.\nYou only need to do this once.\n\nPlease enter the S3 bucket access details as well as an encryption password.\nThe password is required to retrieve the data later on!\nNOTE: The password is NOT stored here for security reasons!", + "infotitle": "", + "params": [ + { + "id": "S3_BUCKET_URL", + "name": "S3 bucket URL", + "suggest": "https:///" + }, + { + "id": "S3_KEY_ID", + "name": "S3 key ID", + "suggest": "S3 key ID" + }, + { + "id": "S3_SECRET_KEY", + "name": "S3 secret key", + "suggest": "S3 secrey key", + "type": "password" + }, + { + "id": "RESTIC_PASSWORD", + "name": "restic password", + "suggest": "restic password", + "type": "password" + } + ] +} diff --git a/etc/ncp-config.d/nc-restic-s3-restore.cfg b/etc/ncp-config.d/nc-restic-s3-restore.cfg new file mode 100644 index 000000000..f779a4eb5 --- /dev/null +++ b/etc/ncp-config.d/nc-restic-s3-restore.cfg @@ -0,0 +1,32 @@ +{ + "id": "nc-restic-s3-restore", + "name": "nc-restic-s3-restore", + "title": "nc-restic-s3-restore", + "description": "Restore Nextcloud data from S3-compatible storage via restic", + "info": "WARNING: This will overwrite existing files in your current Nextcloud data directory with files from latest backup created by nc-restic-s3-backup!\n\nNote that files which already exist in the data directory, but are not present in the backup, are not deleted.", + "infotitle": "", + "params": [ + { + "id": "S3_BUCKET_URL", + "name": "S3 bucket URL", + "suggest": "https:///" + }, + { + "id": "S3_KEY_ID", + "name": "S3 key ID", + "suggest": "S3 key ID" + }, + { + "id": "S3_SECRET_KEY", + "name": "S3 secret key", + "suggest": "S3 secrey key", + "type": "password" + }, + { + "id": "RESTIC_PASSWORD", + "name": "restic password", + "suggest": "restic password", + "type": "password" + } + ] +} From cca2325b04152bf352d4ad6c2e1d1a57dede871d Mon Sep 17 00:00:00 2001 From: Thomas Heller Date: Mon, 1 Nov 2021 07:54:17 +0100 Subject: [PATCH 02/10] Add prune option to nc-restic-s3-backup --- bin/ncp/BACKUPS/nc-restic-s3-backup.sh | 27 +++++++++++++++++++----- etc/ncp-config.d/nc-restic-s3-backup.cfg | 8 ++++++- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/bin/ncp/BACKUPS/nc-restic-s3-backup.sh b/bin/ncp/BACKUPS/nc-restic-s3-backup.sh index 114f2144b..9e6384888 100644 --- a/bin/ncp/BACKUPS/nc-restic-s3-backup.sh +++ b/bin/ncp/BACKUPS/nc-restic-s3-backup.sh @@ -39,20 +39,25 @@ configure() return 4 } + [[ "$BACKUPLIMIT" == "" ]] && { + echo "error: please specify number of days to keep" + return 5 + } + save_maintenance_mode || { echo "error: failed to activate Nextcloud maintenance mode" - return 5 + return 6 } local DATADIR DATADIR=$( sudo -u www-data php /var/www/nextcloud/occ config:system:get datadirectory ) || { echo -e "Error reading data directory. Is NextCloud running and configured?" - return 6 + return 7 } cd "$DATADIR" || { echo "error: failed to change to data directory $DATADIR" - return 7 + return 8 } echo "backing up from $DATADIR" @@ -60,15 +65,27 @@ configure() AWS_ACCESS_KEY_ID="$S3_KEY_ID" AWS_SECRET_ACCESS_KEY="$S3_SECRET_KEY" RESTIC_PASSWORD="$RESTIC_PASSWORD" restic -r "s3:$S3_BUCKET_URL/ncp-backup" --verbose backup . || { echo "error: restic backup failed" echo "notice: use nc-maintenance to disable maintenance mode anyway if desired" - return 8 + return 9 } echo "successfully created backup" + [[ "$BACKUPLIMIT" -gt 0 ]] && { + echo "pruning old backups (except for the last $BACKUPLIMIT days)" + + AWS_ACCESS_KEY_ID="$S3_KEY_ID" AWS_SECRET_ACCESS_KEY="$S3_SECRET_KEY" RESTIC_PASSWORD="$RESTIC_PASSWORD" restic -r "s3:$S3_BUCKET_URL/ncp-backup" --verbose forget --keep-daily "$BACKUPLIMIT" --prune || { + echo "error: restic prune failed" + echo "notice: use nc-maintenance to disable maintenance mode anyway if desired" + return 10 + } + + echo "successfully pruned old backups" + } + restore_maintenance_mode || { echo "error: failed to disabled Nextcloud maintenance mode" echo "notice: backup has completed anyways" - return 8 + return 11 } } diff --git a/etc/ncp-config.d/nc-restic-s3-backup.cfg b/etc/ncp-config.d/nc-restic-s3-backup.cfg index b047e0a69..f757490e9 100644 --- a/etc/ncp-config.d/nc-restic-s3-backup.cfg +++ b/etc/ncp-config.d/nc-restic-s3-backup.cfg @@ -3,7 +3,7 @@ "name": "nc-restic-s3-backup", "title": "nc-restic-s3-backup", "description": "Back up Nextcloud data to S3-compatible storage via restic", - "info": "Note that this backs up only the Nextcloud data directory, not the database.\n\nBefore using this, you may need to prepare a restic repository using nc-restic-s3-init, if you haven't already done so.\n\nPlease enter the S3 bucket access details as well as an encryption password.\nThe password is required to retrieve the data later on!\nNOTE: The password is NOT stored here for security reasons!", + "info": "Note that this backs up only the Nextcloud data directory, not the database.\n\nBefore using this, you may need to prepare a restic repository using nc-restic-s3-init, if you haven't already done so.\n\nPlease enter the S3 bucket access details as well as an encryption password.\nThe password is required to retrieve the data later on!\nNOTE: The password is NOT stored here for security reasons!\n\nIf you specify a number of days to keep, all backups older than the number of days will be deleted.\nIn addition, only the latest backup of each day will be kept.", "infotitle": "", "params": [ { @@ -27,6 +27,12 @@ "name": "restic password", "suggest": "restic password", "type": "password" + }, + { + "id": "BACKUPLIMIT", + "name": "Number of days to keep (0 for all)", + "value": "0", + "suggest": "0" } ] } From 1cc6fa0786cb143077b979121b7d963ed28d48de Mon Sep 17 00:00:00 2001 From: Thomas Heller Date: Mon, 1 Nov 2021 08:05:02 +0100 Subject: [PATCH 03/10] nc-restic-s3-backup: Log successful backup in case prune failed --- bin/ncp/BACKUPS/nc-restic-s3-backup.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/bin/ncp/BACKUPS/nc-restic-s3-backup.sh b/bin/ncp/BACKUPS/nc-restic-s3-backup.sh index 9e6384888..50cc4fa21 100644 --- a/bin/ncp/BACKUPS/nc-restic-s3-backup.sh +++ b/bin/ncp/BACKUPS/nc-restic-s3-backup.sh @@ -75,6 +75,7 @@ configure() AWS_ACCESS_KEY_ID="$S3_KEY_ID" AWS_SECRET_ACCESS_KEY="$S3_SECRET_KEY" RESTIC_PASSWORD="$RESTIC_PASSWORD" restic -r "s3:$S3_BUCKET_URL/ncp-backup" --verbose forget --keep-daily "$BACKUPLIMIT" --prune || { echo "error: restic prune failed" + echo "notice: backup has completed anyways" echo "notice: use nc-maintenance to disable maintenance mode anyway if desired" return 10 } From d1bf872c023c90319058d821f898fd6452406869 Mon Sep 17 00:00:00 2001 From: Thomas Heller Date: Mon, 1 Nov 2021 08:19:43 +0100 Subject: [PATCH 04/10] Add force-unlock option to nc-restic-s3-backup --- bin/ncp/BACKUPS/nc-restic-s3-backup.sh | 17 +++++++++++++++-- etc/ncp-config.d/nc-restic-s3-backup.cfg | 8 +++++++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/bin/ncp/BACKUPS/nc-restic-s3-backup.sh b/bin/ncp/BACKUPS/nc-restic-s3-backup.sh index 50cc4fa21..398ac12a8 100644 --- a/bin/ncp/BACKUPS/nc-restic-s3-backup.sh +++ b/bin/ncp/BACKUPS/nc-restic-s3-backup.sh @@ -71,13 +71,26 @@ configure() echo "successfully created backup" [[ "$BACKUPLIMIT" -gt 0 ]] && { + [[ "$FORCE_UNLOCK" == "yes" ]] && { + echo "forcefully unlocking repository ..." + + AWS_ACCESS_KEY_ID="$S3_KEY_ID" AWS_SECRET_ACCESS_KEY="$S3_SECRET_KEY" RESTIC_PASSWORD="$RESTIC_PASSWORD" restic -r "s3:$S3_BUCKET_URL/ncp-backup" --verbose unlock || { + echo "error: restic unlock failed" + echo "notice: backup has completed anyways" + echo "notice: use nc-maintenance to disable maintenance mode anyway if desired" + return 10 + } + + echo "successfully unlocked repository" + } + echo "pruning old backups (except for the last $BACKUPLIMIT days)" AWS_ACCESS_KEY_ID="$S3_KEY_ID" AWS_SECRET_ACCESS_KEY="$S3_SECRET_KEY" RESTIC_PASSWORD="$RESTIC_PASSWORD" restic -r "s3:$S3_BUCKET_URL/ncp-backup" --verbose forget --keep-daily "$BACKUPLIMIT" --prune || { echo "error: restic prune failed" echo "notice: backup has completed anyways" echo "notice: use nc-maintenance to disable maintenance mode anyway if desired" - return 10 + return 11 } echo "successfully pruned old backups" @@ -86,7 +99,7 @@ configure() restore_maintenance_mode || { echo "error: failed to disabled Nextcloud maintenance mode" echo "notice: backup has completed anyways" - return 11 + return 12 } } diff --git a/etc/ncp-config.d/nc-restic-s3-backup.cfg b/etc/ncp-config.d/nc-restic-s3-backup.cfg index f757490e9..ccb450bda 100644 --- a/etc/ncp-config.d/nc-restic-s3-backup.cfg +++ b/etc/ncp-config.d/nc-restic-s3-backup.cfg @@ -3,7 +3,7 @@ "name": "nc-restic-s3-backup", "title": "nc-restic-s3-backup", "description": "Back up Nextcloud data to S3-compatible storage via restic", - "info": "Note that this backs up only the Nextcloud data directory, not the database.\n\nBefore using this, you may need to prepare a restic repository using nc-restic-s3-init, if you haven't already done so.\n\nPlease enter the S3 bucket access details as well as an encryption password.\nThe password is required to retrieve the data later on!\nNOTE: The password is NOT stored here for security reasons!\n\nIf you specify a number of days to keep, all backups older than the number of days will be deleted.\nIn addition, only the latest backup of each day will be kept.", + "info": "Note that this backs up only the Nextcloud data directory, not the database.\n\nBefore using this, you may need to prepare a restic repository using nc-restic-s3-init, if you haven't already done so.\n\nPlease enter the S3 bucket access details as well as an encryption password.\nThe password is required to retrieve the data later on!\nNOTE: The password is NOT stored here for security reasons!\n\nIf you specify a number of days to keep, all backups older than the number of days will be deleted.\nIn addition, only the latest backup of each day will be kept.\n\nIf restic reports Fatal: unable to create lock in backend and you're sure that no other process is running, use "Force unlock".", "infotitle": "", "params": [ { @@ -33,6 +33,12 @@ "name": "Number of days to keep (0 for all)", "value": "0", "suggest": "0" + }, + { + "id": "FORCE_UNLOCK", + "name": "Force unlock", + "value": "no", + "type": "bool" } ] } From f2b9fe791087d39eb6eaeba701f9cdced75b42a6 Mon Sep 17 00:00:00 2001 From: Thomas Heller Date: Mon, 1 Nov 2021 08:31:37 +0100 Subject: [PATCH 05/10] Add stats (bucket utilization) to nc-restic-s3-backup --- bin/ncp/BACKUPS/nc-restic-s3-backup.sh | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/bin/ncp/BACKUPS/nc-restic-s3-backup.sh b/bin/ncp/BACKUPS/nc-restic-s3-backup.sh index 398ac12a8..18ba7adc0 100644 --- a/bin/ncp/BACKUPS/nc-restic-s3-backup.sh +++ b/bin/ncp/BACKUPS/nc-restic-s3-backup.sh @@ -101,6 +101,16 @@ configure() echo "notice: backup has completed anyways" return 12 } + + echo "gathering stats (How much space do the backups take up in the S3 bucket?) ..." + + AWS_ACCESS_KEY_ID="$S3_KEY_ID" AWS_SECRET_ACCESS_KEY="$S3_SECRET_KEY" RESTIC_PASSWORD="$RESTIC_PASSWORD" restic -r "s3:$S3_BUCKET_URL/ncp-backup" --verbose stats --mode raw-data || { + echo "error: restic stats failed" + echo "notice: backup has completed anyways" + return 13 + } + + echo "backup complete" } # License From d4814d64d65c4a8fae55868900c13d9649c00bcf Mon Sep 17 00:00:00 2001 From: Thomas Heller Date: Mon, 1 Nov 2021 09:00:10 +0100 Subject: [PATCH 06/10] nc-restic-s3-backup: exclude files (opcache) --- bin/ncp/BACKUPS/nc-restic-s3-backup.sh | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/bin/ncp/BACKUPS/nc-restic-s3-backup.sh b/bin/ncp/BACKUPS/nc-restic-s3-backup.sh index 18ba7adc0..379702446 100644 --- a/bin/ncp/BACKUPS/nc-restic-s3-backup.sh +++ b/bin/ncp/BACKUPS/nc-restic-s3-backup.sh @@ -62,7 +62,10 @@ configure() echo "backing up from $DATADIR" - AWS_ACCESS_KEY_ID="$S3_KEY_ID" AWS_SECRET_ACCESS_KEY="$S3_SECRET_KEY" RESTIC_PASSWORD="$RESTIC_PASSWORD" restic -r "s3:$S3_BUCKET_URL/ncp-backup" --verbose backup . || { + AWS_ACCESS_KEY_ID="$S3_KEY_ID" AWS_SECRET_ACCESS_KEY="$S3_SECRET_KEY" RESTIC_PASSWORD="$RESTIC_PASSWORD" restic -r "s3:$S3_BUCKET_URL/ncp-backup" --verbose --exclude-file=/dev/stdin backup . < Date: Mon, 1 Nov 2021 09:01:17 +0100 Subject: [PATCH 07/10] nc-restic-s3-backup: show elapsed time --- bin/ncp/BACKUPS/nc-restic-s3-backup.sh | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/bin/ncp/BACKUPS/nc-restic-s3-backup.sh b/bin/ncp/BACKUPS/nc-restic-s3-backup.sh index 379702446..65646f139 100644 --- a/bin/ncp/BACKUPS/nc-restic-s3-backup.sh +++ b/bin/ncp/BACKUPS/nc-restic-s3-backup.sh @@ -19,6 +19,8 @@ install() configure() { + local start=$(date +%s) + [[ "$S3_BUCKET_URL" == "" ]] && { echo "error: please specify S3 bucket URL" return 1 @@ -113,7 +115,9 @@ EXCLUDES_LIST return 13 } - echo "backup complete" + local end=$(date +%s) + + echo "backup complete after $((($end-$start)/60)) minute(s)" } # License From 7e7284157dba54f17fe218ea9be795989e3e077b Mon Sep 17 00:00:00 2001 From: Thomas Heller Date: Mon, 1 Nov 2021 13:24:56 +0100 Subject: [PATCH 08/10] nc-restic-s3-restore: show elapsed time --- bin/ncp/BACKUPS/nc-restic-s3-restore.sh | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/bin/ncp/BACKUPS/nc-restic-s3-restore.sh b/bin/ncp/BACKUPS/nc-restic-s3-restore.sh index cba5aa333..974003412 100644 --- a/bin/ncp/BACKUPS/nc-restic-s3-restore.sh +++ b/bin/ncp/BACKUPS/nc-restic-s3-restore.sh @@ -17,6 +17,8 @@ install() configure() { + local start=$(date +%s) + [[ "$S3_BUCKET_URL" == "" ]] && { echo "error: please specify S3 bucket URL" return 1 @@ -62,6 +64,10 @@ configure() echo "notice: backup has been restored anyways" return 8 } + + local end=$(date +%s) + + echo "restore complete after $((($end-$start)/60)) minute(s)" } # License From 91e512dcc575fa32299660fb84de665d06c8d8b4 Mon Sep 17 00:00:00 2001 From: Thomas Heller Date: Mon, 1 Nov 2021 14:09:39 +0100 Subject: [PATCH 09/10] nc-restic-s3-backup/restore: allow database backup --- bin/ncp/BACKUPS/nc-restic-s3-backup.sh | 44 ++++++++++++++++--- bin/ncp/BACKUPS/nc-restic-s3-restore.sh | 53 ++++++++++++++++++++++- etc/ncp-config.d/nc-restic-s3-backup.cfg | 6 +++ etc/ncp-config.d/nc-restic-s3-restore.cfg | 6 +++ 4 files changed, 102 insertions(+), 7 deletions(-) diff --git a/bin/ncp/BACKUPS/nc-restic-s3-backup.sh b/bin/ncp/BACKUPS/nc-restic-s3-backup.sh index 65646f139..37a6a91ca 100644 --- a/bin/ncp/BACKUPS/nc-restic-s3-backup.sh +++ b/bin/ncp/BACKUPS/nc-restic-s3-backup.sh @@ -64,17 +64,51 @@ configure() echo "backing up from $DATADIR" + if [[ "$INCLUDE_DATABASE" != "yes" ]]; then + echo "info: database is not included in backup" + else + echo "preparing database dump for backup ..." + + [[ -e ncdatabase-restic-dump.sql ]] && { + echo "warning: stale database dump ncdatabase-restic-dump.sql ($(stat --format='%s bytes, last modified: %y' ncdatabase-restic-dump.sql)) still exists, overwriting!" + } + + mysqldump -u root --single-transaction nextcloud > ncdatabase-restic-dump.sql || { + echo "error: mysqldump failed" + echo "notice: use nc-maintenance to disable maintenance mode anyway if desired" + return 9 + } + + echo "successfully created database dump for backup ($(stat --format='%s bytes' ncdatabase-restic-dump.sql))" + fi + + echo "starting backup ..." + AWS_ACCESS_KEY_ID="$S3_KEY_ID" AWS_SECRET_ACCESS_KEY="$S3_SECRET_KEY" RESTIC_PASSWORD="$RESTIC_PASSWORD" restic -r "s3:$S3_BUCKET_URL/ncp-backup" --verbose --exclude-file=/dev/stdin backup . </dev/null" + + AWS_ACCESS_KEY_ID="$S3_KEY_ID" AWS_SECRET_ACCESS_KEY="$S3_SECRET_KEY" RESTIC_PASSWORD="$RESTIC_PASSWORD" restic -r "s3:$S3_BUCKET_URL/ncp-backup" --verbose ls latest | grep '^/ncdatabase-restic-dump\.sql$' >/dev/null || { + echo "error: backup does not contain a database dump (ncdatabase-restic-dump.sql)" + echo "notice: if you want to restore the backup without the database, uncheck the \"Include database\" option" + echo "notice: use nc-maintenance to disable maintenance mode anyway if desired" + return 8 + } + + echo "preparing database for restore ..." + + local DBADMIN=ncadmin + local DBPASSWD="$( grep password /root/.my.cnf | sed 's|password=||' )" + + mysql -u root < Date: Tue, 2 Nov 2021 07:18:16 +0100 Subject: [PATCH 10/10] nc-restic-s3-backup: Remove unneeded BASEDIR declaration --- bin/ncp/BACKUPS/nc-restic-s3-backup.sh | 2 -- 1 file changed, 2 deletions(-) diff --git a/bin/ncp/BACKUPS/nc-restic-s3-backup.sh b/bin/ncp/BACKUPS/nc-restic-s3-backup.sh index 37a6a91ca..b92cb54e4 100644 --- a/bin/ncp/BACKUPS/nc-restic-s3-backup.sh +++ b/bin/ncp/BACKUPS/nc-restic-s3-backup.sh @@ -9,8 +9,6 @@ # More at: https://ownyourbits.com # -BASEDIR=/var/www - install() { apt-get update