]>
git.p6c8.net - nextcloud-backup-restore.git/blob - NextcloudBackup.sh
ce9601a84107921fa33ebb799f600669e8e27435
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 echo "Backup directory: $backupMainDir "
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: The directory of your Nextcloud installation (this is a directory under your web root)
37 nextcloudFileDir
= '/var/www/nextcloud'
39 # TODO: The directory of your Nextcloud data directory (outside the Nextcloud file directory)
40 # 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
41 nextcloudDataDir
= '/var/nextcloud_data'
43 # TODO: The directory of your Nextcloud's local external storage.
44 # Uncomment if you use local external storage.
45 #nextcloudLocalExternalDataDir='/var/nextcloud_external_data'
47 # TODO: The service name of the web server. Used to start/stop web server (e.g. 'systemctl start <webserverServiceName>')
48 webserverServiceName
= 'nginx'
50 # TODO: Your web server user
51 webserverUser
= 'www-data'
53 # TODO: The name of the database system (ome of: mysql, mariadb, postgresql)
54 databaseSystem
= 'mariadb'
56 # TODO: Your Nextcloud database name
57 nextcloudDatabase
= 'nextcloud_db'
59 # TODO: Your Nextcloud database user
60 dbUser
= 'nextcloud_db_user'
62 # TODO: The password of the Nextcloud database user
63 dbPassword
= 'mYpAsSw0rd'
65 # TODO: The maximum number of backups to keep (when set to 0, all backups are kept)
68 # TODO: Ignore updater's backup directory in the data directory to safe space
69 # Set to true to ignore the backup directory
70 ignoreUpdaterBackups
= false
72 # File names for backup files
73 # If you prefer other file names, you'll also have to change the NextcloudRestore.sh script.
74 fileNameBackupFileDir
= 'nextcloud-filedir.tar.gz'
75 fileNameBackupDataDir
= 'nextcloud-datadir.tar.gz'
77 # TODO: Uncomment if you use local external storage
78 #fileNameBackupExternalDataDir='nextcloud-external-datadir.tar.gz'
80 fileNameBackupDb
= 'nextcloud-db.sql'
82 # Function for error messages
83 errorecho
() { cat <<< "$@" 1 >& 2 ; }
85 function DisableMaintenanceMode
() {
86 echo "Switching off maintenance mode..."
87 sudo
-u " ${webserverUser} " php
${nextcloudFileDir} / occ maintenance
: mode
--off
96 read -p "Backup cancelled. Keep maintenance mode? [y/n] " -n 1 -r
99 if ! [[ $REPLY = ~ ^
[ Yy
] $
]]
101 DisableMaintenanceMode
103 echo "Maintenance mode still enabled."
112 if [ "$(id -u)" != "0" ]
114 errorecho
"ERROR: This script has to be run as root!"
119 # Check if backup dir already exists
121 if [ ! -d " ${backupdir} " ]
123 mkdir
-p " ${backupdir} "
125 errorecho
"ERROR: The backup directory ${backupdir} already exists!"
130 # Set maintenance mode
132 echo "Set maintenance mode for Nextcloud..."
133 sudo
-u " ${webserverUser} " php
${nextcloudFileDir} / occ maintenance
: mode
--on
140 echo "Stopping web server..."
141 systemctl stop
" ${webserverServiceName} "
146 # Backup file directory
148 echo "Creating backup of Nextcloud file directory..."
149 tar -cpzf " ${backupdir}/${fileNameBackupFileDir}" -C "${nextcloudFileDir} " .
154 # Backup data directory
156 echo "Creating backup of Nextcloud data directory..."
158 if [ " $ignoreUpdaterBackups " = true
] ; then
159 echo "Ignoring updater backup directory"
160 tar -cpzf " ${backupdir}/${fileNameBackupDataDir}" --exclude="updater-*/backups/*" -C "${nextcloudDataDir} " .
162 tar -cpzf " ${backupdir}/${fileNameBackupDataDir}" -C "${nextcloudDataDir} " .
168 # Backup local external storage.
169 # Uncomment if you use local external storage
170 #echo "Creating backup of Nextcloud local external storage directory..."
171 #tar -cpzf "${backupdir}/${fileNameBackupExternalDataDir}" -C "${nextcloudLocalExternalDataDir}" .
178 if [ " ${databaseSystem,,} " = "mysql" ] ||
[ " ${databaseSystem,,} " = "mariadb" ]; then
179 echo "Backup Nextcloud database (MySQL/MariaDB)..."
181 if ! [ -x "$(command -v mysqldump)" ]; then
182 errorecho
"ERROR: MySQL/MariaDB not installed (command mysqldump not found)."
183 errorecho
"ERROR: No backup of database possible!"
185 mysqldump
--single-transaction -h localhost
-u " ${dbUser}" -p"${dbPassword}" "${nextcloudDatabase}" > "${backupdir}/${fileNameBackupDb} "
190 elif [ " ${databaseSystem,,} " = "postgresql" ]; then
191 echo "Backup Nextcloud database (PostgreSQL)..."
193 if ! [ -x "$(command -v pg_dump)" ]; then
194 errorecho
"ERROR:PostgreSQL not installed (command pg_dump not found)."
195 errorecho
"ERROR: No backup of database possible!"
197 PGPASSWORD
= " ${dbPassword}" pg_dump "${nextcloudDatabase}" -h localhost -U "${dbUser}" -f "${backupdir}/${fileNameBackupDb} "
207 echo "Starting web server..."
208 systemctl start
" ${webserverServiceName} "
213 # Disable maintenance mode
215 DisableMaintenanceMode
220 if [ ${maxNrOfBackups} != 0 ]
222 nrOfBackups
= $
( ls -l ${backupMainDir} |
grep -c ^d
)
224 if [[ ${nrOfBackups} > ${maxNrOfBackups} ]]
226 echo "Removing old backups..."
227 ls -t ${backupMainDir} |
tail - $
(( nrOfBackups
- maxNrOfBackups
)) |
while read -r dirToRemove
; do
228 echo " ${dirToRemove} "
229 rm -r " ${backupMainDir} / ${dirToRemove:?} "
238 echo "Backup created: ${backupdir} "
patrick-canterino.de