]> git.p6c8.net - nextcloud-backup-restore.git/blob - NextcloudBackup.sh
Changed the dummy DB password
[nextcloud-backup-restore.git] / NextcloudBackup.sh
1 #!/bin/bash
2
3 #
4 # Bash script for creating backups of Nextcloud.
5 # Usage: ./NextcloudBackup.sh
6 #
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/
8 #
9
10 #
11 # IMPORTANT
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".
14 #
15
16 # Variables
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="mYpAsSw0rd"
32 # TODO: Your webserver user
33 webserverUser="www-data"
34
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"
40
41 # Function for error messages
42 errorecho() { cat <<< "$@" 1>&2; }
43
44 #
45 # Check for root
46 #
47 if [ "$(id -u)" != "0" ]
48 then
49 errorecho "ERROR: This script has to be run as root!"
50 exit 1
51 fi
52
53 #
54 # Check if backup dir already exists
55 #
56 if [ ! -d "${backupdir}" ]
57 then
58 mkdir -p "${backupdir}"
59 else
60 errorecho "ERROR: The backup directory ${backupdir} already exists!"
61 exit 1
62 fi
63
64 #
65 # Set maintenance mode
66 #
67 echo "Set maintenance mode for Nextcloud..."
68 cd "${nextcloudFileDir}"
69 sudo -u "${webserverUser}" php occ maintenance:mode --on
70 cd ~
71 echo "Done"
72
73 #
74 # Stop webserver
75 #
76 echo "Stopping nginx..."
77 service nginx stop
78 echo "Done"
79
80 #
81 # Backup file and data directory
82 #
83 echo "Creating backup of Nextcloud file directory..."
84 tar -cpzf "${backupdir}/${fileNameBackupFileDir}" -C "${nextcloudFileDir}" .
85 echo "Done"
86 echo "Creating backup of Nextcloud data directory..."
87 tar -cpzf "${backupdir}/${fileNameBackupDataDir}" -C "${nextcloudDataDir}" .
88 echo "Done"
89
90 #
91 # Backup DB
92 #
93 echo "Backup Nextcloud database..."
94 mysqldump --single-transaction -h localhost -u "${dbUser}" -p"${dbPassword}" "${nextcloudDatabase}" > "${backupdir}/${fileNameBackupDb}"
95 echo "Done"
96
97 #
98 # Start webserver
99 #
100 service nginx start
101
102 #
103 # Disable maintenance mode
104 #
105 cd "${nextcloudFileDir}"
106 sudo -u "${webserverUser}" php occ maintenance:mode --off
107 cd ~
108
109 echo "DONE!"
110 echo "Backup created: ${backupdir}"

patrick-canterino.de