]>
git.p6c8.net - nextcloud-backup-restore.git/blob - NextcloudBackup.sh
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'
29 currentDate
= $
( date + "%Y%m%d_%H%M%S" )
31 # The actual directory of the current backup - this is a subdirectory of the main directory above with a timestamp
32 backupdir
= " ${backupMainDir} / ${currentDate} /"
34 # TODO: Use compression for Matrix Synapse installation/lib dir
35 # When this is the only script for backups, it's recommend to enable compression.
36 # If the output of this script is used in another (compressing) backup (e.g. borg backup),
37 # you should probably disable compression here and only enable compression of your main backup script.
40 # TODO: The directory of your Nextcloud installation (this is a directory under your web root)
41 nextcloudFileDir
= '/var/www/nextcloud'
43 # TODO: The directory of your Nextcloud data directory (outside the Nextcloud file directory)
44 # 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
45 nextcloudDataDir
= '/var/nextcloud_data'
47 # TODO: The directory of your Nextcloud's local external storage.
48 # Uncomment if you use local external storage.
49 #nextcloudLocalExternalDataDir='/var/nextcloud_external_data'
51 # TODO: The service name of the web server. Used to start/stop web server (e.g. 'systemctl start <webserverServiceName>')
52 webserverServiceName
= 'nginx'
54 # TODO: Your web server user
55 webserverUser
= 'www-data'
57 # TODO: The name of the database system (one of: mysql, mariadb, postgresql)
58 databaseSystem
= 'mariadb'
60 # TODO: Your Nextcloud database name
61 nextcloudDatabase
= 'nextcloud_db'
63 # TODO: Your Nextcloud database user
64 dbUser
= 'nextcloud_db_user'
66 # TODO: The password of the Nextcloud database user
67 dbPassword
= 'mYpAsSw0rd'
69 # TODO: The maximum number of backups to keep (when set to 0, all backups are kept)
72 # TODO: Ignore updater's backup directory in the data directory to save space
73 # Set to true to ignore the backup directory
74 ignoreUpdaterBackups
= false
76 # File names for backup files
77 # If you prefer other file names, you'll also have to change the NextcloudRestore.sh script.
78 fileNameBackupFileDir
= 'nextcloud-filedir.tar'
79 fileNameBackupDataDir
= 'nextcloud-datadir.tar'
81 if [ " $useCompression " = true
] ; then
82 fileNameBackupFileDir
= 'nextcloud-filedir.tar.gz'
83 fileNameBackupDataDir
= 'nextcloud-datadir.tar.gz'
86 # TODO: Uncomment if you use local external storage
87 #fileNameBackupExternalDataDir='nextcloud-external-datadir.tar'
89 #if [ "$useCompression" = true ] ; then
90 # fileNameBackupExternalDataDir='nextcloud-external-datadir.tar.gz'
93 fileNameBackupDb
= 'nextcloud-db.sql'
95 # Function for error messages
96 errorecho
() { cat <<< "$@" 1 >& 2 ; }
98 function DisableMaintenanceMode
() {
99 echo "Switching off maintenance mode..."
100 sudo
-u " ${webserverUser} " php
${nextcloudFileDir} / occ maintenance
: mode
--off
109 read -p "Backup cancelled. Keep maintenance mode? [y/n] " -n 1 -r
112 if ! [[ $REPLY = ~ ^
[ Yy
] $
]]
114 DisableMaintenanceMode
116 echo "Maintenance mode still enabled."
119 echo "Starting web server..."
120 systemctl start
" ${webserverServiceName} "
130 echo "Backup directory: ${backupMainDir} "
135 if [ "$(id -u)" != "0" ]
137 errorecho
"ERROR: This script has to be run as root!"
142 # Check if backup dir already exists
144 if [ ! -d " ${backupdir} " ]
146 mkdir
-p " ${backupdir} "
148 errorecho
"ERROR: The backup directory ${backupdir} already exists!"
153 # Set maintenance mode
155 echo "Set maintenance mode for Nextcloud..."
156 sudo
-u " ${webserverUser} " php
${nextcloudFileDir} / occ maintenance
: mode
--on
163 echo "Stopping web server..."
164 systemctl stop
" ${webserverServiceName} "
169 # Backup file directory
171 echo "Creating backup of Nextcloud file directory..."
173 if [ " $useCompression " = true
] ; then
174 tar -cpzf " ${backupdir}/${fileNameBackupFileDir}" -C "${nextcloudFileDir} " .
176 tar -cpf " ${backupdir}/${fileNameBackupFileDir}" -C "${nextcloudFileDir} " .
183 # Backup data directory
185 echo "Creating backup of Nextcloud data directory..."
187 if [ " $ignoreUpdaterBackups " = true
] ; then
188 echo "Ignoring updater backup directory"
190 if [ " $useCompression " = true
] ; then
191 tar -cpzf " ${backupdir}/${fileNameBackupDataDir}" --exclude="updater-*/backups/*" -C "${nextcloudDataDir} " .
193 tar -cpf " ${backupdir}/${fileNameBackupDataDir}" --exclude="updater-*/backups/*" -C "${nextcloudDataDir} " .
196 if [ " $useCompression " = true
] ; then
197 tar -cpzf " ${backupdir}/${fileNameBackupDataDir}" -C "${nextcloudDataDir} " .
199 tar -cpf " ${backupdir}/${fileNameBackupDataDir}" -C "${nextcloudDataDir} " .
206 # Backup local external storage.
207 # Uncomment if you use local external storage
208 #echo "Creating backup of Nextcloud local external storage directory..."
209 #if [ "$useCompression" = true ] ; then
210 # tar -cpzf "${backupdir}/${fileNameBackupExternalDataDir}" -C "${nextcloudLocalExternalDataDir}" .
212 # tar -cpf "${backupdir}/${fileNameBackupExternalDataDir}" -C "${nextcloudLocalExternalDataDir}" .
221 if [ " ${databaseSystem,,} " = "mysql" ] ||
[ " ${databaseSystem,,} " = "mariadb" ]; then
222 echo "Backup Nextcloud database (MySQL/MariaDB)..."
224 if ! [ -x "$(command -v mysqldump)" ]; then
225 errorecho
"ERROR: MySQL/MariaDB not installed (command mysqldump not found)."
226 errorecho
"ERROR: No backup of database possible!"
228 mysqldump
--single-transaction -h localhost
-u " ${dbUser}" -p"${dbPassword}" "${nextcloudDatabase}" > "${backupdir}/${fileNameBackupDb} "
233 elif [ " ${databaseSystem,,} " = "postgresql" ] ||
[ " ${databaseSystem,,} " = "pgsql" ]; then
234 echo "Backup Nextcloud database (PostgreSQL)..."
236 if ! [ -x "$(command -v pg_dump)" ]; then
237 errorecho
"ERROR: PostgreSQL not installed (command pg_dump not found)."
238 errorecho
"ERROR: No backup of database possible!"
240 PGPASSWORD
= " ${dbPassword}" pg_dump "${nextcloudDatabase}" -h localhost -U "${dbUser}" -f "${backupdir}/${fileNameBackupDb} "
250 echo "Starting web server..."
251 systemctl start
" ${webserverServiceName} "
256 # Disable maintenance mode
258 DisableMaintenanceMode
263 if [ ${maxNrOfBackups} != 0 ]
265 nrOfBackups
= $
( ls -l ${backupMainDir} |
grep -c ^d
)
267 if [[ ${nrOfBackups} > ${maxNrOfBackups} ]]
269 echo "Removing old backups..."
270 ls -t ${backupMainDir} |
tail - $
(( nrOfBackups
- maxNrOfBackups
)) |
while read -r dirToRemove
; do
271 echo " ${dirToRemove} "
272 rm -r " ${backupMainDir} / ${dirToRemove:?} "
281 echo "Backup created: ${backupdir} "
patrick-canterino.de