]>
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'
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 # TODO: Uncomment if you use local external storage
89 #fileNameBackupExternalDataDir='nextcloud-external-datadir.tar'
91 #if [ "$useCompression" = true ] ; then
92 # fileNameBackupExternalDataDir='nextcloud-external-datadir.tar.gz'
95 fileNameBackupDb
= 'nextcloud-db.sql'
97 # Function for error messages
98 errorecho
() { cat <<< "$@" 1 >& 2 ; }
100 function DisableMaintenanceMode
() {
101 echo "Switching off maintenance mode..."
102 sudo
-u " ${webserverUser} " php
${nextcloudFileDir} / occ maintenance
: mode
--off
111 read -p "Backup cancelled. Keep maintenance mode? [y/n] " -n 1 -r
114 if ! [[ $REPLY = ~ ^
[ Yy
] $
]]
116 DisableMaintenanceMode
118 echo "Maintenance mode still enabled."
121 echo "Starting web server..."
122 systemctl start
" ${webserverServiceName} "
132 echo "Backup directory: ${backupMainDir} "
137 if [ "$(id -u)" != "0" ]
139 errorecho
"ERROR: This script has to be run as root!"
144 # Check if backup dir already exists
146 if [ ! -d " ${backupdir} " ]
148 mkdir
-p " ${backupdir} "
150 errorecho
"ERROR: The backup directory ${backupdir} already exists!"
155 # Set maintenance mode
157 echo "Set maintenance mode for Nextcloud..."
158 sudo
-u " ${webserverUser} " php
${nextcloudFileDir} / occ maintenance
: mode
--on
165 echo "Stopping web server..."
166 systemctl stop
" ${webserverServiceName} "
171 # Backup file directory
173 echo "Creating backup of Nextcloud file directory..."
175 if [ " $useCompression " = true
] ; then
176 tar -I pigz
-cpf " ${backupdir}/${fileNameBackupFileDir}" -C "${nextcloudFileDir} " .
178 tar -cpf " ${backupdir}/${fileNameBackupFileDir}" -C "${nextcloudFileDir} " .
185 # Backup data directory
187 echo "Creating backup of Nextcloud data directory..."
189 if [ " $ignoreUpdaterBackups " = true
] ; then
190 echo "Ignoring updater backup directory"
192 if [ " $useCompression " = true
] ; then
193 tar -I pigz
-cpf " ${backupdir}/${fileNameBackupDataDir}" --exclude="updater-*/backups/*" -C "${nextcloudDataDir} " .
195 tar -cpf " ${backupdir}/${fileNameBackupDataDir}" --exclude="updater-*/backups/*" -C "${nextcloudDataDir} " .
198 if [ " $useCompression " = true
] ; then
199 tar -I pigz
-cpf " ${backupdir}/${fileNameBackupDataDir}" -C "${nextcloudDataDir} " .
201 tar -cpf " ${backupdir}/${fileNameBackupDataDir}" -C "${nextcloudDataDir} " .
208 # Backup local external storage.
209 # Uncomment if you use local external storage
210 #echo "Creating backup of Nextcloud local external storage directory..."
212 #if [ "$useCompression" = true ] ; then
213 # tar -I pigz -cpf "${backupdir}/${fileNameBackupExternalDataDir}" -C "${nextcloudLocalExternalDataDir}" .
215 # tar -cpf "${backupdir}/${fileNameBackupExternalDataDir}" -C "${nextcloudLocalExternalDataDir}" .
224 if [ " ${databaseSystem,,} " = "mysql" ] ||
[ " ${databaseSystem,,} " = "mariadb" ]; then
225 echo "Backup Nextcloud database (MySQL/MariaDB)..."
227 if ! [ -x "$(command -v mysqldump)" ]; then
228 errorecho
"ERROR: MySQL/MariaDB not installed (command mysqldump not found)."
229 errorecho
"ERROR: No backup of database possible!"
231 mysqldump
--single-transaction -h localhost
-u " ${dbUser}" -p"${dbPassword}" "${nextcloudDatabase}" > "${backupdir}/${fileNameBackupDb} "
236 elif [ " ${databaseSystem,,} " = "postgresql" ] ||
[ " ${databaseSystem,,} " = "pgsql" ]; then
237 echo "Backup Nextcloud database (PostgreSQL)..."
239 if ! [ -x "$(command -v pg_dump)" ]; then
240 errorecho
"ERROR: PostgreSQL not installed (command pg_dump not found)."
241 errorecho
"ERROR: No backup of database possible!"
243 PGPASSWORD
= " ${dbPassword}" pg_dump "${nextcloudDatabase}" -h localhost -U "${dbUser}" -f "${backupdir}/${fileNameBackupDb} "
253 echo "Starting web server..."
254 systemctl start
" ${webserverServiceName} "
259 # Disable maintenance mode
261 DisableMaintenanceMode
266 if [ ${maxNrOfBackups} != 0 ]
268 nrOfBackups
= $
( ls -l ${backupMainDir} |
grep -c ^d
)
270 if [ ${nrOfBackups} -gt ${maxNrOfBackups} ]
272 echo "Removing old backups..."
273 ls -t ${backupMainDir} |
tail - $
(( nrOfBackups
- maxNrOfBackups
)) |
while read -r dirToRemove
; do
274 echo " ${dirToRemove} "
275 rm -r " ${backupMainDir} / ${dirToRemove:?} "
284 echo "Backup created: ${backupdir} "
patrick-canterino.de