# 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
#
# 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 <BackupDirectory> (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/
#
#
# 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
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.
# 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'
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
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
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
#
# 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 <BackupName> (e.g. ./NextcloudRestore.sh 20170910_132703)
# - With backup directory specified by parameter: ./NextcloudRestore.sh <BackupName> <BackupDirectory> (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/
#
#
# 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
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'
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
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
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
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.
- 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.
## 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
#
# 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
# 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
#
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