]> git.p6c8.net - nextcloud-backup-restore.git/blob - NextcloudBackup.sh
2e8c62abbcc32b3e9f49ff0896fc4174d97e6257
[nextcloud-backup-restore.git] / NextcloudBackup.sh
1 #!/bin/bash
2
3 #
4 # Bash script for creating backups of Nextcloud.
5 #
6 # Version 2.1.1
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 # TODO: Uncomment if you use local external storage
89 #fileNameBackupExternalDataDir='nextcloud-external-datadir.tar'
90 #
91 #if [ "$useCompression" = true ] ; then
92 # fileNameBackupExternalDataDir='nextcloud-external-datadir.tar.gz'
93 #fi
94
95 fileNameBackupDb='nextcloud-db.sql'
96
97 # Function for error messages
98 errorecho() { cat <<< "$@" 1>&2; }
99
100 function DisableMaintenanceMode() {
101 echo "Switching off maintenance mode..."
102 sudo -u "${webserverUser}" php ${nextcloudFileDir}/occ maintenance:mode --off
103 echo "Done"
104 echo
105 }
106
107 # Capture CTRL+C
108 trap CtrlC INT
109
110 function CtrlC() {
111 read -p "Backup cancelled. Keep maintenance mode? [y/n] " -n 1 -r
112 echo
113
114 if ! [[ $REPLY =~ ^[Yy]$ ]]
115 then
116 DisableMaintenanceMode
117 else
118 echo "Maintenance mode still enabled."
119 fi
120
121 echo "Starting web server..."
122 systemctl start "${webserverServiceName}"
123 echo "Done"
124 echo
125
126 exit 1
127 }
128
129 #
130 # Print information
131 #
132 echo "Backup directory: ${backupMainDir}"
133
134 #
135 # Check for root
136 #
137 if [ "$(id -u)" != "0" ]
138 then
139 errorecho "ERROR: This script has to be run as root!"
140 exit 1
141 fi
142
143 #
144 # Check if backup dir already exists
145 #
146 if [ ! -d "${backupdir}" ]
147 then
148 mkdir -p "${backupdir}"
149 else
150 errorecho "ERROR: The backup directory ${backupdir} already exists!"
151 exit 1
152 fi
153
154 #
155 # Set maintenance mode
156 #
157 echo "Set maintenance mode for Nextcloud..."
158 sudo -u "${webserverUser}" php ${nextcloudFileDir}/occ maintenance:mode --on
159 echo "Done"
160 echo
161
162 #
163 # Stop web server
164 #
165 echo "Stopping web server..."
166 systemctl stop "${webserverServiceName}"
167 echo "Done"
168 echo
169
170 #
171 # Backup file directory
172 #
173 echo "Creating backup of Nextcloud file directory..."
174
175 if [ "$useCompression" = true ] ; then
176 tar -cpzf "${backupdir}/${fileNameBackupFileDir}" -C "${nextcloudFileDir}" .
177 else
178 tar -cpf "${backupdir}/${fileNameBackupFileDir}" -C "${nextcloudFileDir}" .
179 fi
180
181 echo "Done"
182 echo
183
184 #
185 # Backup data directory
186 #
187 echo "Creating backup of Nextcloud data directory..."
188
189 if [ "$ignoreUpdaterBackups" = true ] ; then
190 echo "Ignoring updater backup directory"
191
192 if [ "$useCompression" = true ] ; then
193 tar -cpzf "${backupdir}/${fileNameBackupDataDir}" --exclude="updater-*/backups/*" -C "${nextcloudDataDir}" .
194 else
195 tar -cpf "${backupdir}/${fileNameBackupDataDir}" --exclude="updater-*/backups/*" -C "${nextcloudDataDir}" .
196 fi
197 else
198 if [ "$useCompression" = true ] ; then
199 tar -cpzf "${backupdir}/${fileNameBackupDataDir}" -C "${nextcloudDataDir}" .
200 else
201 tar -cpf "${backupdir}/${fileNameBackupDataDir}" -C "${nextcloudDataDir}" .
202 fi
203 fi
204
205 echo "Done"
206 echo
207
208 # Backup local external storage.
209 # Uncomment if you use local external storage
210 #echo "Creating backup of Nextcloud local external storage directory..."
211
212 #if [ "$useCompression" = true ] ; then
213 # tar -cpzf "${backupdir}/${fileNameBackupExternalDataDir}" -C "${nextcloudLocalExternalDataDir}" .
214 #else
215 # tar -cpf "${backupdir}/${fileNameBackupExternalDataDir}" -C "${nextcloudLocalExternalDataDir}" .
216 #fi
217
218 #echo "Done"
219 #echo
220
221 #
222 # Backup DB
223 #
224 if [ "${databaseSystem,,}" = "mysql" ] || [ "${databaseSystem,,}" = "mariadb" ]; then
225 echo "Backup Nextcloud database (MySQL/MariaDB)..."
226
227 if ! [ -x "$(command -v mysqldump)" ]; then
228 errorecho "ERROR: MySQL/MariaDB not installed (command mysqldump not found)."
229 errorecho "ERROR: No backup of database possible!"
230 else
231 mysqldump --single-transaction -h localhost -u "${dbUser}" -p"${dbPassword}" "${nextcloudDatabase}" > "${backupdir}/${fileNameBackupDb}"
232 fi
233
234 echo "Done"
235 echo
236 elif [ "${databaseSystem,,}" = "postgresql" ] || [ "${databaseSystem,,}" = "pgsql" ]; then
237 echo "Backup Nextcloud database (PostgreSQL)..."
238
239 if ! [ -x "$(command -v pg_dump)" ]; then
240 errorecho "ERROR: PostgreSQL not installed (command pg_dump not found)."
241 errorecho "ERROR: No backup of database possible!"
242 else
243 PGPASSWORD="${dbPassword}" pg_dump "${nextcloudDatabase}" -h localhost -U "${dbUser}" -f "${backupdir}/${fileNameBackupDb}"
244 fi
245
246 echo "Done"
247 echo
248 fi
249
250 #
251 # Start web server
252 #
253 echo "Starting web server..."
254 systemctl start "${webserverServiceName}"
255 echo "Done"
256 echo
257
258 #
259 # Disable maintenance mode
260 #
261 DisableMaintenanceMode
262
263 #
264 # Delete old backups
265 #
266 if [ ${maxNrOfBackups} != 0 ]
267 then
268 nrOfBackups=$(ls -l ${backupMainDir} | grep -c ^d)
269
270 if [ ${nrOfBackups} -gt ${maxNrOfBackups} ]
271 then
272 echo "Removing old backups..."
273 ls -t ${backupMainDir} | tail -$(( nrOfBackups - maxNrOfBackups )) | while read -r dirToRemove; do
274 echo "${dirToRemove}"
275 rm -r "${backupMainDir}/${dirToRemove:?}"
276 echo "Done"
277 echo
278 done
279 fi
280 fi
281
282 echo
283 echo "DONE!"
284 echo "Backup created: ${backupdir}"

patrick-canterino.de