]> git.p6c8.net - nextcloud-backup-restore.git/blob - NextcloudBackup.sh
ace78d8fdf76033069914f4f3ee464c7e0e36bbb
[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 # 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: Your Nextcloud database name
28 nextcloudDatabase="nextcloud_db"
29 # TODO: Your Nextcloud database user
30 dbUser="nextcloud_db_user"
31 # TODO: The password of the Nextcloud database user
32 dbPassword="mYpAsSw0rd"
33 # TODO: Your webserver user
34 webserverUser="www-data"
35
36 # File names for backup files
37 # If you prefer other file names, you'll also have to change the NextcloudRestore.sh script.
38 fileNameBackupFileDir="nextcloud-filedir.tar.gz"
39 fileNameBackupDataDir="nextcloud-datadir.tar.gz"
40 fileNameBackupDb="nextcloud-db.sql"
41
42 # Function for error messages
43 errorecho() { cat <<< "$@" 1>&2; }
44
45 #
46 # Check for root
47 #
48 if [ "$(id -u)" != "0" ]
49 then
50 errorecho "ERROR: This script has to be run as root!"
51 exit 1
52 fi
53
54 #
55 # Check if backup dir already exists
56 #
57 if [ ! -d "${backupdir}" ]
58 then
59 mkdir -p "${backupdir}"
60 else
61 errorecho "ERROR: The backup directory ${backupdir} already exists!"
62 exit 1
63 fi
64
65 #
66 # Set maintenance mode
67 #
68 echo "Set maintenance mode for Nextcloud..."
69 cd "${nextcloudFileDir}"
70 sudo -u "${webserverUser}" php occ maintenance:mode --on
71 cd ~
72 echo "Done"
73
74 #
75 # Stop webserver
76 #
77 echo "Stopping nginx..."
78 service nginx stop
79 echo "Done"
80
81 #
82 # Backup file and data directory
83 #
84 echo "Creating backup of Nextcloud file directory..."
85 tar -cpzf "${backupdir}/${fileNameBackupFileDir}" -C "${nextcloudFileDir}" .
86 echo "Done"
87 echo "Creating backup of Nextcloud data directory..."
88 tar -cpzf "${backupdir}/${fileNameBackupDataDir}" -C "${nextcloudDataDir}" .
89 echo "Done"
90
91 #
92 # Backup DB
93 #
94 echo "Backup Nextcloud database..."
95 mysqldump --single-transaction -h localhost -u "${dbUser}" -p"${dbPassword}" "${nextcloudDatabase}" > "${backupdir}/${fileNameBackupDb}"
96 echo "Done"
97
98 #
99 # Start webserver
100 #
101 service nginx start
102
103 #
104 # Disable maintenance mode
105 #
106 cd "${nextcloudFileDir}"
107 sudo -u "${webserverUser}" php occ maintenance:mode --off
108 cd ~
109
110 echo "DONE!"
111 echo "Backup created: ${backupdir}"

patrick-canterino.de