]>
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 nextcloudDataDir
= "/var/nextcloud_data"
26 # TODO: Your Nextcloud database name
27 nextcloudDatabase
= "nextcloud_db"
28 # TODO: Your Nextcloud database user
29 dbUser
= "nextcloud_db_user"
30 # TODO: The password of the Nextcloud database user
31 dbPassword
= "MeInPasSw0rT"
32 # TODO: Your webserver user
33 webserverUser
= "www-data"
35 # File names for backup files
36 # If you prefer other file names, you'll also have to change the NextcloudRestore.sh script.
37 fileNameBackupFileDir
= "nextcloud-filedir.tar.gz"
38 fileNameBackupDataDir
= "nextcloud-datadir.tar.gz"
39 fileNameBackupDb
= "nextcloud-db.sql"
41 # Function for error messages
42 errorecho
() { cat <<< "$@" 1 >& 2 ; }
47 if [ "$(id -u)" != "0" ]
49 errorecho
"ERROR: This script has to be run as root!"
54 # Check if backup dir already exists
56 if [ ! -d " ${backupdir} " ]
58 mkdir
-p " ${backupdir} "
60 errorecho
"ERROR: The backup directory ${backupdir} already exists!"
65 # Set maintenance mode
67 echo "Set maintenance mode for Nextcloud..."
68 cd " ${nextcloudFileDir} "
69 sudo
-u " ${webserverUser} " php occ maintenance
: mode
--on
76 echo "Stopping nginx..."
81 # Backup file and data directory
83 echo "Creating backup of Nextcloud file directory..."
84 tar -cpzf " ${backupdir}/${fileNameBackupFileDir}" -C "${nextcloudFileDir} " .
86 echo "Creating backup of Nextcloud data directory..."
87 tar -cpzf " ${backupdir}/${fileNameBackupDataDir}" -C "${nextcloudDataDir} " .
93 echo "Backup Nextcloud database..."
94 mysqldump
--single-transaction -h localhost
-u " ${dbUser}" -p"${dbPassword}" "${nextcloudDatabase}" > "${backupdir}/${fileNameBackupDb} "
103 # Disable maintenance mode
105 cd " ${nextcloudFileDir} "
106 sudo
-u " ${webserverUser} " php occ maintenance
: mode
--off
110 echo "Backup created: ${backupdir} "
patrick-canterino.de