]>
git.p6c8.net - nextcloud-backup-restore.git/blob - NextcloudBackup.sh
aeb31fb3c185f20870ce31cbbb550891a4c4d030
4 # Bash script for creating backups of Nextcloud.
6 # - With backup directory specified in the script: ./NextcloudBackup.sh
7 # - With backup directory specified by parameter: ./NextcloudBackup.sh <BackupDirectory> (e.g. ./NextcloudBackup.sh /media/hdd/nextcloud_backup)
9 # 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/
14 # You have to customize this script (directories, users, etc.) for your actual environment.
15 # All entries which need to be customized are tagged with "TODO".
21 if [ -z " $backupMainDir " ]; then
22 # TODO: The directory where you store the Nextcloud backups (when not specified by args)
23 backupMainDir
= "/media/hdd/nextcloud_backup"
26 echo "Backup directory: $backupMainDir "
28 currentDate
= $
( date + "%Y%m%d_%H%M%S" )
30 # The actual directory of the current backup - this is a subdirectory of the main directory above with a timestamp
31 backupdir
= " ${backupMainDir} / ${currentDate} /"
33 # TODO: The directory of your Nextcloud installation (this is a directory under your web root)
34 nextcloudFileDir
= "/var/www/nextcloud"
36 # TODO: The directory of your Nextcloud data directory (outside the Nextcloud file directory)
37 # 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
38 nextcloudDataDir
= "/var/nextcloud_data"
40 # TODO: The directory of your Nextcloud's local external storage.
41 # Uncomment if you use local external storage.
42 #nextcloudLocalExternalDataDir="/var/nextcloud_external_data"
44 # TODO: The service name of the web server. Used to start/stop web server (e.g. 'systemctl start <webserverServiceName>')
45 webserverServiceName
= "nginx"
47 # TODO: Your Nextcloud database name
48 nextcloudDatabase
= "nextcloud_db"
50 # TODO: Your Nextcloud database user
51 dbUser
= "nextcloud_db_user"
53 # TODO: The password of the Nextcloud database user
54 dbPassword
= "mYpAsSw0rd"
56 # TODO: Your web server user
57 webserverUser
= "www-data"
59 # TODO: The maximum number of backups to keep (when set to 0, all backups are kept)
62 # File names for backup files
63 # If you prefer other file names, you'll also have to change the NextcloudRestore.sh script.
64 fileNameBackupFileDir
= "nextcloud-filedir.tar.gz"
65 fileNameBackupDataDir
= "nextcloud-datadir.tar.gz"
67 # TOOD: Uncomment if you use local external storage
68 #fileNameBackupExternalDataDir="nextcloud-external-datadir.tar.gz"
70 fileNameBackupDb
= "nextcloud-db.sql"
72 # Function for error messages
73 errorecho
() { cat <<< "$@" 1 >& 2 ; }
75 function DisableMaintenanceMode
() {
76 echo "Switching off maintenance mode..."
77 sudo
-u " ${webserverUser} " php
${nextcloudFileDir} / occ maintenance
: mode
--off
86 read -p "Backup cancelled. Keep maintenance mode? [y/n] " -n 1 -r
89 if ! [[ $REPLY = ~ ^
[ Yy
] $
]]
91 DisableMaintenanceMode
93 echo "Maintenance mode still enabled."
102 if [ "$(id -u)" != "0" ]
104 errorecho
"ERROR: This script has to be run as root!"
109 # Check if backup dir already exists
111 if [ ! -d " ${backupdir} " ]
113 mkdir
-p " ${backupdir} "
115 errorecho
"ERROR: The backup directory ${backupdir} already exists!"
120 # Set maintenance mode
122 echo "Set maintenance mode for Nextcloud..."
123 sudo
-u " ${webserverUser} " php
${nextcloudFileDir} / occ maintenance
: mode
--on
130 echo "Stopping web server..."
131 systemctl stop
" ${webserverServiceName} "
136 # Backup file directory
138 echo "Creating backup of Nextcloud file directory..."
139 tar -cpzf " ${backupdir}/${fileNameBackupFileDir}" -C "${nextcloudFileDir} " .
144 # Backup data directory
146 echo "Creating backup of Nextcloud data directory..."
147 tar -cpzf " ${backupdir}/${fileNameBackupDataDir}" -C "${nextcloudDataDir} " .
151 # Backup local external storage.
152 # Uncomment if you use local external storage
153 #echo "Creating backup of Nextcloud local external storage directory..."
154 #tar -cpzf "${backupdir}/${fileNameBackupExternalDataDir}" -C "${nextcloudLocalExternalDataDir}" .
161 echo "Backup Nextcloud database..."
163 mysqldump
--single-transaction -h localhost
-u " ${dbUser}" -p"${dbPassword}" "${nextcloudDatabase}" > "${backupdir}/${fileNameBackupDb} "
165 # PostgreSQL (uncomment if you are using PostgreSQL as Nextcloud database)
166 #PGPASSWORD="${dbPassword}" pg_dump "${nextcloudDatabase}" -h localhost -U "${dbUser}" -f "${backupdir}/${fileNameBackupDb}"
173 echo "Starting web server..."
174 systemctl start
" ${webserverServiceName} "
179 # Disable maintenance mode
181 DisableMaintenanceMode
186 if (( ${maxNrOfBackups} != 0 ))
188 nrOfBackups
= $
( ls -l ${backupMainDir} |
grep -c ^d
)
190 if (( ${nrOfBackups} > ${maxNrOfBackups} ))
192 echo "Removing old backups..."
193 ls -t ${backupMainDir} |
tail - $
(( nrOfBackups
- maxNrOfBackups
)) |
while read dirToRemove
; do
194 echo " ${dirToRemove} "
195 rm -r ${backupMainDir} / ${dirToRemove}
204 echo "Backup created: ${backupdir} "
patrick-canterino.de