]> git.p6c8.net - nextcloud-backup-restore.git/blob - NextcloudRestore.sh
Changelog
[nextcloud-backup-restore.git] / NextcloudRestore.sh
1 #!/bin/bash
2
3 #
4 # Bash script for restoring backups of Nextcloud.
5 #
6 # Version 1.1.1
7 #
8 # Usage:
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)
11 #
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/
13 #
14
15 #
16 # IMPORTANT
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".
19 #
20
21 # Variables
22 restore=$1
23 backupMainDir=$2
24
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'
28 fi
29
30 echo "Backup directory: $backupMainDir"
31
32 currentRestoreDir="${backupMainDir}/${restore}"
33
34 # TODO: The directory of your Nextcloud installation (this is a directory under your web root)
35 nextcloudFileDir='/var/www/nextcloud'
36
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'
40
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'
44
45 # TODO: The service name of the web server. Used to start/stop web server (e.g. 'systemctl start <webserverServiceName>')
46 webserverServiceName='nginx'
47
48 # TODO: Your web server user
49 webserverUser='www-data'
50
51 # TODO: The name of the database system (ome of: mysql, mariadb, postgresql)
52 databaseSystem='mariadb'
53
54 # TODO: Your Nextcloud database name
55 nextcloudDatabase='nextcloud_db'
56
57 # TODO: Your Nextcloud database user
58 dbUser='nextcloud_db_user'
59
60 # TODO: The password of the Nextcloud database user
61 dbPassword='mYpAsSw0rd'
62
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'
67
68 # TODO: Uncomment if you use local external storage
69 #fileNameBackupExternalDataDir='nextcloud-external-datadir.tar.gz'
70
71 fileNameBackupDb='nextcloud-db.sql'
72
73 # Function for error messages
74 errorecho() { cat <<< "$@" 1>&2; }
75
76 #
77 # Check if parameter(s) given
78 #
79 if [ $# != "1" ] && [ $# != "2" ]
80 then
81 errorecho "ERROR: No backup name to restore given, or wrong number of parameters!"
82 errorecho "Usage: NextcloudRestore.sh 'BackupDate' ['BackupDirectory']"
83 exit 1
84 fi
85
86 #
87 # Check for root
88 #
89 if [ "$(id -u)" != "0" ]
90 then
91 errorecho "ERROR: This script has to be run as root!"
92 exit 1
93 fi
94
95 #
96 # Check if backup dir exists
97 #
98 if [ ! -d "${currentRestoreDir}" ]
99 then
100 errorecho "ERROR: Backup ${restore} not found!"
101 exit 1
102 fi
103
104 #
105 # Check if the commands for restoring the database are available
106 #
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"
112 exit 1
113 fi
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"
119 exit 1
120 fi
121 fi
122
123 #
124 # Set maintenance mode
125 #
126 echo "Set maintenance mode for Nextcloud..."
127 sudo -u "${webserverUser}" php ${nextcloudFileDir}/occ maintenance:mode --on
128 echo "Done"
129 echo
130
131 #
132 # Stop web server
133 #
134 echo "Stopping web server..."
135 systemctl stop "${webserverServiceName}"
136 echo "Done"
137 echo
138
139 #
140 # Delete old Nextcloud directories
141 #
142
143 # File directory
144 echo "Deleting old Nextcloud file directory..."
145 rm -r "${nextcloudFileDir}"
146 mkdir -p "${nextcloudFileDir}"
147 echo "Done"
148 echo
149
150 # Data directory
151 echo "Deleting old Nextcloud data directory..."
152 rm -r "${nextcloudDataDir}"
153 mkdir -p "${nextcloudDataDir}"
154 echo "Done"
155 echo
156
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}"
162 #echo "Done"
163 #echo
164
165 #
166 # Restore file and data directory
167 #
168
169 # File directory
170 echo "Restoring Nextcloud file directory..."
171 tar -xmpzf "${currentRestoreDir}/${fileNameBackupFileDir}" -C "${nextcloudFileDir}"
172 echo "Done"
173 echo
174
175 # Data directory
176 echo "Restoring Nextcloud data directory..."
177 tar -xmpzf "${currentRestoreDir}/${fileNameBackupDataDir}" -C "${nextcloudDataDir}"
178 echo "Done"
179 echo
180
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}"
185 #echo "Done"
186 #echo
187
188 #
189 # Restore database
190 #
191 echo "Dropping old Nextcloud DB..."
192
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};"
197 fi
198
199 echo "Done"
200 echo
201
202 echo "Creating new DB for Nextcloud..."
203
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\";"
211 fi
212
213 echo "Done"
214 echo
215
216 echo "Restoring backup DB..."
217
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}"
222 fi
223
224 echo "Done"
225 echo
226
227 #
228 # Start web server
229 #
230 echo "Starting web server..."
231 systemctl start "${webserverServiceName}"
232 echo "Done"
233 echo
234
235 #
236 # Set directory permissions
237 #
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}"
243 echo "Done"
244 echo
245
246 #
247 # Update the system data-fingerprint (see https://docs.nextcloud.com/server/16/admin_manual/configuration_server/occ_command.html#maintenance-commands-label)
248 #
249 echo "Updating the system data-fingerprint..."
250 sudo -u "${webserverUser}" php ${nextcloudFileDir}/occ maintenance:data-fingerprint
251 echo "Done"
252 echo
253
254 #
255 # Disbale maintenance mode
256 #
257 echo "Switching off maintenance mode..."
258 sudo -u "${webserverUser}" php ${nextcloudFileDir}/occ maintenance:mode --off
259 echo "Done"
260 echo
261
262 echo
263 echo "DONE!"
264 echo "Backup ${restore} successfully restored."

patrick-canterino.de