Local external storage can also be included in the backup; for restore, the main backup dir can be specified with parameter

This commit is contained in:
DecaTec
2019-01-03 18:55:38 +01:00
parent 0ee3ebac4c
commit 81d53268ae
3 changed files with 94 additions and 15 deletions

View File

@@ -2,7 +2,9 @@
# #
# Bash script for creating backups of Nextcloud. # 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/ # 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/
# #
@@ -18,29 +20,42 @@ backupMainDir=$1
if [ -z "$backupMainDir" ]; then if [ -z "$backupMainDir" ]; then
# TODO: The directory where you store the Nextcloud backups (when not specified by args) # TODO: The directory where you store the Nextcloud backups (when not specified by args)
backupMainDir="/mnt/Share/NextcloudBackups" backupMainDir="/media/hdd/nextcloud_backup"
fi fi
echo "Backup directory: $backupMainDir" echo "Backup directory: $backupMainDir"
currentDate=$(date +"%Y%m%d_%H%M%S") 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 # 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: The directory of your Nextcloud installation (this is a directory under your web root) # TODO: The directory of your Nextcloud installation (this is a directory under your web root)
nextcloudFileDir="/var/www/nextcloud" nextcloudFileDir="/var/www/nextcloud"
# TODO: The directory of your Nextcloud data directory (outside the Nextcloud file directory) # 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 # 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" 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>') # TODO: The service name of the web server. Used to start/stop web server (e.g. 'systemctl start <webserverServiceName>')
webserverServiceName="nginx" webserverServiceName="nginx"
# TODO: Your Nextcloud database name # TODO: Your Nextcloud database name
nextcloudDatabase="nextcloud_db" nextcloudDatabase="nextcloud_db"
# TODO: Your Nextcloud database user # TODO: Your Nextcloud database user
dbUser="nextcloud_db_user" dbUser="nextcloud_db_user"
# TODO: The password of the Nextcloud database user # TODO: The password of the Nextcloud database user
dbPassword="mYpAsSw0rd" dbPassword="mYpAsSw0rd"
# TODO: Your web server user # TODO: Your web server user
webserverUser="www-data" webserverUser="www-data"
# TODO: The maximum number of backups to keep (when set to 0, all backups are kept) # TODO: The maximum number of backups to keep (when set to 0, all backups are kept)
maxNrOfBackups=0 maxNrOfBackups=0
@@ -48,6 +63,10 @@ maxNrOfBackups=0
# If you prefer other file names, you'll also have to change the NextcloudRestore.sh script. # If you prefer other file names, you'll also have to change the NextcloudRestore.sh script.
fileNameBackupFileDir="nextcloud-filedir.tar.gz" fileNameBackupFileDir="nextcloud-filedir.tar.gz"
fileNameBackupDataDir="nextcloud-datadir.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" fileNameBackupDb="nextcloud-db.sql"
# Function for error messages # Function for error messages
@@ -114,18 +133,28 @@ echo "Done"
echo echo
# #
# Backup file and data directory # Backup file directory
# #
echo "Creating backup of Nextcloud file directory..." echo "Creating backup of Nextcloud file directory..."
tar -cpzf "${backupdir}/${fileNameBackupFileDir}" -C "${nextcloudFileDir}" . tar -cpzf "${backupdir}/${fileNameBackupFileDir}" -C "${nextcloudFileDir}" .
echo "Done" echo "Done"
echo echo
#
# Backup data directory
#
echo "Creating backup of Nextcloud data directory..." echo "Creating backup of Nextcloud data directory..."
tar -cpzf "${backupdir}/${fileNameBackupDataDir}" -C "${nextcloudDataDir}" . tar -cpzf "${backupdir}/${fileNameBackupDataDir}" -C "${nextcloudDataDir}" .
echo "Done" echo "Done"
echo 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 # Backup DB
# #
@@ -172,4 +201,4 @@ fi
echo echo
echo "DONE!" echo "DONE!"
echo "Backup created: ${backupdir}" echo "Backup created: ${backupdir}"

View File

@@ -2,7 +2,9 @@
# #
# Bash script for restoring backups of Nextcloud. # 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/ # 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/
# #
@@ -14,23 +16,41 @@
# #
# Variables # Variables
# TODO: The directory where you store the Nextcloud backups
mainBackupDir="/mnt/Share/NextcloudBackups"
restore=$1 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) # TODO: The directory of your Nextcloud installation (this is a directory under your web root)
nextcloudFileDir="/var/www/nextcloud" nextcloudFileDir="/var/www/nextcloud"
# TODO: The directory of your Nextcloud data directory (outside the Nextcloud file directory) # 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 # 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" 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>') # TODO: The service name of the web server. Used to start/stop web server (e.g. 'systemctl start <webserverServiceName>')
webserverServiceName="nginx" webserverServiceName="nginx"
# TODO: Your Nextcloud database name # TODO: Your Nextcloud database name
nextcloudDatabase="nextcloud_db" nextcloudDatabase="nextcloud_db"
# TODO: Your Nextcloud database user # TODO: Your Nextcloud database user
dbUser="nextcloud_db_user" dbUser="nextcloud_db_user"
# TODO: The password of the Nextcloud database user # TODO: The password of the Nextcloud database user
dbPassword="mYpAsSw0rd" dbPassword="mYpAsSw0rd"
# TODO: Your web server user # TODO: Your web server user
webserverUser="www-data" webserverUser="www-data"
@@ -38,6 +58,10 @@ webserverUser="www-data"
# If you prefer other file names, you'll also have to change the NextcloudBackup.sh script. # If you prefer other file names, you'll also have to change the NextcloudBackup.sh script.
fileNameBackupFileDir="nextcloud-filedir.tar.gz" fileNameBackupFileDir="nextcloud-filedir.tar.gz"
fileNameBackupDataDir="nextcloud-datadir.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" fileNameBackupDb="nextcloud-db.sql"
# Function for error messages # Function for error messages
@@ -92,31 +116,52 @@ echo
# #
# Delete old Nextcloud direcories # Delete old Nextcloud direcories
# #
# File directory
echo "Deleting old Nextcloud file directory..." echo "Deleting old Nextcloud file directory..."
rm -r "${nextcloudFileDir}" rm -r "${nextcloudFileDir}"
mkdir -p "${nextcloudFileDir}" mkdir -p "${nextcloudFileDir}"
echo "Done" echo "Done"
echo echo
# Data directory
echo "Deleting old Nextcloud data directory..." echo "Deleting old Nextcloud data directory..."
rm -r "${nextcloudDataDir}" rm -r "${nextcloudDataDir}"
mkdir -p "${nextcloudDataDir}" mkdir -p "${nextcloudDataDir}"
echo "Done" echo "Done"
echo 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 # Restore file and data directory
# #
# File directory
echo "Restoring Nextcloud file directory..." echo "Restoring Nextcloud file directory..."
tar -xmpzf "${currentRestoreDir}/${fileNameBackupFileDir}" -C "${nextcloudFileDir}" tar -xmpzf "${currentRestoreDir}/${fileNameBackupFileDir}" -C "${nextcloudFileDir}"
echo "Done" echo "Done"
echo echo
# Data directory
echo "Restoring Nextcloud data directory..." echo "Restoring Nextcloud data directory..."
tar -xmpzf "${currentRestoreDir}/${fileNameBackupDataDir}" -C "${nextcloudDataDir}" tar -xmpzf "${currentRestoreDir}/${fileNameBackupDataDir}" -C "${nextcloudDataDir}"
echo "Done" echo "Done"
echo 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 # Restore database
# #
@@ -164,6 +209,8 @@ echo
echo "Setting directory permissions..." echo "Setting directory permissions..."
chown -R "${webserverUser}":"${webserverUser}" "${nextcloudFileDir}" chown -R "${webserverUser}":"${webserverUser}" "${nextcloudFileDir}"
chown -R "${webserverUser}":"${webserverUser}" "${nextcloudDataDir}" chown -R "${webserverUser}":"${webserverUser}" "${nextcloudDataDir}"
# TOOD: Uncomment if you use local external storage
#chown -R "${webserverUser}":"${webserverUser}" "${nextcloudLocalExternalDataDir}"
echo "Done" echo "Done"
echo echo
@@ -185,4 +232,4 @@ echo
echo echo
echo "DONE!" echo "DONE!"
echo "Backup ${restore} successfully restored." echo "Backup ${restore} successfully restored."

View File

@@ -6,27 +6,30 @@ It is based on a Nextcloud installation using nginx and MariaDB (see the (German
## General information ## 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 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 - 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:** **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. - 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). - 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). - 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. - 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. - 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 ## Backup
In order to create a backup, simply call the script *NextcloudBackup.sh* on your Nextcloud machine. 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 ## 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*.