From: DecaTec Date: Tue, 2 Nov 2021 11:07:10 +0000 (+0100) Subject: v2.3.0: Bugfix double slashes; exit on error; optimizations X-Git-Tag: v2.3.0 X-Git-Url: https://git.p6c8.net/nextcloud-backup-restore.git/commitdiff_plain/e7157ba0a3afd98929faca45928e6ad9376ec41b v2.3.0: Bugfix double slashes; exit on error; optimizations --- diff --git a/CHANGELOG.md b/CHANGELOG.md index 6e4d5f8..57dfe21 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,18 @@ # Changelog +## 2.3.0 + +### General +- The scripts now exit when any command fails. +- Defined the command for compression in the "TODO section" of the script for easier customization. +- Added section for setup in readme. +- Updated links. +- Document requirement pigz when using compression. +- Formatting. + +### Backup +- Bugfix: Fixed the double trailing slash for paths containing the variable `backupdir`. + ## 2.2.0 ### General diff --git a/NextcloudBackup.sh b/NextcloudBackup.sh index 5a7e091..f719087 100644 --- a/NextcloudBackup.sh +++ b/NextcloudBackup.sh @@ -3,13 +3,20 @@ # # Bash script for creating backups of Nextcloud. # -# Version 2.2.0 +# Version 2.3.0 +# +# Requirements: +# - pigz (https://zlib.net/pigz/) for using backup compression. If not available, you can use another compression algorithm (e.g. gzip) +# +# Supported database systems: +# - MySQL/MariaDB +# - PostgreSQL # # Usage: # - With backup directory specified in the script: ./NextcloudBackup.sh # - With backup directory specified by parameter: ./NextcloudBackup.sh (e.g. ./NextcloudBackup.sh /media/hdd/nextcloud_backup) # -# 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/ +# The script is based on an installation of Nextcloud using nginx and MariaDB, see https://decatec.de/home-server/nextcloud-auf-ubuntu-server-20-04-lts-mit-nginx-mariadb-php-lets-encrypt-redis-und-fail2ban/ # # @@ -18,6 +25,9 @@ # All entries which need to be customized are tagged with "TODO". # +# Make sure the script exits when any command fails +set -Eeuo pipefail + # Variables backupMainDir=$1 @@ -31,7 +41,7 @@ fi currentDate=$(date +"%Y%m%d_%H%M%S") # The actual directory of the current backup - this is a subdirectory of the main directory above with a timestamp -backupdir="${backupMainDir}/${currentDate}/" +backupdir="${backupMainDir}/${currentDate}" # TODO: Use compression for file/data dir # When this is the only script for backups, it's recommend to enable compression. @@ -39,6 +49,10 @@ backupdir="${backupMainDir}/${currentDate}/" # you should probably disable compression here and only enable compression of your main backup script. useCompression=true +# TOOD: The bare tar command for using compression. +# Use 'tar -cpzf' if you want to use gzip compression. +compressionCommand="tar -I pigz -cpf" + # TODO: The directory of your Nextcloud installation (this is a directory under your web root) nextcloudFileDir='/var/www/nextcloud' @@ -176,7 +190,7 @@ echo echo "$(date +"%H:%M:%S"): Creating backup of Nextcloud file directory..." if [ "$useCompression" = true ] ; then - tar -I pigz -cpf "${backupdir}/${fileNameBackupFileDir}" -C "${nextcloudFileDir}" . + `$compressionCommand "${backupdir}/${fileNameBackupFileDir}" -C "${nextcloudFileDir}" .` else tar -cpf "${backupdir}/${fileNameBackupFileDir}" -C "${nextcloudFileDir}" . fi @@ -193,13 +207,13 @@ if [ "$ignoreUpdaterBackups" = true ] ; then echo "Ignoring updater backup directory" if [ "$useCompression" = true ] ; then - tar -I pigz -cpf "${backupdir}/${fileNameBackupDataDir}" --exclude="updater-*/backups/*" -C "${nextcloudDataDir}" . + `$compressionCommand "${backupdir}/${fileNameBackupDataDir}" --exclude="updater-*/backups/*" -C "${nextcloudDataDir}" .` else tar -cpf "${backupdir}/${fileNameBackupDataDir}" --exclude="updater-*/backups/*" -C "${nextcloudDataDir}" . fi else if [ "$useCompression" = true ] ; then - tar -I pigz -cpf "${backupdir}/${fileNameBackupDataDir}" -C "${nextcloudDataDir}" . + `$compressionCommand "${backupdir}/${fileNameBackupDataDir}" -C "${nextcloudDataDir}" .` else tar -cpf "${backupdir}/${fileNameBackupDataDir}" -C "${nextcloudDataDir}" . fi @@ -215,7 +229,7 @@ if [ ! -z "${nextcloudLocalExternalDataDir+x}" ] ; then echo "$(date +"%H:%M:%S"): Creating backup of Nextcloud local external storage directory..." if [ "$useCompression" = true ] ; then - tar -I pigz -cpf "${backupdir}/${fileNameBackupExternalDataDir}" -C "${nextcloudLocalExternalDataDir}" . + `$compressionCommand "${backupdir}/${fileNameBackupExternalDataDir}" -C "${nextcloudLocalExternalDataDir}" .` else tar -cpf "${backupdir}/${fileNameBackupExternalDataDir}" -C "${nextcloudLocalExternalDataDir}" . fi diff --git a/NextcloudRestore.sh b/NextcloudRestore.sh index 8fb38c2..a18ecd0 100644 --- a/NextcloudRestore.sh +++ b/NextcloudRestore.sh @@ -3,13 +3,20 @@ # # Bash script for restoring backups of Nextcloud. # -# Version 2.2.0 +# Version 2.3.0 +# +# Requirements: +# - pigz (https://zlib.net/pigz/) for using backup compression. If not available, you can use another compression algorithm (e.g. gzip) +# +# Supported database systems: +# - MySQL/MariaDB +# - PostgreSQL # # Usage: # - With backup directory specified in the script: ./NextcloudRestore.sh (e.g. ./NextcloudRestore.sh 20170910_132703) # - With backup directory specified by parameter: ./NextcloudRestore.sh (e.g. ./NextcloudRestore.sh 20170910_132703 /media/hdd/nextcloud_backup) # -# 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/ +# The script is based on an installation of Nextcloud using nginx and MariaDB, see https://decatec.de/home-server/nextcloud-auf-ubuntu-server-20-04-lts-mit-nginx-mariadb-php-lets-encrypt-redis-und-fail2ban/ # # @@ -18,6 +25,9 @@ # All entries which need to be customized are tagged with "TODO". # +# Make sure the script exits when any command fails +set -Eeuo pipefail + # Variables restore=$1 backupMainDir=$2 @@ -29,10 +39,14 @@ fi echo "Backup directory: $backupMainDir" +currentRestoreDir="${backupMainDir}/${restore}" + # TODO: Set this to true, if the backup was created with compression enabled, otherwiese false. useCompression=true -currentRestoreDir="${backupMainDir}/${restore}" +# TOOD: The bare tar command for using compression. +# Use 'tar -xmpzf' if you want to use gzip compression. +compressionCommand="tar -I pigz -cpf" # TODO: The directory of your Nextcloud installation (this is a directory under your web root) nextcloudFileDir='/var/www/nextcloud' @@ -189,7 +203,7 @@ fi echo "$(date +"%H:%M:%S"): Restoring Nextcloud file directory..." if [ "$useCompression" = true ] ; then - tar -I pigz -xmpf "${currentRestoreDir}/${fileNameBackupFileDir}" -C "${nextcloudFileDir}" + `$compressionCommand "${currentRestoreDir}/${fileNameBackupFileDir}" -C "${nextcloudFileDir}"` else tar -xmpf "${currentRestoreDir}/${fileNameBackupFileDir}" -C "${nextcloudFileDir}" fi @@ -201,7 +215,7 @@ echo echo "$(date +"%H:%M:%S"): Restoring Nextcloud data directory..." if [ "$useCompression" = true ] ; then - tar -I pigz -xmpf "${currentRestoreDir}/${fileNameBackupDataDir}" -C "${nextcloudDataDir}" + `$compressionCommand "${currentRestoreDir}/${fileNameBackupDataDir}" -C "${nextcloudDataDir}"` else tar -xmpf "${currentRestoreDir}/${fileNameBackupDataDir}" -C "${nextcloudDataDir}" fi @@ -214,7 +228,7 @@ if [ ! -z "${nextcloudLocalExternalDataDir+x}" ] ; then echo "$(date +"%H:%M:%S"): Restoring Nextcloud local external storage directory..." if [ "$useCompression" = true ] ; then - tar -I pigz -xmpf "${currentRestoreDir}/${fileNameBackupExternalDataDir}" -C "${nextcloudLocalExternalDataDir}" + `$compressionCommand "${currentRestoreDir}/${fileNameBackupExternalDataDir}" -C "${nextcloudLocalExternalDataDir}"` else tar -xmpf "${currentRestoreDir}/${fileNameBackupExternalDataDir}" -C "${nextcloudLocalExternalDataDir}" fi diff --git a/README.md b/README.md index 65b5b62..4e2002d 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,10 @@ For a complete backup of any Nextcloud instance, you'll have to backup these ite The scripts take care of these items to backup automatically. +## Requirements + +- *pigz* (https://zlib.net/pigz/) when using backup compression. If not available, you can use another compression algorithm (e.g. gzip) + **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. @@ -23,6 +27,21 @@ The scripts take care of these items to backup automatically. - The scripts support MariaDB/MySQL and PostgreSQL as database. - 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 (variable `dbNoMultibyte`). +## Setup + +1. Clone the repository: `git clone https://codeberg.org/DecaTec/Nextcloud-Backup-Restore.git` +2. Set permissions: + - `chown -R root Nextcloud-Backup-Restore` + - `cd Nextcloud-Backup-Restore` + - `chmod 700 *.sh` +3. Call the (interactive) script for automated setup (this will modify the scripts for backup/restore to fit your Nextcloud instance, see below): `./setup.sh` +4. **Important**: Check the scripts `NextcloudBackup.sh` and `NextcloudRestore.sh` if everything was set up correctly (see *TODO* in the script's comments) +5. Start using the scripts: See sections *Backup* and *Restore* below + +### 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. + ## Backup In order to create a backup, simply call the script *NextcloudBackup.sh* on your Nextcloud machine. @@ -36,8 +55,4 @@ You can also call this script by cron. Example (at 2am every night, with log out ## 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*. - -## 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 +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 diff --git a/setup.sh b/setup.sh index ed31371..ed29c3e 100755 --- a/setup.sh +++ b/setup.sh @@ -3,7 +3,7 @@ # # Bash script an easy setup of NextcloudBackup.sh and NextcloudRestore.sh # -# Version 2.2.0 +# Version 2.3.0 # # Usage: # - call the setup.sh script @@ -19,6 +19,9 @@ # However, you should always check the backup/restore scripts BEFORE executing these! # +# Make sure the script exits when any command fails +set -Eeuo pipefail + # # Pre defined variables # @@ -86,7 +89,7 @@ occ_get datadirectory if [ $? -ne 0 ]; then echo "Error calling OCC: Please check if the information provided was correct." - echo "ABORTING!" + echo "ABORTING!" echo "No file has been altered." exit 1 fi