#
# Bash script for creating backups of Nextcloud.
-# Usage: ./NextcloudBackup.sh
+# 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/
#
if [ -z "$backupMainDir" ]; then
# TODO: The directory where you store the Nextcloud backups (when not specified by args)
- backupMainDir="/mnt/Share/NextcloudBackups"
+ backupMainDir="/media/hdd/nextcloud_backup"
fi
echo "Backup directory: $backupMainDir"
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}/"
+
# TODO: The directory of your Nextcloud installation (this is a directory under your web root)
nextcloudFileDir="/var/www/nextcloud"
+
# TODO: The directory of your Nextcloud data directory (outside the Nextcloud file directory)
# If your data directory is located under Nextcloud's file directory (somewhere in the web root), the data directory should not be a separate part of the backup
nextcloudDataDir="/var/nextcloud_data"
+
+# TODO: The directory of your Nextcloud's local external storage.
+# Uncomment if you use local external storage.
+#nextcloudLocalExternalDataDir="/var/nextcloud_external_data"
+
# TODO: The service name of the web server. Used to start/stop web server (e.g. 'systemctl start <webserverServiceName>')
webserverServiceName="nginx"
+
# TODO: Your Nextcloud database name
nextcloudDatabase="nextcloud_db"
+
# TODO: Your Nextcloud database user
dbUser="nextcloud_db_user"
+
# TODO: The password of the Nextcloud database user
dbPassword="mYpAsSw0rd"
+
# TODO: Your web server user
webserverUser="www-data"
+
# TODO: The maximum number of backups to keep (when set to 0, all backups are kept)
maxNrOfBackups=0
# If you prefer other file names, you'll also have to change the NextcloudRestore.sh script.
fileNameBackupFileDir="nextcloud-filedir.tar.gz"
fileNameBackupDataDir="nextcloud-datadir.tar.gz"
+
+# TOOD: Uncomment if you use local external storage
+#fileNameBackupExternalDataDir="nextcloud-external-datadir.tar.gz"
+
fileNameBackupDb="nextcloud-db.sql"
# Function for error messages
echo
#
-# Backup file and data directory
+# Backup file directory
#
echo "Creating backup of Nextcloud file directory..."
tar -cpzf "${backupdir}/${fileNameBackupFileDir}" -C "${nextcloudFileDir}" .
echo "Done"
echo
+#
+# Backup data directory
+#
echo "Creating backup of Nextcloud data directory..."
tar -cpzf "${backupdir}/${fileNameBackupDataDir}" -C "${nextcloudDataDir}" .
echo "Done"
echo
+# Backup local external storage.
+# Uncomment if you use local external storage
+#echo "Creating backup of Nextcloud local external storage directory..."
+#tar -cpzf "${backupdir}/${fileNameBackupExternalDataDir}" -C "${nextcloudLocalExternalDataDir}" .
+#echo "Done"
+#echo
+
#
# Backup DB
#
echo
echo "DONE!"
-echo "Backup created: ${backupdir}"
+echo "Backup created: ${backupdir}"
\ No newline at end of file
#
# Bash script for restoring backups of Nextcloud.
-# Usage: ./NextcloudRestore.sh <BackupName> (e.g. ./NextcloudRestore.sh 20170910_132703)
+# 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/
#
#
# Variables
-# TODO: The directory where you store the Nextcloud backups
-mainBackupDir="/mnt/Share/NextcloudBackups"
restore=$1
-currentRestoreDir="${mainBackupDir}/${restore}"
+backupMainDir=$2
+
+if [ -z "$backupMainDir" ]; then
+ # TODO: The directory where you store the Nextcloud backups (when not specified by args)
+ backupMainDir="/mnt/hdd1/nextcloudb_ackups"
+fi
+
+echo "Backup directory: $backupMainDir"
+
+currentRestoreDir="${backupMainDir}/${restore}"
+
# TODO: The directory of your Nextcloud installation (this is a directory under your web root)
nextcloudFileDir="/var/www/nextcloud"
+
# TODO: The directory of your Nextcloud data directory (outside the Nextcloud file directory)
# If your data directory is located under Nextcloud's file directory (somewhere in the web root), the data directory should not be restored separately
nextcloudDataDir="/var/nextcloud_data"
+
+# TODO: The directory of your Nextcloud's local external storage.
+# Uncomment if you use local external storage.
+#nextcloudLocalExternalDataDir="/var/nextcloud_external_data"
+
# TODO: The service name of the web server. Used to start/stop web server (e.g. 'systemctl start <webserverServiceName>')
webserverServiceName="nginx"
+
# TODO: Your Nextcloud database name
nextcloudDatabase="nextcloud_db"
+
# TODO: Your Nextcloud database user
dbUser="nextcloud_db_user"
+
# TODO: The password of the Nextcloud database user
dbPassword="mYpAsSw0rd"
+
# TODO: Your web server user
webserverUser="www-data"
# If you prefer other file names, you'll also have to change the NextcloudBackup.sh script.
fileNameBackupFileDir="nextcloud-filedir.tar.gz"
fileNameBackupDataDir="nextcloud-datadir.tar.gz"
+
+# TOOD: Uncomment if you use local external storage
+#fileNameBackupExternalDataDir="nextcloud-external-datadir.tar.gz"
+
fileNameBackupDb="nextcloud-db.sql"
# Function for error messages
#
# Delete old Nextcloud direcories
#
+
+# File directory
echo "Deleting old Nextcloud file directory..."
rm -r "${nextcloudFileDir}"
mkdir -p "${nextcloudFileDir}"
echo "Done"
echo
+# Data directory
echo "Deleting old Nextcloud data directory..."
rm -r "${nextcloudDataDir}"
mkdir -p "${nextcloudDataDir}"
echo "Done"
echo
+# Local external storage
+# TOOD: Uncomment if you use local external storage
+#echo "Deleting old Nextcloud local external storage directory..."
+#rm -r "${nextcloudLocalExternalDataDir}"
+#mkdir -p "${nextcloudLocalExternalDataDir}"
+#echo "Done"
+#echo
+
#
# Restore file and data directory
#
+
+# File directory
echo "Restoring Nextcloud file directory..."
tar -xmpzf "${currentRestoreDir}/${fileNameBackupFileDir}" -C "${nextcloudFileDir}"
echo "Done"
echo
+# Data directory
echo "Restoring Nextcloud data directory..."
tar -xmpzf "${currentRestoreDir}/${fileNameBackupDataDir}" -C "${nextcloudDataDir}"
echo "Done"
echo
+# Local external storage
+# TOOD: Uncomment if you use local external storage
+#echo "Restoring Nextcloud data directory..."
+#tar -xmpzf "${currentRestoreDir}/${fileNameBackupExternalDataDir}" -C "${nextcloudLocalExternalDataDir}"
+#echo "Done"
+#echo
+
#
# Restore database
#
echo "Setting directory permissions..."
chown -R "${webserverUser}":"${webserverUser}" "${nextcloudFileDir}"
chown -R "${webserverUser}":"${webserverUser}" "${nextcloudDataDir}"
+# TOOD: Uncomment if you use local external storage
+#chown -R "${webserverUser}":"${webserverUser}" "${nextcloudLocalExternalDataDir}"
echo "Done"
echo
echo
echo "DONE!"
-echo "Backup ${restore} successfully restored."
+echo "Backup ${restore} successfully restored."
\ No newline at end of file
## General information
-For a complete backup of any Nextcloud instance, you'll have to backup three items:
+For a complete backup of any Nextcloud instance, you'll have to backup these items:
- The Nextcloud file directory (usually */var/www/nextcloud*)
-- The data directory of Nextcloud (it's recommended to locate this not under the web root, so e.g. */var/nextcloud_data*)
+- The data directory of Nextcloud (it's recommended that this is *not* located in the web root, so e.g. */var/nextcloud_data*)
- The Nextcloud database
+- Maybe a local external storage mounted into Nextcloud
-The scripts take care of these three items to backup automatically.
+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/15/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. If you have any external storage mounted in Nextcloud, these directories have to be handled separately.
+- 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/15/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
In order to create a backup, simply call the script *NextcloudBackup.sh* on your Nextcloud machine.
-This will create a directory with the current time stamp in your main backup directory (you already edited the script so that it fits your Nextcloud installation, haven't you): As an example, this would be */mnt/Share/NextcloudBackups/20170910_132703*.
+If this script is called without parameter, the backup is saved in a directory with the current time stamp in your main backup directory: As an example, this would be */media/hdd/nextcloud_backup/20170910_132703*.
+The backup script can also be called with a parameter specifiying the main backup directory, e.g. *./NextcloudBackup.sh /media/hdd/nextcloud_backup*. In this case, the directory specified will be used as main backup directory.
## Restore
-For restore, just call *NextcloudRestore.sh*. This script expects one parameter which is 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*.
+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