]>
git.p6c8.net - nextcloud-backup-restore.git/blob - NextcloudBackup.sh
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-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".
17 currentDate
= $
( date + "%Y%m%d_%H%M%S" )
18 # TODO: The directory where you store the Nextcloud backups
19 backupMainDir
= "/mnt/Share/NextcloudBackups/"
20 # The actual directory of the current backup - this is is subdirectory of the main directory above with a timestamp
21 backupdir
= " ${backupMainDir} / ${currentDate} /"
22 # TODO: The directory of your Nextcloud installation (this is a directory under your web root)
23 nextcloudFileDir
= "/var/www/nextcloud"
24 # TODO: The directory of your Nextcloud data directory (outside the Nextcloud file directory)
25 # 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
26 nextcloudDataDir
= "/var/nextcloud_data"
27 # TODO: The service name of the webserver. Used to start/stop webserver (e.g. 'service <webserverServiceName> start')
28 webserverServiceName
= "nginx"
29 # TODO: Your Nextcloud database name
30 nextcloudDatabase
= "nextcloud_db"
31 # TODO: Your Nextcloud database user
32 dbUser
= "nextcloud_db_user"
33 # TODO: The password of the Nextcloud database user
34 dbPassword
= "mYpAsSw0rd"
35 # TODO: Your webserver user
36 webserverUser
= "www-data"
37 # TODO: The maximum number of backups to keep (when set to 0, all backups are kept)
40 # File names for backup files
41 # If you prefer other file names, you'll also have to change the NextcloudRestore.sh script.
42 fileNameBackupFileDir
= "nextcloud-filedir.tar.gz"
43 fileNameBackupDataDir
= "nextcloud-datadir.tar.gz"
44 fileNameBackupDb
= "nextcloud-db.sql"
46 # Function for error messages
47 errorecho
() { cat <<< "$@" 1 >& 2 ; }
52 if [ "$(id -u)" != "0" ]
54 errorecho
"ERROR: This script has to be run as root!"
59 # Check if backup dir already exists
61 if [ ! -d " ${backupdir} " ]
63 mkdir
-p " ${backupdir} "
65 errorecho
"ERROR: The backup directory ${backupdir} already exists!"
70 # Set maintenance mode
72 echo "Set maintenance mode for Nextcloud..."
73 cd " ${nextcloudFileDir} "
74 sudo
-u " ${webserverUser} " php occ maintenance
: mode
--on
82 echo "Stopping webserver..."
83 service
" ${webserverServiceName} " stop
88 # Backup file and data directory
90 echo "Creating backup of Nextcloud file directory..."
91 tar -cpzf " ${backupdir}/${fileNameBackupFileDir}" -C "${nextcloudFileDir} " .
95 echo "Creating backup of Nextcloud data directory..."
96 tar -cpzf " ${backupdir}/${fileNameBackupDataDir}" -C "${nextcloudDataDir} " .
103 echo "Backup Nextcloud database..."
104 mysqldump
--single-transaction -h localhost
-u " ${dbUser}" -p"${dbPassword}" "${nextcloudDatabase}" > "${backupdir}/${fileNameBackupDb} "
111 echo "Starting webserver..."
112 service
" ${webserverServiceName} " start
117 # Disable maintenance mode
119 echo "Switching off maintenance mode..."
120 cd " ${nextcloudFileDir} "
121 sudo
-u " ${webserverUser} " php occ maintenance
: mode
--off
129 if [ ${maxNrOfBackups} != 0 ]
131 nrOfBackups
= $
( ls -l ${backupMainDir} |
grep -c ^d
)
133 if [ ${maxNrOfBackups} > ${nrOfBackups} ]
135 echo "Removing old backups..."
136 ls -t ${backupMainDir} |
tail - $
(( nrOfBackups
- maxNrOfBackups
)) |
while read dirToRemove
; do
137 echo " ${dirToRemove} "
138 rm -r ${backupMainDir} / ${dirToRemove}
147 echo "Backup created: ${backupdir} "
patrick-canterino.de