]>
git.p6c8.net - nextcloud-backup-restore.git/blob - NextcloudRestore.sh
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 # TODO: Uncomment if you use local external storage
77 #fileNameBackupExternalDataDir='nextcloud-external-datadir.tar'
79 #if [ "$useCompression" = true ] ; then
80 # fileNameBackupExternalDataDir='nextcloud-external-datadir.tar.gz'
83 fileNameBackupDb
= 'nextcloud-db.sql'
85 # Function for error messages
86 errorecho
() { cat <<< "$@" 1 >& 2 ; }
89 # Check if parameter(s) given
91 if [ $# != "1" ] && [ $# != "2" ]
93 errorecho
"ERROR: No backup name to restore given, or wrong number of parameters!"
94 errorecho
"Usage: NextcloudRestore.sh 'BackupDate' ['BackupDirectory']"
101 if [ "$(id -u)" != "0" ]
103 errorecho
"ERROR: This script has to be run as root!"
108 # Check if backup dir exists
110 if [ ! -d " ${currentRestoreDir} " ]
112 errorecho
"ERROR: Backup ${restore} not found!"
117 # Check if the commands for restoring the database are available
119 if [ " ${databaseSystem,,} " = "mysql" ] ||
[ " ${databaseSystem,,} " = "mariadb" ]; then
120 if ! [ -x "$(command -v mysql)" ]; then
121 errorecho
"ERROR: MySQL/MariaDB not installed (command mysql not found)."
122 errorecho
"ERROR: No restore of database possible!"
123 errorecho
"Cancel restore"
126 elif [ " ${databaseSystem,,} " = "postgresql" ] ||
[ " ${databaseSystem,,} " = "pgsql" ]; then
127 if ! [ -x "$(command -v psql)" ]; then
128 errorecho
"ERROR: PostgreSQL not installed (command psql not found)."
129 errorecho
"ERROR: No restore of database possible!"
130 errorecho
"Cancel restore"
136 # Set maintenance mode
138 echo "Set maintenance mode for Nextcloud..."
139 sudo
-u " ${webserverUser} " php
${nextcloudFileDir} / occ maintenance
: mode
--on
146 echo "Stopping web server..."
147 systemctl stop
" ${webserverServiceName} "
152 # Delete old Nextcloud directories
156 echo "Deleting old Nextcloud file directory..."
157 rm -r " ${nextcloudFileDir} "
158 mkdir
-p " ${nextcloudFileDir} "
163 echo "Deleting old Nextcloud data directory..."
164 rm -r " ${nextcloudDataDir} "
165 mkdir
-p " ${nextcloudDataDir} "
169 # Local external storage
170 # TODO: Uncomment if you use local external storage
171 #echo "Deleting old Nextcloud local external storage directory..."
172 #rm -r "${nextcloudLocalExternalDataDir}"
173 #mkdir -p "${nextcloudLocalExternalDataDir}"
178 # Restore file and data directory
182 echo "Restoring Nextcloud file directory..."
184 if [ " $useCompression " = true
] ; then
185 tar -I pigz
-xmpf " ${currentRestoreDir}/${fileNameBackupFileDir}" -C "${nextcloudFileDir} "
187 tar -xmpf " ${currentRestoreDir}/${fileNameBackupFileDir}" -C "${nextcloudFileDir} "
194 echo "Restoring Nextcloud data directory..."
196 if [ " $useCompression " = true
] ; then
197 tar -I pigz
-xmpf " ${currentRestoreDir}/${fileNameBackupDataDir}" -C "${nextcloudDataDir} "
199 tar -xmpf " ${currentRestoreDir}/${fileNameBackupDataDir}" -C "${nextcloudDataDir} "
205 # Local external storage
206 # TODO: Uncomment if you use local external storage
207 #echo "Restoring Nextcloud data directory..."
209 #if [ "$useCompression" = true ] ; then
210 # tar -I pigz -xmpf "${currentRestoreDir}/${fileNameBackupExternalDataDir}" -C "${nextcloudLocalExternalDataDir}"
212 # tar -xmpf "${currentRestoreDir}/${fileNameBackupExternalDataDir}" -C "${nextcloudLocalExternalDataDir}"
221 echo "Dropping old Nextcloud DB..."
223 if [ " ${databaseSystem,,} " = "mysql" ] ||
[ " ${databaseSystem,,} " = "mariadb" ]; then
224 mysql
-h localhost
-u " ${dbUser}" -p"${dbPassword}" -e "DROP DATABASE ${nextcloudDatabase} "
225 elif [ " ${databaseSystem,,} " = "postgresql" ]; then
226 sudo
-u postgres psql
-c "DROP DATABASE ${nextcloudDatabase} ;"
232 echo "Creating new DB for Nextcloud..."
234 if [ " ${databaseSystem,,} " = "mysql" ] ||
[ " ${databaseSystem,,} " = "mariadb" ]; then
235 # Use this if the databse from the backup uses UTF8 with multibyte support (e.g. for emoijs in filenames):
236 mysql
-h localhost
-u " ${dbUser}" -p"${dbPassword}" -e "CREATE DATABASE ${nextcloudDatabase} CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci"
237 # TODO: Use this if the database from the backup DOES NOT use UTF8 with multibyte support (e.g. for emoijs in filenames):
238 #mysql -h localhost -u "${dbUser}" -p"${dbPassword}" -e "CREATE DATABASE ${nextcloudDatabase}"
239 elif [ " ${databaseSystem,,} " = "postgresql" ] ||
[ " ${databaseSystem,,} " = "pgsql" ]; then
240 sudo
-u postgres psql
-c "CREATE DATABASE ${nextcloudDatabase} WITH OWNER ${dbUser} TEMPLATE template0 ENCODING \" UNICODE \" ;"
246 echo "Restoring backup DB..."
248 if [ " ${databaseSystem,,} " = "mysql" ] ||
[ " ${databaseSystem,,} " = "mariadb" ]; then
249 mysql
-h localhost
-u " ${dbUser}" -p"${dbPassword}" "${nextcloudDatabase}" < "${currentRestoreDir}/${fileNameBackupDb} "
250 elif [ " ${databaseSystem,,} " = "postgresql" ] ||
[ " ${databaseSystem,,} " = "pgsql" ]; then
251 sudo
-u postgres psql
" ${nextcloudDatabase}" < "${currentRestoreDir}/${fileNameBackupDb} "
260 echo "Starting web server..."
261 systemctl start
" ${webserverServiceName} "
266 # Set directory permissions
268 echo "Setting directory permissions..."
269 chown
-R " ${webserverUser}":"${webserverUser}" "${nextcloudFileDir} "
270 chown
-R " ${webserverUser}":"${webserverUser}" "${nextcloudDataDir} "
271 # TODO: Uncomment if you use local external storage
272 #chown -R "${webserverUser}":"${webserverUser}" "${nextcloudLocalExternalDataDir}"
277 # Update the system data-fingerprint (see https://docs.nextcloud.com/server/latest/admin_manual/configuration_server/occ_command.html#maintenance-commands-label)
279 echo "Updating the system data-fingerprint..."
280 sudo
-u " ${webserverUser} " php
${nextcloudFileDir} / occ maintenance
: data-fingerprint
285 # Disbale maintenance mode
287 echo "Switching off maintenance mode..."
288 sudo
-u " ${webserverUser} " php
${nextcloudFileDir} / occ maintenance
: mode
--off
294 echo "Backup ${restore} successfully restored."
patrick-canterino.de