]>
git.p6c8.net - nextcloud-backup-restore.git/blob - NextcloudBackup.sh
d1b1ed2a403866ff2172e619a7746eee8c30446c
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 (ome 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 safe 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."
110 echo "Backup directory: ${backupMainDir} "
115 if [ "$(id -u)" != "0" ]
117 errorecho
"ERROR: This script has to be run as root!"
122 # Check if backup dir already exists
124 if [ ! -d " ${backupdir} " ]
126 mkdir
-p " ${backupdir} "
128 errorecho
"ERROR: The backup directory ${backupdir} already exists!"
133 # Set maintenance mode
135 echo "Set maintenance mode for Nextcloud..."
136 sudo
-u " ${webserverUser} " php
${nextcloudFileDir} / occ maintenance
: mode
--on
143 echo "Stopping web server..."
144 systemctl stop
" ${webserverServiceName} "
149 # Backup file directory
151 echo "Creating backup of Nextcloud file directory..."
152 tar -cpzf " ${backupdir}/${fileNameBackupFileDir}" -C "${nextcloudFileDir} " .
157 # Backup data directory
159 echo "Creating backup of Nextcloud data directory..."
161 if [ " $ignoreUpdaterBackups " = true
] ; then
162 echo "Ignoring updater backup directory"
163 tar -cpzf " ${backupdir}/${fileNameBackupDataDir}" --exclude="updater-*/backups/*" -C "${nextcloudDataDir} " .
165 tar -cpzf " ${backupdir}/${fileNameBackupDataDir}" -C "${nextcloudDataDir} " .
171 # Backup local external storage.
172 # Uncomment if you use local external storage
173 #echo "Creating backup of Nextcloud local external storage directory..."
174 #tar -cpzf "${backupdir}/${fileNameBackupExternalDataDir}" -C "${nextcloudLocalExternalDataDir}" .
181 if [ " ${databaseSystem,,} " = "mysql" ] ||
[ " ${databaseSystem,,} " = "mariadb" ]; then
182 echo "Backup Nextcloud database (MySQL/MariaDB)..."
184 if ! [ -x "$(command -v mysqldump)" ]; then
185 errorecho
"ERROR: MySQL/MariaDB not installed (command mysqldump not found)."
186 errorecho
"ERROR: No backup of database possible!"
188 mysqldump
--single-transaction -h localhost
-u " ${dbUser}" -p"${dbPassword}" "${nextcloudDatabase}" > "${backupdir}/${fileNameBackupDb} "
193 elif [ " ${databaseSystem,,} " = "postgresql" ]; then
194 echo "Backup Nextcloud database (PostgreSQL)..."
196 if ! [ -x "$(command -v pg_dump)" ]; then
197 errorecho
"ERROR:PostgreSQL not installed (command pg_dump not found)."
198 errorecho
"ERROR: No backup of database possible!"
200 PGPASSWORD
= " ${dbPassword}" pg_dump "${nextcloudDatabase}" -h localhost -U "${dbUser}" -f "${backupdir}/${fileNameBackupDb} "
210 echo "Starting web server..."
211 systemctl start
" ${webserverServiceName} "
216 # Disable maintenance mode
218 DisableMaintenanceMode
223 if [ ${maxNrOfBackups} != 0 ]
225 nrOfBackups
= $
( ls -l ${backupMainDir} |
grep -c ^d
)
227 if [[ ${nrOfBackups} > ${maxNrOfBackups} ]]
229 echo "Removing old backups..."
230 ls -t ${backupMainDir} |
tail - $
(( nrOfBackups
- maxNrOfBackups
)) |
while read -r dirToRemove
; do
231 echo " ${dirToRemove} "
232 rm -r " ${backupMainDir} / ${dirToRemove:?} "
241 echo "Backup created: ${backupdir} "
patrick-canterino.de