]>
git.p6c8.net - nextcloud-backup-restore.git/blob - NextcloudBackup.sh
76ca6765ecca487796e1bda720f906f56dbbe74f
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: The directory of your Nextcloud installation (this is a directory under your web root)
35 nextcloudFileDir
= '/var/www/nextcloud'
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'
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'
45 # TODO: The service name of the web server. Used to start/stop web server (e.g. 'systemctl start <webserverServiceName>')
46 webserverServiceName
= 'nginx'
48 # TODO: Your web server user
49 webserverUser
= 'www-data'
51 # TODO: The name of the database system (one of: mysql, mariadb, postgresql)
52 databaseSystem
= 'mariadb'
54 # TODO: Your Nextcloud database name
55 nextcloudDatabase
= 'nextcloud_db'
57 # TODO: Your Nextcloud database user
58 dbUser
= 'nextcloud_db_user'
60 # TODO: The password of the Nextcloud database user
61 dbPassword
= 'mYpAsSw0rd'
63 # TODO: The maximum number of backups to keep (when set to 0, all backups are kept)
66 # TODO: Ignore updater's backup directory in the data directory to save space
67 # Set to true to ignore the backup directory
68 ignoreUpdaterBackups
= false
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'
75 # TODO: Uncomment if you use local external storage
76 #fileNameBackupExternalDataDir='nextcloud-external-datadir.tar.gz'
78 fileNameBackupDb
= 'nextcloud-db.sql'
80 # Function for error messages
81 errorecho
() { cat <<< "$@" 1 >& 2 ; }
83 function DisableMaintenanceMode
() {
84 echo "Switching off maintenance mode..."
85 sudo
-u " ${webserverUser} " php
${nextcloudFileDir} / occ maintenance
: mode
--off
94 read -p "Backup cancelled. Keep maintenance mode? [y/n] " -n 1 -r
97 if ! [[ $REPLY = ~ ^
[ Yy
] $
]]
99 DisableMaintenanceMode
101 echo "Maintenance mode still enabled."
104 echo "Starting web server..."
105 systemctl start
" ${webserverServiceName} "
115 echo "Backup directory: ${backupMainDir} "
120 if [ "$(id -u)" != "0" ]
122 errorecho
"ERROR: This script has to be run as root!"
127 # Check if backup dir already exists
129 if [ ! -d " ${backupdir} " ]
131 mkdir
-p " ${backupdir} "
133 errorecho
"ERROR: The backup directory ${backupdir} already exists!"
138 # Set maintenance mode
140 echo "Set maintenance mode for Nextcloud..."
141 sudo
-u " ${webserverUser} " php
${nextcloudFileDir} / occ maintenance
: mode
--on
148 echo "Stopping web server..."
149 systemctl stop
" ${webserverServiceName} "
154 # Backup file directory
156 echo "Creating backup of Nextcloud file directory..."
157 tar -cpzf " ${backupdir}/${fileNameBackupFileDir}" -C "${nextcloudFileDir} " .
162 # Backup data directory
164 echo "Creating backup of Nextcloud data directory..."
166 if [ " $ignoreUpdaterBackups " = true
] ; then
167 echo "Ignoring updater backup directory"
168 tar -cpzf " ${backupdir}/${fileNameBackupDataDir}" --exclude="updater-*/backups/*" -C "${nextcloudDataDir} " .
170 tar -cpzf " ${backupdir}/${fileNameBackupDataDir}" -C "${nextcloudDataDir} " .
176 # Backup local external storage.
177 # Uncomment if you use local external storage
178 #echo "Creating backup of Nextcloud local external storage directory..."
179 #tar -cpzf "${backupdir}/${fileNameBackupExternalDataDir}" -C "${nextcloudLocalExternalDataDir}" .
186 if [ " ${databaseSystem,,} " = "mysql" ] ||
[ " ${databaseSystem,,} " = "mariadb" ]; then
187 echo "Backup Nextcloud database (MySQL/MariaDB)..."
189 if ! [ -x "$(command -v mysqldump)" ]; then
190 errorecho
"ERROR: MySQL/MariaDB not installed (command mysqldump not found)."
191 errorecho
"ERROR: No backup of database possible!"
193 mysqldump
--single-transaction -h localhost
-u " ${dbUser}" -p"${dbPassword}" "${nextcloudDatabase}" > "${backupdir}/${fileNameBackupDb} "
198 elif [ " ${databaseSystem,,} " = "postgresql" ]; then
199 echo "Backup Nextcloud database (PostgreSQL)..."
201 if ! [ -x "$(command -v pg_dump)" ]; then
202 errorecho
"ERROR: PostgreSQL not installed (command pg_dump not found)."
203 errorecho
"ERROR: No backup of database possible!"
205 PGPASSWORD
= " ${dbPassword}" pg_dump "${nextcloudDatabase}" -h localhost -U "${dbUser}" -f "${backupdir}/${fileNameBackupDb} "
215 echo "Starting web server..."
216 systemctl start
" ${webserverServiceName} "
221 # Disable maintenance mode
223 DisableMaintenanceMode
228 if [ ${maxNrOfBackups} != 0 ]
230 nrOfBackups
= $
( ls -l ${backupMainDir} |
grep -c ^d
)
232 if [[ ${nrOfBackups} > ${maxNrOfBackups} ]]
234 echo "Removing old backups..."
235 ls -t ${backupMainDir} |
tail - $
(( nrOfBackups
- maxNrOfBackups
)) |
while read -r dirToRemove
; do
236 echo " ${dirToRemove} "
237 rm -r " ${backupMainDir} / ${dirToRemove:?} "
246 echo "Backup created: ${backupdir} "
patrick-canterino.de