]> git.p6c8.net - nextcloud-backup-restore.git/blob - NextcloudBackup.sh
d1b1ed2a403866ff2172e619a7746eee8c30446c
[nextcloud-backup-restore.git] / NextcloudBackup.sh
1 #!/bin/bash
2
3 #
4 # Bash script for creating backups of Nextcloud.
5 #
6 # Version 1.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: 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 (ome 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 safe 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 exit 1
105 }
106
107 #
108 # Print information
109 #
110 echo "Backup directory: ${backupMainDir}"
111
112 #
113 # Check for root
114 #
115 if [ "$(id -u)" != "0" ]
116 then
117 errorecho "ERROR: This script has to be run as root!"
118 exit 1
119 fi
120
121 #
122 # Check if backup dir already exists
123 #
124 if [ ! -d "${backupdir}" ]
125 then
126 mkdir -p "${backupdir}"
127 else
128 errorecho "ERROR: The backup directory ${backupdir} already exists!"
129 exit 1
130 fi
131
132 #
133 # Set maintenance mode
134 #
135 echo "Set maintenance mode for Nextcloud..."
136 sudo -u "${webserverUser}" php ${nextcloudFileDir}/occ maintenance:mode --on
137 echo "Done"
138 echo
139
140 #
141 # Stop web server
142 #
143 echo "Stopping web server..."
144 systemctl stop "${webserverServiceName}"
145 echo "Done"
146 echo
147
148 #
149 # Backup file directory
150 #
151 echo "Creating backup of Nextcloud file directory..."
152 tar -cpzf "${backupdir}/${fileNameBackupFileDir}" -C "${nextcloudFileDir}" .
153 echo "Done"
154 echo
155
156 #
157 # Backup data directory
158 #
159 echo "Creating backup of Nextcloud data directory..."
160
161 if [ "$ignoreUpdaterBackups" = true ] ; then
162 echo "Ignoring updater backup directory"
163 tar -cpzf "${backupdir}/${fileNameBackupDataDir}" --exclude="updater-*/backups/*" -C "${nextcloudDataDir}" .
164 else
165 tar -cpzf "${backupdir}/${fileNameBackupDataDir}" -C "${nextcloudDataDir}" .
166 fi
167
168 echo "Done"
169 echo
170
171 # Backup local external storage.
172 # Uncomment if you use local external storage
173 #echo "Creating backup of Nextcloud local external storage directory..."
174 #tar -cpzf "${backupdir}/${fileNameBackupExternalDataDir}" -C "${nextcloudLocalExternalDataDir}" .
175 #echo "Done"
176 #echo
177
178 #
179 # Backup DB
180 #
181 if [ "${databaseSystem,,}" = "mysql" ] || [ "${databaseSystem,,}" = "mariadb" ]; then
182 echo "Backup Nextcloud database (MySQL/MariaDB)..."
183
184 if ! [ -x "$(command -v mysqldump)" ]; then
185 errorecho "ERROR: MySQL/MariaDB not installed (command mysqldump not found)."
186 errorecho "ERROR: No backup of database possible!"
187 else
188 mysqldump --single-transaction -h localhost -u "${dbUser}" -p"${dbPassword}" "${nextcloudDatabase}" > "${backupdir}/${fileNameBackupDb}"
189 fi
190
191 echo "Done"
192 echo
193 elif [ "${databaseSystem,,}" = "postgresql" ]; then
194 echo "Backup Nextcloud database (PostgreSQL)..."
195
196 if ! [ -x "$(command -v pg_dump)" ]; then
197 errorecho "ERROR:PostgreSQL not installed (command pg_dump not found)."
198 errorecho "ERROR: No backup of database possible!"
199 else
200 PGPASSWORD="${dbPassword}" pg_dump "${nextcloudDatabase}" -h localhost -U "${dbUser}" -f "${backupdir}/${fileNameBackupDb}"
201 fi
202
203 echo "Done"
204 echo
205 fi
206
207 #
208 # Start web server
209 #
210 echo "Starting web server..."
211 systemctl start "${webserverServiceName}"
212 echo "Done"
213 echo
214
215 #
216 # Disable maintenance mode
217 #
218 DisableMaintenanceMode
219
220 #
221 # Delete old backups
222 #
223 if [ ${maxNrOfBackups} != 0 ]
224 then
225 nrOfBackups=$(ls -l ${backupMainDir} | grep -c ^d)
226
227 if [[ ${nrOfBackups} > ${maxNrOfBackups} ]]
228 then
229 echo "Removing old backups..."
230 ls -t ${backupMainDir} | tail -$(( nrOfBackups - maxNrOfBackups )) | while read -r dirToRemove; do
231 echo "${dirToRemove}"
232 rm -r "${backupMainDir}/${dirToRemove:?}"
233 echo "Done"
234 echo
235 done
236 fi
237 fi
238
239 echo
240 echo "DONE!"
241 echo "Backup created: ${backupdir}"

patrick-canterino.de