4 # Bash script for restoring backups of Nextcloud.
9 # - With backup directory specified in the script: ./NextcloudRestore.sh <BackupName> (e.g. ./NextcloudRestore.sh 20170910_132703)
10 # - With backup directory specified by parameter: ./NextcloudRestore.sh <BackupName> <BackupDirectory> (e.g. ./NextcloudRestore.sh 20170910_132703 /media/hdd/nextcloud_backup)
12 # 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/
17 # You have to customize this script (directories, users, etc.) for your actual environment.
18 # All entries which need to be customized are tagged with "TODO".
25 if [ -z "$backupMainDir" ]; then
26 # TODO: The directory where you store the Nextcloud backups (when not specified by args)
27 backupMainDir
='/media/hdd/nextcloud_backup'
30 echo "Backup directory: $backupMainDir"
32 # TODO: Set this to true, if the backup was created with compression enabled, otherwiese false.
35 currentRestoreDir
="${backupMainDir}/${restore}"
37 # TODO: The directory of your Nextcloud installation (this is a directory under your web root)
38 nextcloudFileDir
='/var/www/nextcloud'
40 # TODO: The directory of your Nextcloud data directory (outside the Nextcloud file directory)
41 # If your data directory is located under Nextcloud's file directory (somewhere in the web root), the data directory should not be restored separately
42 nextcloudDataDir
='/var/nextcloud_data'
44 # TODO: The directory of your Nextcloud's local external storage.
45 # Uncomment if you use local external storage.
46 #nextcloudLocalExternalDataDir='/var/nextcloud_external_data'
48 # TODO: The service name of the web server. Used to start/stop web server (e.g. 'systemctl start <webserverServiceName>')
49 webserverServiceName
='nginx'
51 # TODO: Your web server user
52 webserverUser
='www-data'
54 # TODO: The name of the database system (one of: mysql, mariadb, postgresql)
55 databaseSystem
='mariadb'
57 # TODO: Your Nextcloud database name
58 nextcloudDatabase
='nextcloud_db'
60 # TODO: Your Nextcloud database user
61 dbUser
='nextcloud_db_user'
63 # TODO: The password of the Nextcloud database user
64 dbPassword
='mYpAsSw0rd'
66 # TODO: Uncomment this and set to true if the database from the backup DOES NOT use UTF8 with multibyte support (e.g. for emoijs in filenames)
69 # File names for backup files
70 # If you prefer other file names, you'll also have to change the NextcloudBackup.sh script.
71 fileNameBackupFileDir
='nextcloud-filedir.tar'
72 fileNameBackupDataDir
='nextcloud-datadir.tar'
74 if [ "$useCompression" = true
] ; then
75 fileNameBackupFileDir
='nextcloud-filedir.tar.gz'
76 fileNameBackupDataDir
='nextcloud-datadir.tar.gz'
79 fileNameBackupExternalDataDir
=''
81 if [ ! -z "${nextcloudLocalExternalDataDir+x}" ] ; then
82 fileNameBackupExternalDataDir
='nextcloud-external-datadir.tar'
84 if [ "$useCompression" = true
] ; then
85 fileNameBackupExternalDataDir
='nextcloud-external-datadir.tar.gz'
89 fileNameBackupDb
='nextcloud-db.sql'
91 # Function for error messages
92 errorecho
() { cat <<< "$@" 1>&2; }
95 # Check if parameter(s) given
97 if [ $# != "1" ] && [ $# != "2" ]
99 errorecho
"ERROR: No backup name to restore given, or wrong number of parameters!"
100 errorecho
"Usage: NextcloudRestore.sh 'BackupDate' ['BackupDirectory']"
107 if [ "$(id -u)" != "0" ]
109 errorecho
"ERROR: This script has to be run as root!"
114 # Check if backup dir exists
116 if [ ! -d "${currentRestoreDir}" ]
118 errorecho
"ERROR: Backup ${restore} not found!"
123 # Check if the commands for restoring the database are available
125 if [ "${databaseSystem,,}" = "mysql" ] ||
[ "${databaseSystem,,}" = "mariadb" ]; then
126 if ! [ -x "$(command -v mysql)" ]; then
127 errorecho
"ERROR: MySQL/MariaDB not installed (command mysql not found)."
128 errorecho
"ERROR: No restore of database possible!"
129 errorecho
"Cancel restore"
132 elif [ "${databaseSystem,,}" = "postgresql" ] ||
[ "${databaseSystem,,}" = "pgsql" ]; then
133 if ! [ -x "$(command -v psql)" ]; then
134 errorecho
"ERROR: PostgreSQL not installed (command psql not found)."
135 errorecho
"ERROR: No restore of database possible!"
136 errorecho
"Cancel restore"
142 # Set maintenance mode
144 echo "$(date +"%H
:%M
:%S
"): Set maintenance mode for Nextcloud..."
145 sudo
-u "${webserverUser}" php
${nextcloudFileDir}/occ maintenance
:mode
--on
152 echo "$(date +"%H
:%M
:%S
"): Stopping web server..."
153 systemctl stop
"${webserverServiceName}"
158 # Delete old Nextcloud directories
162 echo "$(date +"%H
:%M
:%S
"): Deleting old Nextcloud file directory..."
163 rm -r "${nextcloudFileDir}"
164 mkdir
-p "${nextcloudFileDir}"
169 echo "$(date +"%H
:%M
:%S
"): Deleting old Nextcloud data directory..."
170 rm -r "${nextcloudDataDir}"
171 mkdir
-p "${nextcloudDataDir}"
175 # Local external storage
176 if [ ! -z "${nextcloudLocalExternalDataDir+x}" ] ; then
177 echo "Deleting old Nextcloud local external storage directory..."
178 rm -r "${nextcloudLocalExternalDataDir}"
179 mkdir
-p "${nextcloudLocalExternalDataDir}"
185 # Restore file and data directory
189 echo "$(date +"%H
:%M
:%S
"): Restoring Nextcloud file directory..."
191 if [ "$useCompression" = true
] ; then
192 tar -I pigz
-xmpf "${currentRestoreDir}/${fileNameBackupFileDir}" -C "${nextcloudFileDir}"
194 tar -xmpf "${currentRestoreDir}/${fileNameBackupFileDir}" -C "${nextcloudFileDir}"
201 echo "$(date +"%H
:%M
:%S
"): Restoring Nextcloud data directory..."
203 if [ "$useCompression" = true
] ; then
204 tar -I pigz
-xmpf "${currentRestoreDir}/${fileNameBackupDataDir}" -C "${nextcloudDataDir}"
206 tar -xmpf "${currentRestoreDir}/${fileNameBackupDataDir}" -C "${nextcloudDataDir}"
212 # Local external storage
213 if [ ! -z "${nextcloudLocalExternalDataDir+x}" ] ; then
214 echo "$(date +"%H
:%M
:%S
"): Restoring Nextcloud data directory..."
216 if [ "$useCompression" = true
] ; then
217 tar -I pigz
-xmpf "${currentRestoreDir}/${fileNameBackupExternalDataDir}" -C "${nextcloudLocalExternalDataDir}"
219 tar -xmpf "${currentRestoreDir}/${fileNameBackupExternalDataDir}" -C "${nextcloudLocalExternalDataDir}"
229 echo "$(date +"%H
:%M
:%S
"): Dropping old Nextcloud DB..."
231 if [ "${databaseSystem,,}" = "mysql" ] ||
[ "${databaseSystem,,}" = "mariadb" ]; then
232 mysql
-h localhost
-u "${dbUser}" -p"${dbPassword}" -e "DROP DATABASE ${nextcloudDatabase}"
233 elif [ "${databaseSystem,,}" = "postgresql" ]; then
234 sudo
-u postgres psql
-c "DROP DATABASE ${nextcloudDatabase};"
240 echo "$(date +"%H
:%M
:%S
"): Creating new DB for Nextcloud..."
242 if [ "${databaseSystem,,}" = "mysql" ] ||
[ "${databaseSystem,,}" = "mariadb" ]; then
243 if [ ! -z "${dbNoMultibyte+x}" ] && [ "${dbNoMultibyte}" = true
] ; then
244 # Database from the backup DOES NOT use UTF8 with multibyte support (e.g. for emoijs in filenames)
245 mysql
-h localhost
-u "${dbUser}" -p"${dbPassword}" -e "CREATE DATABASE ${nextcloudDatabase}"
247 # Database from the backup uses UTF8 with multibyte support (e.g. for emoijs in filenames)
248 mysql
-h localhost
-u "${dbUser}" -p"${dbPassword}" -e "CREATE DATABASE ${nextcloudDatabase} CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci"
250 elif [ "${databaseSystem,,}" = "postgresql" ] ||
[ "${databaseSystem,,}" = "pgsql" ]; then
251 sudo
-u postgres psql
-c "CREATE DATABASE ${nextcloudDatabase} WITH OWNER ${dbUser} TEMPLATE template0 ENCODING \"UNICODE\";"
257 echo "$(date +"%H
:%M
:%S
"): Restoring backup DB..."
259 if [ "${databaseSystem,,}" = "mysql" ] ||
[ "${databaseSystem,,}" = "mariadb" ]; then
260 mysql
-h localhost
-u "${dbUser}" -p"${dbPassword}" "${nextcloudDatabase}" < "${currentRestoreDir}/${fileNameBackupDb}"
261 elif [ "${databaseSystem,,}" = "postgresql" ] ||
[ "${databaseSystem,,}" = "pgsql" ]; then
262 sudo
-u postgres psql
"${nextcloudDatabase}" < "${currentRestoreDir}/${fileNameBackupDb}"
271 echo "$(date +"%H
:%M
:%S
"): Starting web server..."
272 systemctl start
"${webserverServiceName}"
277 # Set directory permissions
279 echo "$(date +"%H
:%M
:%S
"): Setting directory permissions..."
280 chown
-R "${webserverUser}":"${webserverUser}" "${nextcloudFileDir}"
281 chown
-R "${webserverUser}":"${webserverUser}" "${nextcloudDataDir}"
283 if [ ! -z "${nextcloudLocalExternalDataDir+x}" ] ; then
284 chown
-R "${webserverUser}":"${webserverUser}" "${nextcloudLocalExternalDataDir}"
291 # Update the system data-fingerprint (see https://docs.nextcloud.com/server/latest/admin_manual/configuration_server/occ_command.html#maintenance-commands-label)
293 echo "$(date +"%H
:%M
:%S
"): Updating the system data-fingerprint..."
294 sudo
-u "${webserverUser}" php
${nextcloudFileDir}/occ maintenance
:data-fingerprint
299 # Disbale maintenance mode
301 echo "$(date +"%H
:%M
:%S
"): Switching off maintenance mode..."
302 sudo
-u "${webserverUser}" php
${nextcloudFileDir}/occ maintenance
:mode
--off
308 echo "$(date +"%H
:%M
:%S
"): Backup ${restore} successfully restored."