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 # File names for backup files
67 # If you prefer other file names, you'll also have to change the NextcloudBackup.sh script.
68 fileNameBackupFileDir
='nextcloud-filedir.tar'
69 fileNameBackupDataDir
='nextcloud-datadir.tar'
71 if [ "$useCompression" = true
] ; then
72 fileNameBackupFileDir
='nextcloud-filedir.tar.gz'
73 fileNameBackupDataDir
='nextcloud-datadir.tar.gz'
76 fileNameBackupExternalDataDir
=''
78 if [ ! -z "${nextcloudLocalExternalDataDir+x}" ] ; then
79 fileNameBackupExternalDataDir
='nextcloud-external-datadir.tar'
81 if [ "$useCompression" = true
] ; then
82 fileNameBackupExternalDataDir
='nextcloud-external-datadir.tar.gz'
86 fileNameBackupDb
='nextcloud-db.sql'
88 # Function for error messages
89 errorecho
() { cat <<< "$@" 1>&2; }
92 # Check if parameter(s) given
94 if [ $# != "1" ] && [ $# != "2" ]
96 errorecho
"ERROR: No backup name to restore given, or wrong number of parameters!"
97 errorecho
"Usage: NextcloudRestore.sh 'BackupDate' ['BackupDirectory']"
104 if [ "$(id -u)" != "0" ]
106 errorecho
"ERROR: This script has to be run as root!"
111 # Check if backup dir exists
113 if [ ! -d "${currentRestoreDir}" ]
115 errorecho
"ERROR: Backup ${restore} not found!"
120 # Check if the commands for restoring the database are available
122 if [ "${databaseSystem,,}" = "mysql" ] ||
[ "${databaseSystem,,}" = "mariadb" ]; then
123 if ! [ -x "$(command -v mysql)" ]; then
124 errorecho
"ERROR: MySQL/MariaDB not installed (command mysql not found)."
125 errorecho
"ERROR: No restore of database possible!"
126 errorecho
"Cancel restore"
129 elif [ "${databaseSystem,,}" = "postgresql" ] ||
[ "${databaseSystem,,}" = "pgsql" ]; then
130 if ! [ -x "$(command -v psql)" ]; then
131 errorecho
"ERROR: PostgreSQL not installed (command psql not found)."
132 errorecho
"ERROR: No restore of database possible!"
133 errorecho
"Cancel restore"
139 # Set maintenance mode
141 echo "$(date +"%H
:%M
:%S
"): Set maintenance mode for Nextcloud..."
142 sudo
-u "${webserverUser}" php
${nextcloudFileDir}/occ maintenance
:mode
--on
149 echo "$(date +"%H
:%M
:%S
"): Stopping web server..."
150 systemctl stop
"${webserverServiceName}"
155 # Delete old Nextcloud directories
159 echo "$(date +"%H
:%M
:%S
"): Deleting old Nextcloud file directory..."
160 rm -r "${nextcloudFileDir}"
161 mkdir
-p "${nextcloudFileDir}"
166 echo "$(date +"%H
:%M
:%S
"): Deleting old Nextcloud data directory..."
167 rm -r "${nextcloudDataDir}"
168 mkdir
-p "${nextcloudDataDir}"
172 # Local external storage
173 if [ ! -z "${nextcloudLocalExternalDataDir+x}" ] ; then
174 echo "Deleting old Nextcloud local external storage directory..."
175 rm -r "${nextcloudLocalExternalDataDir}"
176 mkdir
-p "${nextcloudLocalExternalDataDir}"
182 # Restore file and data directory
186 echo "$(date +"%H
:%M
:%S
"): Restoring Nextcloud file directory..."
188 if [ "$useCompression" = true
] ; then
189 tar -I pigz
-xmpf "${currentRestoreDir}/${fileNameBackupFileDir}" -C "${nextcloudFileDir}"
191 tar -xmpf "${currentRestoreDir}/${fileNameBackupFileDir}" -C "${nextcloudFileDir}"
198 echo "$(date +"%H
:%M
:%S
"): Restoring Nextcloud data directory..."
200 if [ "$useCompression" = true
] ; then
201 tar -I pigz
-xmpf "${currentRestoreDir}/${fileNameBackupDataDir}" -C "${nextcloudDataDir}"
203 tar -xmpf "${currentRestoreDir}/${fileNameBackupDataDir}" -C "${nextcloudDataDir}"
209 # Local external storage
210 if [ ! -z "${nextcloudLocalExternalDataDir+x}" ] ; then
211 echo "$(date +"%H
:%M
:%S
"): Restoring Nextcloud data directory..."
213 if [ "$useCompression" = true
] ; then
214 tar -I pigz
-xmpf "${currentRestoreDir}/${fileNameBackupExternalDataDir}" -C "${nextcloudLocalExternalDataDir}"
216 tar -xmpf "${currentRestoreDir}/${fileNameBackupExternalDataDir}" -C "${nextcloudLocalExternalDataDir}"
226 echo "$(date +"%H
:%M
:%S
"): Dropping old Nextcloud DB..."
228 if [ "${databaseSystem,,}" = "mysql" ] ||
[ "${databaseSystem,,}" = "mariadb" ]; then
229 mysql
-h localhost
-u "${dbUser}" -p"${dbPassword}" -e "DROP DATABASE ${nextcloudDatabase}"
230 elif [ "${databaseSystem,,}" = "postgresql" ]; then
231 sudo
-u postgres psql
-c "DROP DATABASE ${nextcloudDatabase};"
237 echo "$(date +"%H
:%M
:%S
"): Creating new DB for Nextcloud..."
239 if [ "${databaseSystem,,}" = "mysql" ] ||
[ "${databaseSystem,,}" = "mariadb" ]; then
240 # Use this if the databse from the backup uses UTF8 with multibyte support (e.g. for emoijs in filenames):
241 mysql
-h localhost
-u "${dbUser}" -p"${dbPassword}" -e "CREATE DATABASE ${nextcloudDatabase} CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci"
242 # TODO: Use this if the database from the backup DOES NOT use UTF8 with multibyte support (e.g. for emoijs in filenames):
243 #mysql -h localhost -u "${dbUser}" -p"${dbPassword}" -e "CREATE DATABASE ${nextcloudDatabase}"
244 elif [ "${databaseSystem,,}" = "postgresql" ] ||
[ "${databaseSystem,,}" = "pgsql" ]; then
245 sudo
-u postgres psql
-c "CREATE DATABASE ${nextcloudDatabase} WITH OWNER ${dbUser} TEMPLATE template0 ENCODING \"UNICODE\";"
251 echo "$(date +"%H
:%M
:%S
"): Restoring backup DB..."
253 if [ "${databaseSystem,,}" = "mysql" ] ||
[ "${databaseSystem,,}" = "mariadb" ]; then
254 mysql
-h localhost
-u "${dbUser}" -p"${dbPassword}" "${nextcloudDatabase}" < "${currentRestoreDir}/${fileNameBackupDb}"
255 elif [ "${databaseSystem,,}" = "postgresql" ] ||
[ "${databaseSystem,,}" = "pgsql" ]; then
256 sudo
-u postgres psql
"${nextcloudDatabase}" < "${currentRestoreDir}/${fileNameBackupDb}"
265 echo "$(date +"%H
:%M
:%S
"): Starting web server..."
266 systemctl start
"${webserverServiceName}"
271 # Set directory permissions
273 echo "$(date +"%H
:%M
:%S
"): Setting directory permissions..."
274 chown
-R "${webserverUser}":"${webserverUser}" "${nextcloudFileDir}"
275 chown
-R "${webserverUser}":"${webserverUser}" "${nextcloudDataDir}"
277 if [ ! -z "${nextcloudLocalExternalDataDir+x}" ] ; then
278 chown
-R "${webserverUser}":"${webserverUser}" "${nextcloudLocalExternalDataDir}"
285 # Update the system data-fingerprint (see https://docs.nextcloud.com/server/latest/admin_manual/configuration_server/occ_command.html#maintenance-commands-label)
287 echo "$(date +"%H
:%M
:%S
"): Updating the system data-fingerprint..."
288 sudo
-u "${webserverUser}" php
${nextcloudFileDir}/occ maintenance
:data-fingerprint
293 # Disbale maintenance mode
295 echo "$(date +"%H
:%M
:%S
"): Switching off maintenance mode..."
296 sudo
-u "${webserverUser}" php
${nextcloudFileDir}/occ maintenance
:mode
--off
302 echo "$(date +"%H
:%M
:%S
"): Backup ${restore} successfully restored."