]>
git.p6c8.net - nextcloud-backup-restore.git/blob - NextcloudBackup.sh
6781eafc947a265569a203bb59f32a6921f211a9
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 # File names for backup files
69 # If you prefer other file names, you'll also have to change the NextcloudRestore.sh script.
70 fileNameBackupFileDir
= 'nextcloud-filedir.tar.gz'
71 fileNameBackupDataDir
= 'nextcloud-datadir.tar.gz'
73 # TODO: Uncomment if you use local external storage
74 #fileNameBackupExternalDataDir='nextcloud-external-datadir.tar.gz'
76 fileNameBackupDb
= 'nextcloud-db.sql'
78 # Function for error messages
79 errorecho
() { cat <<< "$@" 1 >& 2 ; }
81 function DisableMaintenanceMode
() {
82 echo "Switching off maintenance mode..."
83 sudo
-u " ${webserverUser} " php
${nextcloudFileDir} / occ maintenance
: mode
--off
92 read -p "Backup cancelled. Keep maintenance mode? [y/n] " -n 1 -r
95 if ! [[ $REPLY = ~ ^
[ Yy
] $
]]
97 DisableMaintenanceMode
99 echo "Maintenance mode still enabled."
108 if [ "$(id -u)" != "0" ]
110 errorecho
"ERROR: This script has to be run as root!"
115 # Check if backup dir already exists
117 if [ ! -d " ${backupdir} " ]
119 mkdir
-p " ${backupdir} "
121 errorecho
"ERROR: The backup directory ${backupdir} already exists!"
126 # Set maintenance mode
128 echo "Set maintenance mode for Nextcloud..."
129 sudo
-u " ${webserverUser} " php
${nextcloudFileDir} / occ maintenance
: mode
--on
136 echo "Stopping web server..."
137 systemctl stop
" ${webserverServiceName} "
142 # Backup file directory
144 echo "Creating backup of Nextcloud file directory..."
145 tar -cpzf " ${backupdir}/${fileNameBackupFileDir}" -C "${nextcloudFileDir} " .
150 # Backup data directory
152 echo "Creating backup of Nextcloud data directory..."
153 tar -cpzf " ${backupdir}/${fileNameBackupDataDir}" -C "${nextcloudDataDir} " .
157 # Backup local external storage.
158 # Uncomment if you use local external storage
159 #echo "Creating backup of Nextcloud local external storage directory..."
160 #tar -cpzf "${backupdir}/${fileNameBackupExternalDataDir}" -C "${nextcloudLocalExternalDataDir}" .
167 if [ " ${databaseSystem,,} " = "mysql" ] ||
[ " ${databaseSystem,,} " = "mariadb" ]; then
168 echo "Backup Nextcloud database (MySQL/MariaDB)..."
170 if ! [ -x "$(command -v mysqldump)" ]; then
171 errorecho
"ERROR: MySQL/MariaDB not installed (command mysqldump not found)."
172 errorecho
"ERROR: No backup of database possible!"
174 mysqldump
--single-transaction -h localhost
-u " ${dbUser}" -p"${dbPassword}" "${nextcloudDatabase}" > "${backupdir}/${fileNameBackupDb} "
179 elif [ " ${databaseSystem,,} " = "postgresql" ]; then
180 echo "Backup Nextcloud database (PostgreSQL)..."
182 if ! [ -x "$(command -v pg_dump)" ]; then
183 errorecho
"ERROR:PostgreSQL not installed (command pg_dump not found)."
184 errorecho
"ERROR: No backup of database possible!"
186 PGPASSWORD
= " ${dbPassword}" pg_dump "${nextcloudDatabase}" -h localhost -U "${dbUser}" -f "${backupdir}/${fileNameBackupDb} "
196 echo "Starting web server..."
197 systemctl start
" ${webserverServiceName} "
202 # Disable maintenance mode
204 DisableMaintenanceMode
209 if [ ${maxNrOfBackups} != 0 ]
211 nrOfBackups
= $
( ls -l ${backupMainDir} |
grep -c ^d
)
213 if [[ ${nrOfBackups} > ${maxNrOfBackups} ]]
215 echo "Removing old backups..."
216 ls -t ${backupMainDir} |
tail - $
(( nrOfBackups
- maxNrOfBackups
)) |
while read -r dirToRemove
; do
217 echo " ${dirToRemove} "
218 rm -r " ${backupMainDir} / ${dirToRemove:?} "
227 echo "Backup created: ${backupdir} "
patrick-canterino.de