4 # Bash script for creating backups of Nextcloud.
9 # - pigz (https://zlib.net/pigz/) for using backup compression. If not available, you can use another compression algorithm (e.g. gzip)
11 # Supported database systems:
16 # - With backup directory specified in the script: ./NextcloudBackup.sh
17 # - With backup directory specified by parameter: ./NextcloudBackup.sh <backupDirectory> (e.g. ./NextcloudBackup.sh /media/hdd/nextcloud_backup)
19 # The script is based on an installation of Nextcloud using nginx and MariaDB, see https://decatec.de/home-server/nextcloud-auf-ubuntu-server-20-04-lts-mit-nginx-mariadb-php-lets-encrypt-redis-und-fail2ban/
24 # You have to customize this script (directories, users, etc.) for your actual environment.
25 # All entries which need to be customized are tagged with "TODO".
28 # Make sure the script exits when any command fails
34 if [ -z "$backupMainDir" ]; then
35 # TODO: The directory where you store the Nextcloud backups (when not specified by args)
36 backupMainDir
='/media/hdd/nextcloud_backup'
38 backupMainDir
=$
(echo $backupMainDir |
sed 's:/*$::')
41 currentDate
=$
(date +"%Y%m%d_%H%M%S")
43 # The actual directory of the current backup - this is a subdirectory of the main directory above with a timestamp
44 backupDir
="${backupMainDir}/${currentDate}"
46 # TODO: Use compression for file/data dir
47 # When this is the only script for backups, it's recommend to enable compression.
48 # If the output of this script is used in another (compressing) backup (e.g. borg backup),
49 # you should probably disable compression here and only enable compression of your main backup script.
52 # TOOD: The bare tar command for using compression.
53 # Use 'tar -cpzf' if you want to use gzip compression.
54 compressionCommand
="tar -I pigz -cpf"
56 # TODO: The directory of your Nextcloud installation (this is a directory under your web root)
57 nextcloudFileDir
='/var/www/nextcloud'
59 # TODO: The directory of your Nextcloud data directory (outside the Nextcloud file directory)
60 # 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
61 nextcloudDataDir
='/var/nextcloud_data'
63 # TODO: The directory of your Nextcloud's local external storage.
64 # Uncomment if you use local external storage.
65 #nextcloudLocalExternalDataDir='/var/nextcloud_external_data'
67 # TODO: The service name of the web server. Used to start/stop web server (e.g. 'systemctl start <webserverServiceName>')
68 webserverServiceName
='nginx'
70 # TODO: Your web server user
71 webserverUser
='www-data'
73 # TODO: The name of the database system (one of: mysql, mariadb, postgresql)
74 databaseSystem
='mariadb'
76 # TODO: Your Nextcloud database name
77 nextcloudDatabase
='nextcloud_db'
79 # TODO: Your Nextcloud database user
80 dbUser
='nextcloud_db_user'
82 # TODO: The password of the Nextcloud database user
83 dbPassword
='mYpAsSw0rd'
85 # TODO: The maximum number of backups to keep (when set to 0, all backups are kept)
88 # TODO: Ignore updater's backup directory in the data directory to save space
89 # Set to true to ignore the backup directory
90 ignoreUpdaterBackups
=false
92 # File names for backup files
93 # If you prefer other file names, you'll also have to change the NextcloudRestore.sh script.
94 fileNameBackupFileDir
='nextcloud-filedir.tar'
95 fileNameBackupDataDir
='nextcloud-datadir.tar'
97 if [ "$useCompression" = true
] ; then
98 fileNameBackupFileDir
='nextcloud-filedir.tar.gz'
99 fileNameBackupDataDir
='nextcloud-datadir.tar.gz'
102 fileNameBackupExternalDataDir
=''
104 if [ ! -z "${nextcloudLocalExternalDataDir+x}" ] ; then
105 fileNameBackupExternalDataDir
='nextcloud-external-datadir.tar'
107 if [ "$useCompression" = true
] ; then
108 fileNameBackupExternalDataDir
='nextcloud-external-datadir.tar.gz'
112 fileNameBackupDb
='nextcloud-db.sql'
114 # Function for error messages
115 errorecho
() { cat <<< "$@" 1>&2; }
117 function DisableMaintenanceMode
() {
118 echo "$(date +"%H
:%M
:%S
"): Switching off maintenance mode..."
119 sudo
-u "${webserverUser}" php
${nextcloudFileDir}/occ maintenance
:mode
--off
128 read -p "Backup cancelled. Keep maintenance mode? [y/n] " -n 1 -r
131 if ! [[ $REPLY =~ ^
[Yy
]$
]]
133 DisableMaintenanceMode
135 echo "Maintenance mode still enabled."
138 echo "Starting web server..."
139 systemctl start
"${webserverServiceName}"
149 echo "Backup directory: ${backupMainDir}"
154 if [ "$(id -u)" != "0" ]
156 errorecho
"ERROR: This script has to be run as root!"
161 # Check if backup dir already exists
163 if [ ! -d "${backupDir}" ]
165 mkdir
-p "${backupDir}"
167 errorecho
"ERROR: The backup directory ${backupDir} already exists!"
172 # Set maintenance mode
174 echo "$(date +"%H
:%M
:%S
"): Set maintenance mode for Nextcloud..."
175 sudo
-u "${webserverUser}" php
${nextcloudFileDir}/occ maintenance
:mode
--on
182 echo "$(date +"%H
:%M
:%S
"): Stopping web server..."
183 systemctl stop
"${webserverServiceName}"
188 # Backup file directory
190 echo "$(date +"%H
:%M
:%S
"): Creating backup of Nextcloud file directory..."
192 if [ "$useCompression" = true
] ; then
193 `$compressionCommand "${backupDir}/${fileNameBackupFileDir}" -C "${nextcloudFileDir}" .`
195 tar -cpf "${backupDir}/${fileNameBackupFileDir}" -C "${nextcloudFileDir}" .
202 # Backup data directory
204 echo "$(date +"%H
:%M
:%S
"): Creating backup of Nextcloud data directory..."
206 if [ "$ignoreUpdaterBackups" = true
] ; then
207 echo "Ignoring updater backup directory"
209 if [ "$useCompression" = true
] ; then
210 `$compressionCommand "${backupDir}/${fileNameBackupDataDir}" --exclude="updater-*/backups/*" -C "${nextcloudDataDir}" .`
212 tar -cpf "${backupDir}/${fileNameBackupDataDir}" --exclude="updater-*/backups/*" -C "${nextcloudDataDir}" .
215 if [ "$useCompression" = true
] ; then
216 `$compressionCommand "${backupDir}/${fileNameBackupDataDir}" -C "${nextcloudDataDir}" .`
218 tar -cpf "${backupDir}/${fileNameBackupDataDir}" -C "${nextcloudDataDir}" .
226 # Backup local external storage.
228 if [ ! -z "${nextcloudLocalExternalDataDir+x}" ] ; then
229 echo "$(date +"%H
:%M
:%S
"): Creating backup of Nextcloud local external storage directory..."
231 if [ "$useCompression" = true
] ; then
232 `$compressionCommand "${backupDir}/${fileNameBackupExternalDataDir}" -C "${nextcloudLocalExternalDataDir}" .`
234 tar -cpf "${backupDir}/${fileNameBackupExternalDataDir}" -C "${nextcloudLocalExternalDataDir}" .
244 if [ "${databaseSystem,,}" = "mysql" ] ||
[ "${databaseSystem,,}" = "mariadb" ]; then
245 echo "$(date +"%H
:%M
:%S
"): Backup Nextcloud database (MySQL/MariaDB)..."
247 if ! [ -x "$(command -v mysqldump)" ]; then
248 errorecho
"ERROR: MySQL/MariaDB not installed (command mysqldump not found)."
249 errorecho
"ERROR: No backup of database possible!"
251 mysqldump
--single-transaction -h localhost
-u "${dbUser}" -p"${dbPassword}" "${nextcloudDatabase}" > "${backupDir}/${fileNameBackupDb}"
256 elif [ "${databaseSystem,,}" = "postgresql" ] ||
[ "${databaseSystem,,}" = "pgsql" ]; then
257 echo "$(date +"%H
:%M
:%S
"): Backup Nextcloud database (PostgreSQL)..."
259 if ! [ -x "$(command -v pg_dump)" ]; then
260 errorecho
"ERROR: PostgreSQL not installed (command pg_dump not found)."
261 errorecho
"ERROR: No backup of database possible!"
263 PGPASSWORD
="${dbPassword}" pg_dump "${nextcloudDatabase}" -h localhost -U "${dbUser}" -f "${backupDir}/${fileNameBackupDb}"
273 echo "$(date +"%H
:%M
:%S
"): Starting web server..."
274 systemctl start
"${webserverServiceName}"
279 # Disable maintenance mode
281 DisableMaintenanceMode
286 if [ ${maxNrOfBackups} != 0 ]
288 nrOfBackups
=$
(ls -l ${backupMainDir} |
grep -c ^d
)
290 if [ ${nrOfBackups} -gt ${maxNrOfBackups} ]
292 echo "$(date +"%H
:%M
:%S
"): Removing old backups..."
293 ls -t ${backupMainDir} |
tail -$
(( nrOfBackups
- maxNrOfBackups
)) |
while read -r dirToRemove
; do
294 echo "${dirToRemove}"
295 rm -r "${backupMainDir}/${dirToRemove:?}"
304 echo "$(date +"%H
:%M
:%S
"): Backup created: ${backupDir}"