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

patrick-canterino.de