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

patrick-canterino.de