]>
git.p6c8.net - nextcloud-backup-restore.git/blob - NextcloudBackup.sh
4c95f10ca6a8d92fb77601a8270a214f44dac141
4 # Bash script for creating backups of Nextcloud.
5 # Usage: ./NextcloudBackup.sh
7 # 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/
12 # You have to customize this script (directories, users, etc.) for your actual environment.
13 # All entries which need to be customized are tagged with "TODO".
19 if [ -z " $backupMainDir " ]; then
20 # TODO: The directory where you store the Nextcloud backups (when not specified by args)
21 backupMainDir
= "/mnt/Share/NextcloudBackups"
24 echo "Backup directory: $backupMainDir "
26 currentDate
= $
( date + "%Y%m%d_%H%M%S" )
27 # The actual directory of the current backup - this is a subdirectory of the main directory above with a timestamp
28 backupdir
= " ${backupMainDir} / ${currentDate} /"
29 # TODO: The directory of your Nextcloud installation (this is a directory under your web root)
30 nextcloudFileDir
= "/var/www/nextcloud"
31 # TODO: The directory of your Nextcloud data directory (outside the Nextcloud file directory)
32 # 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
33 nextcloudDataDir
= "/var/nextcloud_data"
34 # TODO: The service name of the web server. Used to start/stop web server (e.g. 'systemctl start <webserverServiceName>')
35 webserverServiceName
= "nginx"
36 # TODO: Your Nextcloud database name
37 nextcloudDatabase
= "nextcloud_db"
38 # TODO: Your Nextcloud database user
39 dbUser
= "nextcloud_db_user"
40 # TODO: The password of the Nextcloud database user
41 dbPassword
= "mYpAsSw0rd"
42 # TODO: Your web server user
43 webserverUser
= "www-data"
44 # TODO: The maximum number of backups to keep (when set to 0, all backups are kept)
47 # File names for backup files
48 # If you prefer other file names, you'll also have to change the NextcloudRestore.sh script.
49 fileNameBackupFileDir
= "nextcloud-filedir.tar.gz"
50 fileNameBackupDataDir
= "nextcloud-datadir.tar.gz"
51 fileNameBackupDb
= "nextcloud-db.sql"
53 # Function for error messages
54 errorecho
() { cat <<< "$@" 1 >& 2 ; }
56 function DisableMaintenanceMode
() {
57 echo "Switching off maintenance mode..."
58 sudo
-u " ${webserverUser} " php
${nextcloudFileDir} / occ maintenance
: mode
--off
67 read -p "Backup cancelled. Keep maintenance mode? [y/n] " -n 1 -r
70 if ! [[ $REPLY = ~ ^
[ Yy
] $
]]
72 DisableMaintenanceMode
74 echo "Maintenance mode still enabled."
83 if [ "$(id -u)" != "0" ]
85 errorecho
"ERROR: This script has to be run as root!"
90 # Check if backup dir already exists
92 if [ ! -d " ${backupdir} " ]
94 mkdir
-p " ${backupdir} "
96 errorecho
"ERROR: The backup directory ${backupdir} already exists!"
101 # Set maintenance mode
103 echo "Set maintenance mode for Nextcloud..."
104 sudo
-u " ${webserverUser} " php
${nextcloudFileDir} / occ maintenance
: mode
--on
111 echo "Stopping web server..."
112 systemctl stop
" ${webserverServiceName} "
117 # Backup file and data directory
119 echo "Creating backup of Nextcloud file directory..."
120 tar -cpzf " ${backupdir}/${fileNameBackupFileDir}" -C "${nextcloudFileDir} " .
124 echo "Creating backup of Nextcloud data directory..."
125 tar -cpzf " ${backupdir}/${fileNameBackupDataDir}" -C "${nextcloudDataDir} " .
132 echo "Backup Nextcloud database..."
134 mysqldump
--single-transaction -h localhost
-u " ${dbUser}" -p"${dbPassword}" "${nextcloudDatabase}" > "${backupdir}/${fileNameBackupDb} "
136 # PostgreSQL (uncomment if you are using PostgreSQL as Nextcloud database)
137 #PGPASSWORD="${dbPassword}" pg_dump "${nextcloudDatabase}" -h localhost -U "${dbUser}" -f "${backupdir}/${fileNameBackupDb}"
144 echo "Starting web server..."
145 systemctl start
" ${webserverServiceName} "
150 # Disable maintenance mode
152 DisableMaintenanceMode
157 if (( ${maxNrOfBackups} != 0 ))
159 nrOfBackups
= $
( ls -l ${backupMainDir} |
grep -c ^d
)
161 if (( ${nrOfBackups} > ${maxNrOfBackups} ))
163 echo "Removing old backups..."
164 ls -t ${backupMainDir} |
tail - $
(( nrOfBackups
- maxNrOfBackups
)) |
while read dirToRemove
; do
165 echo " ${dirToRemove} "
166 rm -r ${backupMainDir} / ${dirToRemove}
175 echo "Backup created: ${backupdir} "
patrick-canterino.de