]> git.p6c8.net - nextcloud-backup-restore.git/commitdiff
Merge pull request 'Setup script' (#19) from auto-setup into master
authorDecaTec <decatec@noreply.codeberg.org>
Mon, 22 Jun 2020 12:47:44 +0000 (14:47 +0200)
committerDecaTec <decatec@noreply.codeberg.org>
Mon, 22 Jun 2020 12:47:44 +0000 (14:47 +0200)
NextcloudBackup.sh
NextcloudRestore.sh
README.md
setup.sh [new file with mode: 0755]

index a2467a7ae3b74cdef56f2a14fef2b664379f7f79..47abd1329cabc5839c849d351c643657e3eab486 100644 (file)
@@ -3,7 +3,7 @@
 #
 # Bash script for creating backups of Nextcloud.
 #
-# Version 1.1.1
+# Version 2.0.0
 #
 # Usage:
 #      - With backup directory specified in the script:  ./NextcloudBackup.sh
@@ -195,7 +195,7 @@ if [ "${databaseSystem,,}" = "mysql" ] || [ "${databaseSystem,,}" = "mariadb" ];
 
        echo "Done"
        echo
-elif [ "${databaseSystem,,}" = "postgresql" ]; then
+elif [ "${databaseSystem,,}" = "postgresql" ] || [ "${databaseSystem,,}" = "pgsql" ]; then
        echo "Backup Nextcloud database (PostgreSQL)..."
 
        if ! [ -x "$(command -v pg_dump)" ]; then
index 6f983fb57b4c9236d5a917e8faa4b6228b39ebaa..90ec5453b015d059d54529f3c079ffc679368c28 100644 (file)
@@ -3,7 +3,7 @@
 #
 # Bash script for restoring backups of Nextcloud.
 #
-# Version 1.1.1
+# Version 2.0.0
 #
 # Usage:
 #   - With backup directory specified in the script: ./NextcloudRestore.sh <BackupName> (e.g. ./NextcloudRestore.sh 20170910_132703)
@@ -111,7 +111,7 @@ if [ "${databaseSystem,,}" = "mysql" ] || [ "${databaseSystem,,}" = "mariadb" ];
         errorecho "Cancel restore"
         exit 1
     fi
-elif [ "${databaseSystem,,}" = "postgresql" ]; then
+elif [ "${databaseSystem,,}" = "postgresql" ] || [ "${databaseSystem,,}" = "pgsql" ]; then
     if ! [ -x "$(command -v psql)" ]; then
                errorecho "ERROR: PostgreSQL not installed (command psql not found)."
                errorecho "ERROR: No restore of database possible!"
@@ -206,7 +206,7 @@ if [ "${databaseSystem,,}" = "mysql" ] || [ "${databaseSystem,,}" = "mariadb" ];
     mysql -h localhost -u "${dbUser}" -p"${dbPassword}" -e "CREATE DATABASE ${nextcloudDatabase} CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci"
     # TODO: Use this if the database from the backup DOES NOT use UTF8 with multibyte support (e.g. for emoijs in filenames):
     #mysql -h localhost -u "${dbUser}" -p"${dbPassword}" -e "CREATE DATABASE ${nextcloudDatabase}"
-elif [ "${databaseSystem,,}" = "postgresql" ]; then
+elif [ "${databaseSystem,,}" = "postgresql" ] || [ "${databaseSystem,,}" = "pgsql" ]; then
     sudo -u postgres psql -c "CREATE DATABASE ${nextcloudDatabase} WITH OWNER ${dbUser} TEMPLATE template0 ENCODING \"UTF8\";"
 fi
 
@@ -217,7 +217,7 @@ echo "Restoring backup DB..."
 
 if [ "${databaseSystem,,}" = "mysql" ] || [ "${databaseSystem,,}" = "mariadb" ]; then
        mysql -h localhost -u "${dbUser}" -p"${dbPassword}" "${nextcloudDatabase}" < "${currentRestoreDir}/${fileNameBackupDb}"
-elif [ "${databaseSystem,,}" = "postgresql" ]; then
+elif [ "${databaseSystem,,}" = "postgresql" ] || [ "${databaseSystem,,}" = "pgsql" ]; then
        sudo -u postgres psql "${nextcloudDatabase}" < "${currentRestoreDir}/${fileNameBackupDb}"
 fi
 
index 7c349711f5abf148fb13dd4477810d10e5da72d7..54681bf9d7f576167dea57097c624fa0534c7884 100644 (file)
--- a/README.md
+++ b/README.md
@@ -17,11 +17,11 @@ The scripts take care of these items to backup automatically.
 **Important:**
 
 - After cloning or downloading the repository, you'll have to edit the scripts so that they represent your current Nextcloud installation (directories, users, etc.). All values which need to be customized are marked with *TODO* in the script's comments.
-- The scripts assume that Nextcloud's data directory is *not* a subdirectory of the Nextcloud installation (file directory). The general recommendation is that the data directory should not be located somewhere in the web folder of your webserver (usually */var/www/*), but in a different folder (e.g. */var/nextcloud_data*). For more information, see [here](https://docs.nextcloud.com/server/16/admin_manual/installation/installation_wizard.html#data-directory-location-label).
+- The scripts assume that Nextcloud's data directory is *not* a subdirectory of the Nextcloud installation (file directory). The general recommendation is that the data directory should not be located somewhere in the web folder of your webserver (usually */var/www/*), but in a different folder (e.g. */var/nextcloud_data*). For more information, see [here](https://docs.nextcloud.com/server/latest/admin_manual/installation/installation_wizard.html#data-directory-location-label).
 - However, if your data directory *is* located under the Nextcloud file directory, you'll have to change the scripts so that the data directory is not part of the backup/restore (otherwise, it would be copied twice).
 - The scripts only backup the Nextcloud data directory and can backup a local external storage mounted into Nextcloud. If you have any other external storage mounted in Nextcloud (e.g. FTP), these files have to be handled separately.
 - The scripts assume that you are using MySQL/MariaDB as database for Nextcloud. However, it also supports PostreSQL databases. In this case you have to uncomment the parts of backing up/restoring the database.
-- You should have enabled 4 byte support (see [Nextcloud Administration Manual](https://docs.nextcloud.com/server/16/admin_manual/configuration_database/mysql_4byte_support.html)) on your Nextcloud database. Otherwise, when you have *not* enabled 4 byte support, you have to edit the restore script, so that the database is not created with 4 byte support enabled.
+- You should have enabled 4 byte support (see [Nextcloud Administration Manual](https://docs.nextcloud.com/server/latest/admin_manual/configuration_database/mysql_4byte_support.html)) on your Nextcloud database. Otherwise, when you have *not* enabled 4 byte support, you have to edit the restore script, so that the database is not created with 4 byte support enabled.
 
 ## Backup
 
@@ -32,4 +32,8 @@ The backup script can also be called with a parameter specifiying the main backu
 ## Restore
 
 For restore, just call *NextcloudRestore.sh*. This script expects at least one parameter specifying the name of the backup to be restored. In our example, this would be *20170910_132703* (the time stamp of the backup created before). The full command for a restore would be *./NextcloudRestore.sh 20170910_132703*.
-You can also specify the main backup directory with a second parameter, e.g. *./NextcloudRestore.sh 20170910_132703 /media/hdd/nextcloud_backup*.
\ No newline at end of file
+You can also specify the main backup directory with a second parameter, e.g. *./NextcloudRestore.sh 20170910_132703 /media/hdd/nextcloud_backup*.
+
+## Automated setup
+
+Next to the backup/restore scripts, there is another script (`setup.sh`). The setup script gathers some information and uses the [OCC command](https://docs.nextcloud.com/server/latest/admin_manual/configuration_server/occ_command.html) in order to set the required variables in the backup/restore scripts automatically. This way, the configuration of the backup/restore scripts can be automated to some extend.
\ No newline at end of file
diff --git a/setup.sh b/setup.sh
new file mode 100755 (executable)
index 0000000..6b1dfbd
--- /dev/null
+++ b/setup.sh
@@ -0,0 +1,160 @@
+#!/bin/bash
+
+#
+# Bash script an easy setup of NextcloudBackup.sh and NextcloudRestore.sh
+#
+# Version 2.0.0
+#
+# Usage:
+#      - call the setup.sh script
+#   - Enter the required information
+#   - You NextcloudBackup.sh and NextcloudRestore.sh scripts will be tailored to match you installation.
+#
+# The script is based on an installation of Nextcloud using nginx and MariaDB, see https://decatec.de/home-server/nextcloud-auf-ubuntu-server-18-04-lts-mit-nginx-mariadb-php-lets-encrypt-redis-und-fail2ban/
+#
+
+#
+# IMPORTANT
+# The setup.sh script automated the configuration for the backup/restore scripts.
+# However, you should always check the backup/restore scripts BEFORE executing these!
+#
+
+#
+# Pre defined variables
+#
+backupMainDir='/media/hdd/nextcloud_backup'
+nextcloudFileDir='/var/www/nextcloud'
+webserverUser='www-data'
+webserverServiceName='nginx'
+
+#
+# Gather information
+#
+clear
+
+echo "Enter the directory to which the backups should be saved."
+echo "Default: ${backupMainDir}"
+echo ""
+read -p "Enter a directory or press ENTER if the backup directory should be ${backupMainDir}: " BACKUPMAINDIR
+
+[ -z "$BACKUPMAINDIR" ] ||  backupMainDir=$BACKUPMAINDIR
+clear
+
+echo "Enter the path to the Nextcloud file directory."
+echo "Usually: ${nextcloudFileDir}"
+echo ""
+read -p "Enter a directory or press ENTER if the file directory is ${nextcloudFileDir}: " NEXTCLOUDFILEDIRECTORY
+
+[ -z "$NEXTCLOUDFILEDIRECTORY" ] ||  nextcloudFileDir=$NEXTCLOUDFILEDIRECTORY
+clear
+
+echo "Enter the webserver user."
+echo "Usually: ${webserverUser}"
+echo ""
+read -p "Enter an new user or press ENTER if the webserver user is ${webserverUser}: " WEBSERVERUSER
+
+[ -z "$WEBSERVERUSER" ] ||  webserverUser=$WEBSERVERUSER
+clear
+
+echo "Enter the webserver service name."
+echo "Usually: nginx or apache2"
+echo ""
+read -p "Enter an new webserver service name or press ENTER if the webserver service name is ${webserverServiceName}: " WEBSERVERSERVICENAME
+
+[ -z "$WEBSERVERSERVICENAME" ] ||  webserverServiceName=$WEBSERVERSERVICENAME
+clear
+
+echo "Backup directory: ${backupMainDir}"
+echo "Nextcloud file directory: ${nextcloudFileDir}"
+echo "Webserver user: ${webserverUser}"
+echo "Webserver service name: ${webserverServiceName}"
+echo ""
+read -p "Is the information correct? [y/N] " CORRECTINFO
+
+if [ "$CORRECTINFO" != 'y' ] ; then
+  echo "ABORTING!"
+  echo "No file has been altered."
+  exit 1
+fi
+
+function occ_get() {
+       sudo -u "${webserverUser}" php ${nextcloudFileDir}/occ config:system:get "$1"
+}
+
+# Make test call to OCC
+occ_get datadirectory
+
+if [ $? -ne 0 ]; then
+    echo "Error calling OCC: Please check if the information provided was correct."
+       echo "ABORTING!"
+       echo "No file has been altered."
+       exit 1
+fi
+
+#
+# Read data from OCC and write to backup/restore scripts.
+#
+
+echo ""
+echo ""
+echo "Modifying NextcloudBackup.sh and NextcloudRestore.sh to match your installation..."
+echo ""
+
+# Backup main dir
+sed -i "s@^    backupMainDir.*@    backupMainDir='$backupMainDir'@" ./NextcloudBackup.sh
+sed -i "s@^    backupMainDir.*@    backupMainDir='$backupMainDir'@" ./NextcloudRestore.sh
+
+# Nextcloud file dir
+sed -i "s@^nextcloudFileDir.*@nextcloudFileDir='$nextcloudFileDir'@" ./NextcloudBackup.sh
+sed -i "s@^nextcloudFileDir.*@nextcloudFileDir='$nextcloudFileDir'@" ./NextcloudRestore.sh
+
+# Nextcloud data dir
+nextcloudDataDir=$(occ_get datadirectory)
+
+sed -i "s@^nextcloudDataDir=.*@nextcloudDataDir='$nextcloudDataDir'@" ./NextcloudBackup.sh
+sed -i "s@^nextcloudDataDir=.*@nextcloudDataDir='$nextcloudDataDir'@" ./NextcloudRestore.sh
+
+# Webserver service name
+sed -i "s/^webserverServiceName.*/webserverServiceName='$webserverServiceName'/" ./NextcloudBackup.sh
+sed -i "s/^webserverServiceName.*/webserverServiceName='$webserverServiceName'/" ./NextcloudRestore.sh
+
+# Webserver user
+sed -i "s/^webserverUser.*/webserverUser='$webserverUser'/" ./NextcloudBackup.sh
+sed -i "s/^webserverUser.*/webserverUser='$webserverUser'/" ./NextcloudRestore.sh
+
+# Database system
+databaseSystem=$(occ_get dbtype)
+
+# PostgreSQL is identified as pgsql
+if [ "${databaseSystem,,}" = "pgsql" ]; then 
+  databaseSystem='postgresql'; 
+fi
+
+sed -i "s/^databaseSystem.*/databaseSystem='$databaseSystem'/" ./NextcloudBackup.sh
+sed -i "s/^databaseSystem.*/databaseSystem='$databaseSystem'/" ./NextcloudRestore.sh
+
+# Database
+nextcloudDatabase=$(occ_get dbname)
+
+sed -i "s/^nextcloudDatabase.*/nextcloudDatabase='$nextcloudDatabase'/" ./NextcloudBackup.sh
+sed -i "s/^nextcloudDatabase.*/nextcloudDatabase='$nextcloudDatabase'/" ./NextcloudRestore.sh
+
+# Database user
+dbUser=$(occ_get dbuser)
+
+sed -i "s/^dbUser.*/dbUser='$dbUser'/" ./NextcloudBackup.sh
+sed -i "s/^dbUser.*/dbUser='$dbUser'/" ./NextcloudRestore.sh
+
+# Database password
+dbPassword=$(occ_get dbpassword)
+
+sed -i "s/^dbPassword.*/dbPassword='$dbPassword'/" ./NextcloudBackup.sh
+sed -i "s/^dbPassword.*/dbPassword='$dbPassword'/" ./NextcloudRestore.sh
+
+echo ""
+echo "Done!"
+echo ""
+echo ""
+echo "IMPORTANT: Please check NextcloudBackup.sh and NextcloudRestore.sh if all variables were set correctly BEFORE running these scripts!"
+echo ""
+echo ""
\ No newline at end of file

patrick-canterino.de