]> git.p6c8.net - nextcloud-backup-restore.git/blob - NextcloudRestore.sh
Updated links
[nextcloud-backup-restore.git] / NextcloudRestore.sh
1 #!/bin/bash
2
3 #
4 # Bash script for restoring backups of Nextcloud.
5 # Usage:
6 # - With backup directory specified in the script: ./NextcloudRestore.sh <BackupName> (e.g. ./NextcloudRestore.sh 20170910_132703)
7 # - With backup directory specified by parameter: ./NextcloudRestore.sh <BackupName> <BackupDirectory> (e.g. ./NextcloudRestore.sh 20170910_132703 /media/hdd/nextcloud_backup)
8 #
9 # 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/
10 #
11
12 #
13 # IMPORTANT
14 # You have to customize this script (directories, users, etc.) for your actual environment.
15 # All entries which need to be customized are tagged with "TODO".
16 #
17
18 # Variables
19 restore=$1
20 backupMainDir=$2
21
22 if [ -z "$backupMainDir" ]; then
23 # TODO: The directory where you store the Nextcloud backups (when not specified by args)
24 backupMainDir="/mnt/hdd1/nextcloudb_backup"
25 fi
26
27 echo "Backup directory: $backupMainDir"
28
29 currentRestoreDir="${backupMainDir}/${restore}"
30
31 # TODO: The directory of your Nextcloud installation (this is a directory under your web root)
32 nextcloudFileDir="/var/www/nextcloud"
33
34 # TODO: The directory of your Nextcloud data directory (outside the Nextcloud file directory)
35 # If your data directory is located under Nextcloud's file directory (somewhere in the web root), the data directory should not be restored separately
36 nextcloudDataDir="/var/nextcloud_data"
37
38 # TODO: The directory of your Nextcloud's local external storage.
39 # Uncomment if you use local external storage.
40 #nextcloudLocalExternalDataDir="/var/nextcloud_external_data"
41
42 # TODO: The service name of the web server. Used to start/stop web server (e.g. 'systemctl start <webserverServiceName>')
43 webserverServiceName="nginx"
44
45 # TODO: Your Nextcloud database name
46 nextcloudDatabase="nextcloud_db"
47
48 # TODO: Your Nextcloud database user
49 dbUser="nextcloud_db_user"
50
51 # TODO: The password of the Nextcloud database user
52 dbPassword="mYpAsSw0rd"
53
54 # TODO: Your web server user
55 webserverUser="www-data"
56
57 # File names for backup files
58 # If you prefer other file names, you'll also have to change the NextcloudBackup.sh script.
59 fileNameBackupFileDir="nextcloud-filedir.tar.gz"
60 fileNameBackupDataDir="nextcloud-datadir.tar.gz"
61
62 # TOOD: Uncomment if you use local external storage
63 #fileNameBackupExternalDataDir="nextcloud-external-datadir.tar.gz"
64
65 fileNameBackupDb="nextcloud-db.sql"
66
67 # Function for error messages
68 errorecho() { cat <<< "$@" 1>&2; }
69
70 #
71 # Check if parameter(s) given
72 #
73 if [ $# != "1" ] && [ $# != "2" ]
74 then
75 errorecho "ERROR: No backup name to restore given, or wrong number of parameters!"
76 errorecho "Usage: NextcloudRestore.sh 'BackupDate' ['BackupDirectory']"
77 exit 1
78 fi
79
80 #
81 # Check for root
82 #
83 if [ "$(id -u)" != "0" ]
84 then
85 errorecho "ERROR: This script has to be run as root!"
86 exit 1
87 fi
88
89 #
90 # Check if backup dir exists
91 #
92 if [ ! -d "${currentRestoreDir}" ]
93 then
94 errorecho "ERROR: Backup ${restore} not found!"
95 exit 1
96 fi
97
98 #
99 # Set maintenance mode
100 #
101 echo "Set maintenance mode for Nextcloud..."
102 sudo -u "${webserverUser}" php ${nextcloudFileDir}/occ maintenance:mode --on
103 echo "Done"
104 echo
105
106 #
107 # Stop web server
108 #
109 echo "Stopping web server..."
110 systemctl stop "${webserverServiceName}"
111 echo "Done"
112 echo
113
114 #
115 # Delete old Nextcloud direcories
116 #
117
118 # File directory
119 echo "Deleting old Nextcloud file directory..."
120 rm -r "${nextcloudFileDir}"
121 mkdir -p "${nextcloudFileDir}"
122 echo "Done"
123 echo
124
125 # Data directory
126 echo "Deleting old Nextcloud data directory..."
127 rm -r "${nextcloudDataDir}"
128 mkdir -p "${nextcloudDataDir}"
129 echo "Done"
130 echo
131
132 # Local external storage
133 # TOOD: Uncomment if you use local external storage
134 #echo "Deleting old Nextcloud local external storage directory..."
135 #rm -r "${nextcloudLocalExternalDataDir}"
136 #mkdir -p "${nextcloudLocalExternalDataDir}"
137 #echo "Done"
138 #echo
139
140 #
141 # Restore file and data directory
142 #
143
144 # File directory
145 echo "Restoring Nextcloud file directory..."
146 tar -xmpzf "${currentRestoreDir}/${fileNameBackupFileDir}" -C "${nextcloudFileDir}"
147 echo "Done"
148 echo
149
150 # Data directory
151 echo "Restoring Nextcloud data directory..."
152 tar -xmpzf "${currentRestoreDir}/${fileNameBackupDataDir}" -C "${nextcloudDataDir}"
153 echo "Done"
154 echo
155
156 # Local external storage
157 # TOOD: Uncomment if you use local external storage
158 #echo "Restoring Nextcloud data directory..."
159 #tar -xmpzf "${currentRestoreDir}/${fileNameBackupExternalDataDir}" -C "${nextcloudLocalExternalDataDir}"
160 #echo "Done"
161 #echo
162
163 #
164 # Restore database
165 #
166 echo "Dropping old Nextcloud DB..."
167 # MySQL/MariaDB:
168 mysql -h localhost -u "${dbUser}" -p"${dbPassword}" -e "DROP DATABASE ${nextcloudDatabase}"
169
170 # PostgreSQL (uncomment if you are using PostgreSQL as Nextcloud database)
171 #sudo -u postgres psql -c "DROP DATABASE ${nextcloudDatabase};"
172 echo "Done"
173 echo
174
175 echo "Creating new DB for Nextcloud..."
176 # MySQL/MariaDB:
177 # Use this if the databse from the backup uses UTF8 with multibyte support (e.g. for emoijs in filenames):
178 mysql -h localhost -u "${dbUser}" -p"${dbPassword}" -e "CREATE DATABASE ${nextcloudDatabase} CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci"
179 # Use this if the database from the backup DOES NOT use UTF8 with multibyte support (e.g. for emoijs in filenames):
180 #mysql -h localhost -u "${dbUser}" -p"${dbPassword}" -e "CREATE DATABASE ${nextcloudDatabase}"
181
182 # PostgreSQL (uncomment if you are using PostgreSQL as Nextcloud database)
183 #sudo -u postgres psql -c "CREATE DATABASE ${nextcloudDatabase} WITH OWNER ${dbUser} TEMPLATE template0 ENCODING \"UTF8\";"
184 echo "Done"
185 echo
186
187 echo "Restoring backup DB..."
188 # MySQL/MariaDB:
189 mysql -h localhost -u "${dbUser}" -p"${dbPassword}" "${nextcloudDatabase}" < "${currentRestoreDir}/${fileNameBackupDb}"
190
191 # PostgreSQL (uncomment if you are using PostgreSQL as Nextcloud database)
192 #sudo -u postgres psql "${nextcloudDatabase}" < "${currentRestoreDir}/${fileNameBackupDb}"
193 echo "Done"
194 echo
195
196 #
197 # Start web server
198 #
199 echo "Starting web server..."
200 systemctl start "${webserverServiceName}"
201 echo "Done"
202 echo
203
204 #
205 # Set directory permissions
206 #
207 echo "Setting directory permissions..."
208 chown -R "${webserverUser}":"${webserverUser}" "${nextcloudFileDir}"
209 chown -R "${webserverUser}":"${webserverUser}" "${nextcloudDataDir}"
210 # TOOD: Uncomment if you use local external storage
211 #chown -R "${webserverUser}":"${webserverUser}" "${nextcloudLocalExternalDataDir}"
212 echo "Done"
213 echo
214
215 #
216 # Update the system data-fingerprint (see https://docs.nextcloud.com/server/16/admin_manual/configuration_server/occ_command.html#maintenance-commands-label)
217 #
218 echo "Updating the system data-fingerprint..."
219 sudo -u "${webserverUser}" php ${nextcloudFileDir}/occ maintenance:data-fingerprint
220 echo "Done"
221 echo
222
223 #
224 # Disbale maintenance mode
225 #
226 echo "Switching off maintenance mode..."
227 sudo -u "${webserverUser}" php ${nextcloudFileDir}/occ maintenance:mode --off
228 echo "Done"
229 echo
230
231 echo
232 echo "DONE!"
233 echo "Backup ${restore} successfully restored."

patrick-canterino.de