4 # Bash script for creating backups of Nextcloud.
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)
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/
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".
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'
28 backupMainDir
=$
(echo $backupMainDir |
sed 's:/*$::')
31 currentDate
=$
(date +"%Y%m%d_%H%M%S")
33 # The actual directory of the current backup - this is a subdirectory of the main directory above with a timestamp
34 backupdir
="${backupMainDir}/${currentDate}/"
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.
42 # TODO: The directory of your Nextcloud installation (this is a directory under your web root)
43 nextcloudFileDir
='/var/www/nextcloud'
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'
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'
53 # TODO: The service name of the web server. Used to start/stop web server (e.g. 'systemctl start <webserverServiceName>')
54 webserverServiceName
='nginx'
56 # TODO: Your web server user
57 webserverUser
='www-data'
59 # TODO: The name of the database system (one of: mysql, mariadb, postgresql)
60 databaseSystem
='mariadb'
62 # TODO: Your Nextcloud database name
63 nextcloudDatabase
='nextcloud_db'
65 # TODO: Your Nextcloud database user
66 dbUser
='nextcloud_db_user'
68 # TODO: The password of the Nextcloud database user
69 dbPassword
='mYpAsSw0rd'
71 # TODO: The maximum number of backups to keep (when set to 0, all backups are kept)
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
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'
83 if [ "$useCompression" = true
] ; then
84 fileNameBackupFileDir
='nextcloud-filedir.tar.gz'
85 fileNameBackupDataDir
='nextcloud-datadir.tar.gz'
88 fileNameBackupExternalDataDir
=''
90 if [ ! -z "${nextcloudLocalExternalDataDir+x}" ] ; then
91 fileNameBackupExternalDataDir
='nextcloud-external-datadir.tar'
93 if [ "$useCompression" = true
] ; then
94 fileNameBackupExternalDataDir
='nextcloud-external-datadir.tar.gz'
98 fileNameBackupDb
='nextcloud-db.sql'
100 # Function for error messages
101 errorecho
() { cat <<< "$@" 1>&2; }
103 function DisableMaintenanceMode
() {
104 echo "$(date +"%H
:%M
:%S
"): Switching off maintenance mode..."
105 sudo
-u "${webserverUser}" php
${nextcloudFileDir}/occ maintenance
:mode
--off
114 read -p "Backup cancelled. Keep maintenance mode? [y/n] " -n 1 -r
117 if ! [[ $REPLY =~ ^
[Yy
]$
]]
119 DisableMaintenanceMode
121 echo "Maintenance mode still enabled."
124 echo "Starting web server..."
125 systemctl start
"${webserverServiceName}"
135 echo "Backup directory: ${backupMainDir}"
140 if [ "$(id -u)" != "0" ]
142 errorecho
"ERROR: This script has to be run as root!"
147 # Check if backup dir already exists
149 if [ ! -d "${backupdir}" ]
151 mkdir
-p "${backupdir}"
153 errorecho
"ERROR: The backup directory ${backupdir} already exists!"
158 # Set maintenance mode
160 echo "$(date +"%H
:%M
:%S
"): Set maintenance mode for Nextcloud..."
161 sudo
-u "${webserverUser}" php
${nextcloudFileDir}/occ maintenance
:mode
--on
168 echo "$(date +"%H
:%M
:%S
"): Stopping web server..."
169 systemctl stop
"${webserverServiceName}"
174 # Backup file directory
176 echo "$(date +"%H
:%M
:%S
"): Creating backup of Nextcloud file directory..."
178 if [ "$useCompression" = true
] ; then
179 tar -I pigz
-cpf "${backupdir}/${fileNameBackupFileDir}" -C "${nextcloudFileDir}" .
181 tar -cpf "${backupdir}/${fileNameBackupFileDir}" -C "${nextcloudFileDir}" .
188 # Backup data directory
190 echo "$(date +"%H
:%M
:%S
"): Creating backup of Nextcloud data directory..."
192 if [ "$ignoreUpdaterBackups" = true
] ; then
193 echo "Ignoring updater backup directory"
195 if [ "$useCompression" = true
] ; then
196 tar -I pigz
-cpf "${backupdir}/${fileNameBackupDataDir}" --exclude="updater-*/backups/*" -C "${nextcloudDataDir}" .
198 tar -cpf "${backupdir}/${fileNameBackupDataDir}" --exclude="updater-*/backups/*" -C "${nextcloudDataDir}" .
201 if [ "$useCompression" = true
] ; then
202 tar -I pigz
-cpf "${backupdir}/${fileNameBackupDataDir}" -C "${nextcloudDataDir}" .
204 tar -cpf "${backupdir}/${fileNameBackupDataDir}" -C "${nextcloudDataDir}" .
212 # Backup local external storage.
214 if [ ! -z "${nextcloudLocalExternalDataDir+x}" ] ; then
215 echo "$(date +"%H
:%M
:%S
"): Creating backup of Nextcloud local external storage directory..."
217 if [ "$useCompression" = true
] ; then
218 tar -I pigz
-cpf "${backupdir}/${fileNameBackupExternalDataDir}" -C "${nextcloudLocalExternalDataDir}" .
220 tar -cpf "${backupdir}/${fileNameBackupExternalDataDir}" -C "${nextcloudLocalExternalDataDir}" .
230 if [ "${databaseSystem,,}" = "mysql" ] ||
[ "${databaseSystem,,}" = "mariadb" ]; then
231 echo "$(date +"%H
:%M
:%S
"): Backup Nextcloud database (MySQL/MariaDB)..."
233 if ! [ -x "$(command -v mysqldump)" ]; then
234 errorecho
"ERROR: MySQL/MariaDB not installed (command mysqldump not found)."
235 errorecho
"ERROR: No backup of database possible!"
237 mysqldump
--single-transaction -h localhost
-u "${dbUser}" -p"${dbPassword}" "${nextcloudDatabase}" > "${backupdir}/${fileNameBackupDb}"
242 elif [ "${databaseSystem,,}" = "postgresql" ] ||
[ "${databaseSystem,,}" = "pgsql" ]; then
243 echo "$(date +"%H
:%M
:%S
"): Backup Nextcloud database (PostgreSQL)..."
245 if ! [ -x "$(command -v pg_dump)" ]; then
246 errorecho
"ERROR: PostgreSQL not installed (command pg_dump not found)."
247 errorecho
"ERROR: No backup of database possible!"
249 PGPASSWORD
="${dbPassword}" pg_dump "${nextcloudDatabase}" -h localhost -U "${dbUser}" -f "${backupdir}/${fileNameBackupDb}"
259 echo "$(date +"%H
:%M
:%S
"): Starting web server..."
260 systemctl start
"${webserverServiceName}"
265 # Disable maintenance mode
267 DisableMaintenanceMode
272 if [ ${maxNrOfBackups} != 0 ]
274 nrOfBackups
=$
(ls -l ${backupMainDir} |
grep -c ^d
)
276 if [ ${nrOfBackups} -gt ${maxNrOfBackups} ]
278 echo "$(date +"%H
:%M
:%S
"): Removing old backups..."
279 ls -t ${backupMainDir} |
tail -$
(( nrOfBackups
- maxNrOfBackups
)) |
while read -r dirToRemove
; do
280 echo "${dirToRemove}"
281 rm -r "${backupMainDir}/${dirToRemove:?}"
290 echo "$(date +"%H
:%M
:%S
"): Backup created: ${backupdir}"