]> git.p6c8.net - nextcloud-backup-restore.git/blob - NextcloudBackup.sh
0b03af13afe5deaf85510261289b47392cf9ab9a
[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 #if [ "$useCompression" = true ] ; then
210 # tar -cpzf "${backupdir}/${fileNameBackupExternalDataDir}" -C "${nextcloudLocalExternalDataDir}" .
211 #else
212 # tar -cpf "${backupdir}/${fileNameBackupExternalDataDir}" -C "${nextcloudLocalExternalDataDir}" .
213 #fi
214
215 #echo "Done"
216 #echo
217
218 #
219 # Backup DB
220 #
221 if [ "${databaseSystem,,}" = "mysql" ] || [ "${databaseSystem,,}" = "mariadb" ]; then
222 echo "Backup Nextcloud database (MySQL/MariaDB)..."
223
224 if ! [ -x "$(command -v mysqldump)" ]; then
225 errorecho "ERROR: MySQL/MariaDB not installed (command mysqldump not found)."
226 errorecho "ERROR: No backup of database possible!"
227 else
228 mysqldump --single-transaction -h localhost -u "${dbUser}" -p"${dbPassword}" "${nextcloudDatabase}" > "${backupdir}/${fileNameBackupDb}"
229 fi
230
231 echo "Done"
232 echo
233 elif [ "${databaseSystem,,}" = "postgresql" ] || [ "${databaseSystem,,}" = "pgsql" ]; then
234 echo "Backup Nextcloud database (PostgreSQL)..."
235
236 if ! [ -x "$(command -v pg_dump)" ]; then
237 errorecho "ERROR: PostgreSQL not installed (command pg_dump not found)."
238 errorecho "ERROR: No backup of database possible!"
239 else
240 PGPASSWORD="${dbPassword}" pg_dump "${nextcloudDatabase}" -h localhost -U "${dbUser}" -f "${backupdir}/${fileNameBackupDb}"
241 fi
242
243 echo "Done"
244 echo
245 fi
246
247 #
248 # Start web server
249 #
250 echo "Starting web server..."
251 systemctl start "${webserverServiceName}"
252 echo "Done"
253 echo
254
255 #
256 # Disable maintenance mode
257 #
258 DisableMaintenanceMode
259
260 #
261 # Delete old backups
262 #
263 if [ ${maxNrOfBackups} != 0 ]
264 then
265 nrOfBackups=$(ls -l ${backupMainDir} | grep -c ^d)
266
267 if [[ ${nrOfBackups} > ${maxNrOfBackups} ]]
268 then
269 echo "Removing old backups..."
270 ls -t ${backupMainDir} | tail -$(( nrOfBackups - maxNrOfBackups )) | while read -r dirToRemove; do
271 echo "${dirToRemove}"
272 rm -r "${backupMainDir}/${dirToRemove:?}"
273 echo "Done"
274 echo
275 done
276 fi
277 fi
278
279 echo
280 echo "DONE!"
281 echo "Backup created: ${backupdir}"

patrick-canterino.de