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

patrick-canterino.de