]>
git.p6c8.net - nextcloud-backup-restore.git/blob - NextcloudRestore.sh
6a4fcaeb9d93b04daddc4d16d2ffc4814cb88c55
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 currentRestoreDir
= " ${backupMainDir} / ${restore} "
34 # TODO: The directory of your Nextcloud installation (this is a directory under your web root)
35 nextcloudFileDir
= '/var/www/nextcloud'
37 # TODO: The directory of your Nextcloud data directory (outside the Nextcloud file directory)
38 # If your data directory is located under Nextcloud's file directory (somewhere in the web root), the data directory should not be restored separately
39 nextcloudDataDir
= '/var/nextcloud_data'
41 # TODO: The directory of your Nextcloud's local external storage.
42 # Uncomment if you use local external storage.
43 #nextcloudLocalExternalDataDir='/var/nextcloud_external_data'
45 # TODO: The service name of the web server. Used to start/stop web server (e.g. 'systemctl start <webserverServiceName>')
46 webserverServiceName
= 'nginx'
48 # TODO: Your web server user
49 webserverUser
= 'www-data'
51 # TODO: The name of the database system (ome of: mysql, mariadb, postgresql)
52 databaseSystem
= 'mariadb'
54 # TODO: Your Nextcloud database name
55 nextcloudDatabase
= 'nextcloud_db'
57 # TODO: Your Nextcloud database user
58 dbUser
= 'nextcloud_db_user'
60 # TODO: The password of the Nextcloud database user
61 dbPassword
= 'mYpAsSw0rd'
63 # File names for backup files
64 # If you prefer other file names, you'll also have to change the NextcloudBackup.sh script.
65 fileNameBackupFileDir
= 'nextcloud-filedir.tar.gz'
66 fileNameBackupDataDir
= 'nextcloud-datadir.tar.gz'
68 # TODO: Uncomment if you use local external storage
69 #fileNameBackupExternalDataDir='nextcloud-external-datadir.tar.gz'
71 fileNameBackupDb
= 'nextcloud-db.sql'
73 # Function for error messages
74 errorecho
() { cat <<< "$@" 1 >& 2 ; }
77 # Check if parameter(s) given
79 if [ $# != "1" ] && [ $# != "2" ]
81 errorecho
"ERROR: No backup name to restore given, or wrong number of parameters!"
82 errorecho
"Usage: NextcloudRestore.sh 'BackupDate' ['BackupDirectory']"
89 if [ "$(id -u)" != "0" ]
91 errorecho
"ERROR: This script has to be run as root!"
96 # Check if backup dir exists
98 if [ ! -d " ${currentRestoreDir} " ]
100 errorecho
"ERROR: Backup ${restore} not found!"
105 # Check if the commands for restoring the database are available
107 if [ " ${databaseSystem,,} " = "mysql" ] ||
[ " ${databaseSystem,,} " = "mariadb" ]; then
108 if ! [ -x "$(command -v mysql)" ]; then
109 errorecho
"ERROR: MySQL/MariaDB not installed (command mysql not found)."
110 errorecho
"ERROR: No restore of database possible!"
111 errorecho
"Cancel restore"
114 elif [ " ${databaseSystem,,} " = "postgresql" ]; then
115 if ! [ -x "$(command -v psql)" ]; then
116 errorecho
"ERROR: PostgreSQL not installed (command psql not found)."
117 errorecho
"ERROR: No restore of database possible!"
118 errorecho
"Cancel restore"
124 # Set maintenance mode
126 echo "Set maintenance mode for Nextcloud..."
127 sudo
-u " ${webserverUser} " php
${nextcloudFileDir} / occ maintenance
: mode
--on
134 echo "Stopping web server..."
135 systemctl stop
" ${webserverServiceName} "
140 # Delete old Nextcloud directories
144 echo "Deleting old Nextcloud file directory..."
145 rm -r " ${nextcloudFileDir} "
146 mkdir
-p " ${nextcloudFileDir} "
151 echo "Deleting old Nextcloud data directory..."
152 rm -r " ${nextcloudDataDir} "
153 mkdir
-p " ${nextcloudDataDir} "
157 # Local external storage
158 # TODO: Uncomment if you use local external storage
159 #echo "Deleting old Nextcloud local external storage directory..."
160 #rm -r "${nextcloudLocalExternalDataDir}"
161 #mkdir -p "${nextcloudLocalExternalDataDir}"
166 # Restore file and data directory
170 echo "Restoring Nextcloud file directory..."
171 tar -xmpzf " ${currentRestoreDir}/${fileNameBackupFileDir}" -C "${nextcloudFileDir} "
176 echo "Restoring Nextcloud data directory..."
177 tar -xmpzf " ${currentRestoreDir}/${fileNameBackupDataDir}" -C "${nextcloudDataDir} "
181 # Local external storage
182 # TODO: Uncomment if you use local external storage
183 #echo "Restoring Nextcloud data directory..."
184 #tar -xmpzf "${currentRestoreDir}/${fileNameBackupExternalDataDir}" -C "${nextcloudLocalExternalDataDir}"
191 echo "Dropping old Nextcloud DB..."
193 if [ " ${databaseSystem,,} " = "mysql" ] ||
[ " ${databaseSystem,,} " = "mariadb" ]; then
194 mysql
-h localhost
-u " ${dbUser}" -p"${dbPassword}" -e "DROP DATABASE ${nextcloudDatabase} "
195 elif [ " ${databaseSystem,,} " = "postgresql" ]; then
196 sudo
-u postgres psql
-c "DROP DATABASE ${nextcloudDatabase} ;"
202 echo "Creating new DB for Nextcloud..."
204 if [ " ${databaseSystem,,} " = "mysql" ] ||
[ " ${databaseSystem,,} " = "mariadb" ]; then
205 # Use this if the databse from the backup uses UTF8 with multibyte support (e.g. for emoijs in filenames):
206 mysql
-h localhost
-u " ${dbUser}" -p"${dbPassword}" -e "CREATE DATABASE ${nextcloudDatabase} CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci"
207 # TODO: Use this if the database from the backup DOES NOT use UTF8 with multibyte support (e.g. for emoijs in filenames):
208 #mysql -h localhost -u "${dbUser}" -p"${dbPassword}" -e "CREATE DATABASE ${nextcloudDatabase}"
209 elif [ " ${databaseSystem,,} " = "postgresql" ]; then
210 sudo
-u postgres psql
-c "CREATE DATABASE ${nextcloudDatabase} WITH OWNER ${dbUser} TEMPLATE template0 ENCODING \" UTF8 \" ;"
216 echo "Restoring backup DB..."
218 if [ " ${databaseSystem,,} " = "mysql" ] ||
[ " ${databaseSystem,,} " = "mariadb" ]; then
219 mysql
-h localhost
-u " ${dbUser}" -p"${dbPassword}" "${nextcloudDatabase}" < "${currentRestoreDir}/${fileNameBackupDb} "
220 elif [ " ${databaseSystem,,} " = "postgresql" ]; then
221 sudo
-u postgres psql
" ${nextcloudDatabase}" < "${currentRestoreDir}/${fileNameBackupDb} "
230 echo "Starting web server..."
231 systemctl start
" ${webserverServiceName} "
236 # Set directory permissions
238 echo "Setting directory permissions..."
239 chown
-R " ${webserverUser}":"${webserverUser}" "${nextcloudFileDir} "
240 chown
-R " ${webserverUser}":"${webserverUser}" "${nextcloudDataDir} "
241 # TODO: Uncomment if you use local external storage
242 #chown -R "${webserverUser}":"${webserverUser}" "${nextcloudLocalExternalDataDir}"
247 # Update the system data-fingerprint (see https://docs.nextcloud.com/server/16/admin_manual/configuration_server/occ_command.html#maintenance-commands-label)
249 echo "Updating the system data-fingerprint..."
250 sudo
-u " ${webserverUser} " php
${nextcloudFileDir} / occ maintenance
: data-fingerprint
255 # Disbale maintenance mode
257 echo "Switching off maintenance mode..."
258 sudo
-u " ${webserverUser} " php
${nextcloudFileDir} / occ maintenance
: mode
--off
264 echo "Backup ${restore} successfully restored."
patrick-canterino.de