]> git.p6c8.net - nextcloud-backup-restore.git/blob - NextcloudBackup.sh
Introduced new config variable dbNoMultibyte for restoring database without multibyte
[nextcloud-backup-restore.git] / NextcloudBackup.sh
1 #!/bin/bash
2
3 #
4 # Bash script for creating backups of Nextcloud.
5 #
6 # Version 2.1.3
7 #
8 # Usage:
9 # - With backup directory specified in the script: ./NextcloudBackup.sh
10 # - With backup directory specified by parameter: ./NextcloudBackup.sh <BackupDirectory> (e.g. ./NextcloudBackup.sh /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 backupMainDir=$1
23
24 if [ -z "$backupMainDir" ]; then
25 # TODO: The directory where you store the Nextcloud backups (when not specified by args)
26 backupMainDir='/media/hdd/nextcloud_backup'
27 else
28 backupMainDir=$(echo $backupMainDir | sed 's:/*$::')
29 fi
30
31 currentDate=$(date +"%Y%m%d_%H%M%S")
32
33 # The actual directory of the current backup - this is a subdirectory of the main directory above with a timestamp
34 backupdir="${backupMainDir}/${currentDate}/"
35
36 # TODO: Use compression for file/data dir
37 # When this is the only script for backups, it's recommend to enable compression.
38 # If the output of this script is used in another (compressing) backup (e.g. borg backup),
39 # you should probably disable compression here and only enable compression of your main backup script.
40 useCompression=true
41
42 # TODO: The directory of your Nextcloud installation (this is a directory under your web root)
43 nextcloudFileDir='/var/www/nextcloud'
44
45 # TODO: The directory of your Nextcloud data directory (outside the Nextcloud file directory)
46 # If your data directory is located under Nextcloud's file directory (somewhere in the web root), the data directory should not be a separate part of the backup
47 nextcloudDataDir='/var/nextcloud_data'
48
49 # TODO: The directory of your Nextcloud's local external storage.
50 # Uncomment if you use local external storage.
51 #nextcloudLocalExternalDataDir='/var/nextcloud_external_data'
52
53 # TODO: The service name of the web server. Used to start/stop web server (e.g. 'systemctl start <webserverServiceName>')
54 webserverServiceName='nginx'
55
56 # TODO: Your web server user
57 webserverUser='www-data'
58
59 # TODO: The name of the database system (one of: mysql, mariadb, postgresql)
60 databaseSystem='mariadb'
61
62 # TODO: Your Nextcloud database name
63 nextcloudDatabase='nextcloud_db'
64
65 # TODO: Your Nextcloud database user
66 dbUser='nextcloud_db_user'
67
68 # TODO: The password of the Nextcloud database user
69 dbPassword='mYpAsSw0rd'
70
71 # TODO: The maximum number of backups to keep (when set to 0, all backups are kept)
72 maxNrOfBackups=0
73
74 # TODO: Ignore updater's backup directory in the data directory to save space
75 # Set to true to ignore the backup directory
76 ignoreUpdaterBackups=false
77
78 # File names for backup files
79 # If you prefer other file names, you'll also have to change the NextcloudRestore.sh script.
80 fileNameBackupFileDir='nextcloud-filedir.tar'
81 fileNameBackupDataDir='nextcloud-datadir.tar'
82
83 if [ "$useCompression" = true ] ; then
84 fileNameBackupFileDir='nextcloud-filedir.tar.gz'
85 fileNameBackupDataDir='nextcloud-datadir.tar.gz'
86 fi
87
88 fileNameBackupExternalDataDir=''
89
90 if [ ! -z "${nextcloudLocalExternalDataDir+x}" ] ; then
91 fileNameBackupExternalDataDir='nextcloud-external-datadir.tar'
92
93 if [ "$useCompression" = true ] ; then
94 fileNameBackupExternalDataDir='nextcloud-external-datadir.tar.gz'
95 fi
96 fi
97
98 fileNameBackupDb='nextcloud-db.sql'
99
100 # Function for error messages
101 errorecho() { cat <<< "$@" 1>&2; }
102
103 function DisableMaintenanceMode() {
104 echo "$(date +"%H:%M:%S"): Switching off maintenance mode..."
105 sudo -u "${webserverUser}" php ${nextcloudFileDir}/occ maintenance:mode --off
106 echo "Done"
107 echo
108 }
109
110 # Capture CTRL+C
111 trap CtrlC INT
112
113 function CtrlC() {
114 read -p "Backup cancelled. Keep maintenance mode? [y/n] " -n 1 -r
115 echo
116
117 if ! [[ $REPLY =~ ^[Yy]$ ]]
118 then
119 DisableMaintenanceMode
120 else
121 echo "Maintenance mode still enabled."
122 fi
123
124 echo "Starting web server..."
125 systemctl start "${webserverServiceName}"
126 echo "Done"
127 echo
128
129 exit 1
130 }
131
132 #
133 # Print information
134 #
135 echo "Backup directory: ${backupMainDir}"
136
137 #
138 # Check for root
139 #
140 if [ "$(id -u)" != "0" ]
141 then
142 errorecho "ERROR: This script has to be run as root!"
143 exit 1
144 fi
145
146 #
147 # Check if backup dir already exists
148 #
149 if [ ! -d "${backupdir}" ]
150 then
151 mkdir -p "${backupdir}"
152 else
153 errorecho "ERROR: The backup directory ${backupdir} already exists!"
154 exit 1
155 fi
156
157 #
158 # Set maintenance mode
159 #
160 echo "$(date +"%H:%M:%S"): Set maintenance mode for Nextcloud..."
161 sudo -u "${webserverUser}" php ${nextcloudFileDir}/occ maintenance:mode --on
162 echo "Done"
163 echo
164
165 #
166 # Stop web server
167 #
168 echo "$(date +"%H:%M:%S"): Stopping web server..."
169 systemctl stop "${webserverServiceName}"
170 echo "Done"
171 echo
172
173 #
174 # Backup file directory
175 #
176 echo "$(date +"%H:%M:%S"): Creating backup of Nextcloud file directory..."
177
178 if [ "$useCompression" = true ] ; then
179 tar -I pigz -cpf "${backupdir}/${fileNameBackupFileDir}" -C "${nextcloudFileDir}" .
180 else
181 tar -cpf "${backupdir}/${fileNameBackupFileDir}" -C "${nextcloudFileDir}" .
182 fi
183
184 echo "Done"
185 echo
186
187 #
188 # Backup data directory
189 #
190 echo "$(date +"%H:%M:%S"): Creating backup of Nextcloud data directory..."
191
192 if [ "$ignoreUpdaterBackups" = true ] ; then
193 echo "Ignoring updater backup directory"
194
195 if [ "$useCompression" = true ] ; then
196 tar -I pigz -cpf "${backupdir}/${fileNameBackupDataDir}" --exclude="updater-*/backups/*" -C "${nextcloudDataDir}" .
197 else
198 tar -cpf "${backupdir}/${fileNameBackupDataDir}" --exclude="updater-*/backups/*" -C "${nextcloudDataDir}" .
199 fi
200 else
201 if [ "$useCompression" = true ] ; then
202 tar -I pigz -cpf "${backupdir}/${fileNameBackupDataDir}" -C "${nextcloudDataDir}" .
203 else
204 tar -cpf "${backupdir}/${fileNameBackupDataDir}" -C "${nextcloudDataDir}" .
205 fi
206 fi
207
208 echo "Done"
209 echo
210
211 #
212 # Backup local external storage.
213 #
214 if [ ! -z "${nextcloudLocalExternalDataDir+x}" ] ; then
215 echo "$(date +"%H:%M:%S"): Creating backup of Nextcloud local external storage directory..."
216
217 if [ "$useCompression" = true ] ; then
218 tar -I pigz -cpf "${backupdir}/${fileNameBackupExternalDataDir}" -C "${nextcloudLocalExternalDataDir}" .
219 else
220 tar -cpf "${backupdir}/${fileNameBackupExternalDataDir}" -C "${nextcloudLocalExternalDataDir}" .
221 fi
222
223 echo "Done"
224 echo
225 fi
226
227 #
228 # Backup DB
229 #
230 if [ "${databaseSystem,,}" = "mysql" ] || [ "${databaseSystem,,}" = "mariadb" ]; then
231 echo "$(date +"%H:%M:%S"): Backup Nextcloud database (MySQL/MariaDB)..."
232
233 if ! [ -x "$(command -v mysqldump)" ]; then
234 errorecho "ERROR: MySQL/MariaDB not installed (command mysqldump not found)."
235 errorecho "ERROR: No backup of database possible!"
236 else
237 mysqldump --single-transaction -h localhost -u "${dbUser}" -p"${dbPassword}" "${nextcloudDatabase}" > "${backupdir}/${fileNameBackupDb}"
238 fi
239
240 echo "Done"
241 echo
242 elif [ "${databaseSystem,,}" = "postgresql" ] || [ "${databaseSystem,,}" = "pgsql" ]; then
243 echo "$(date +"%H:%M:%S"): Backup Nextcloud database (PostgreSQL)..."
244
245 if ! [ -x "$(command -v pg_dump)" ]; then
246 errorecho "ERROR: PostgreSQL not installed (command pg_dump not found)."
247 errorecho "ERROR: No backup of database possible!"
248 else
249 PGPASSWORD="${dbPassword}" pg_dump "${nextcloudDatabase}" -h localhost -U "${dbUser}" -f "${backupdir}/${fileNameBackupDb}"
250 fi
251
252 echo "Done"
253 echo
254 fi
255
256 #
257 # Start web server
258 #
259 echo "$(date +"%H:%M:%S"): Starting web server..."
260 systemctl start "${webserverServiceName}"
261 echo "Done"
262 echo
263
264 #
265 # Disable maintenance mode
266 #
267 DisableMaintenanceMode
268
269 #
270 # Delete old backups
271 #
272 if [ ${maxNrOfBackups} != 0 ]
273 then
274 nrOfBackups=$(ls -l ${backupMainDir} | grep -c ^d)
275
276 if [ ${nrOfBackups} -gt ${maxNrOfBackups} ]
277 then
278 echo "$(date +"%H:%M:%S"): Removing old backups..."
279 ls -t ${backupMainDir} | tail -$(( nrOfBackups - maxNrOfBackups )) | while read -r dirToRemove; do
280 echo "${dirToRemove}"
281 rm -r "${backupMainDir}/${dirToRemove:?}"
282 echo "Done"
283 echo
284 done
285 fi
286 fi
287
288 echo
289 echo "DONE!"
290 echo "$(date +"%H:%M:%S"): Backup created: ${backupdir}"

patrick-canterino.de