]> git.p6c8.net - nextcloud-backup-restore.git/blob - NextcloudRestore.sh
1669a181d3925943dbaba40930ab6173186e2841
[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 cd "${nextcloudFileDir}"
103 sudo -u "${webserverUser}" php occ maintenance:mode --on
104 cd ~
105 echo "Done"
106 echo
107
108 #
109 # Stop web server
110 #
111 echo "Stopping web server..."
112 systemctl stop "${webserverServiceName}"
113 echo "Done"
114 echo
115
116 #
117 # Delete old Nextcloud direcories
118 #
119
120 # File directory
121 echo "Deleting old Nextcloud file directory..."
122 rm -r "${nextcloudFileDir}"
123 mkdir -p "${nextcloudFileDir}"
124 echo "Done"
125 echo
126
127 # Data directory
128 echo "Deleting old Nextcloud data directory..."
129 rm -r "${nextcloudDataDir}"
130 mkdir -p "${nextcloudDataDir}"
131 echo "Done"
132 echo
133
134 # Local external storage
135 # TOOD: Uncomment if you use local external storage
136 #echo "Deleting old Nextcloud local external storage directory..."
137 #rm -r "${nextcloudLocalExternalDataDir}"
138 #mkdir -p "${nextcloudLocalExternalDataDir}"
139 #echo "Done"
140 #echo
141
142 #
143 # Restore file and data directory
144 #
145
146 # File directory
147 echo "Restoring Nextcloud file directory..."
148 tar -xmpzf "${currentRestoreDir}/${fileNameBackupFileDir}" -C "${nextcloudFileDir}"
149 echo "Done"
150 echo
151
152 # Data directory
153 echo "Restoring Nextcloud data directory..."
154 tar -xmpzf "${currentRestoreDir}/${fileNameBackupDataDir}" -C "${nextcloudDataDir}"
155 echo "Done"
156 echo
157
158 # Local external storage
159 # TOOD: Uncomment if you use local external storage
160 #echo "Restoring Nextcloud data directory..."
161 #tar -xmpzf "${currentRestoreDir}/${fileNameBackupExternalDataDir}" -C "${nextcloudLocalExternalDataDir}"
162 #echo "Done"
163 #echo
164
165 #
166 # Restore database
167 #
168 echo "Dropping old Nextcloud DB..."
169 # MySQL/MariaDB:
170 mysql -h localhost -u "${dbUser}" -p"${dbPassword}" -e "DROP DATABASE ${nextcloudDatabase}"
171
172 # PostgreSQL (uncomment if you are using PostgreSQL as Nextcloud database)
173 #sudo -u postgres psql -c "DROP DATABASE ${nextcloudDatabase};"
174 echo "Done"
175 echo
176
177 echo "Creating new DB for Nextcloud..."
178 # MySQL/MariaDB:
179 # Use this if the databse from the backup uses UTF8 with multibyte support (e.g. for emoijs in filenames):
180 mysql -h localhost -u "${dbUser}" -p"${dbPassword}" -e "CREATE DATABASE ${nextcloudDatabase} CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci"
181 # Use this if the database from the backup DOES NOT use UTF8 with multibyte support (e.g. for emoijs in filenames):
182 #mysql -h localhost -u "${dbUser}" -p"${dbPassword}" -e "CREATE DATABASE ${nextcloudDatabase}"
183
184 # PostgreSQL (uncomment if you are using PostgreSQL as Nextcloud database)
185 #sudo -u postgres psql -c "CREATE DATABASE ${nextcloudDatabase} WITH OWNER ${dbUser} TEMPLATE template0 ENCODING \"UTF8\";"
186 echo "Done"
187 echo
188
189 echo "Restoring backup DB..."
190 # MySQL/MariaDB:
191 mysql -h localhost -u "${dbUser}" -p"${dbPassword}" "${nextcloudDatabase}" < "${currentRestoreDir}/${fileNameBackupDb}"
192
193 # PostgreSQL (uncomment if you are using PostgreSQL as Nextcloud database)
194 #sudo -u postgres psql "${nextcloudDatabase}" < "${currentRestoreDir}/${fileNameBackupDb}"
195 echo "Done"
196 echo
197
198 #
199 # Start web server
200 #
201 echo "Starting web server..."
202 systemctl start "${webserverServiceName}"
203 echo "Done"
204 echo
205
206 #
207 # Set directory permissions
208 #
209 echo "Setting directory permissions..."
210 chown -R "${webserverUser}":"${webserverUser}" "${nextcloudFileDir}"
211 chown -R "${webserverUser}":"${webserverUser}" "${nextcloudDataDir}"
212 # TOOD: Uncomment if you use local external storage
213 #chown -R "${webserverUser}":"${webserverUser}" "${nextcloudLocalExternalDataDir}"
214 echo "Done"
215 echo
216
217 #
218 # Update the system data-fingerprint (see https://docs.nextcloud.com/server/15/admin_manual/configuration_server/occ_command.html#maintenance-commands-label)
219 #
220 echo "Updating the system data-fingerprint..."
221 sudo -u "${webserverUser}" php ${nextcloudFileDir}/occ maintenance:data-fingerprint
222 echo "Done"
223 echo
224
225 #
226 # Disbale maintenance mode
227 #
228 echo "Switching off maintenance mode..."
229 sudo -u "${webserverUser}" php ${nextcloudFileDir}/occ maintenance:mode --off
230 echo "Done"
231 echo
232
233 echo
234 echo "DONE!"
235 echo "Backup ${restore} successfully restored."

patrick-canterino.de