]> git.p6c8.net - nextcloud-backup-restore.git/blob - NextcloudBackup.sh
typo
[nextcloud-backup-restore.git] / NextcloudBackup.sh
1 #!/bin/bash
2
3 #
4 # Bash script for creating backups of Nextcloud.
5 #
6 # Version 1.0.0
7 #
8 # Usage:
9 # - With backup directory specified in the script: ./NextcloudBackup.sh
10 # - With backup directory specified by parameter: ./NextcloudBackup.sh <BackupDirectory> (e.g. ./NextcloudBackup.sh /media/hdd/nextcloud_backup)
11 #
12 # 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/
13 #
14
15 #
16 # IMPORTANT
17 # You have to customize this script (directories, users, etc.) for your actual environment.
18 # All entries which need to be customized are tagged with "TODO".
19 #
20
21 # Variables
22 backupMainDir=$1
23
24 if [ -z "$backupMainDir" ]; then
25 # TODO: The directory where you store the Nextcloud backups (when not specified by args)
26 backupMainDir='/media/hdd/nextcloud_backup'
27 fi
28
29 echo "Backup directory: $backupMainDir"
30
31 currentDate=$(date +"%Y%m%d_%H%M%S")
32
33 # The actual directory of the current backup - this is a subdirectory of the main directory above with a timestamp
34 backupdir="${backupMainDir}/${currentDate}/"
35
36 # TODO: The directory of your Nextcloud installation (this is a directory under your web root)
37 nextcloudFileDir='/var/www/nextcloud'
38
39 # TODO: The directory of your Nextcloud data directory (outside the Nextcloud file directory)
40 # 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
41 nextcloudDataDir='/var/nextcloud_data'
42
43 # TODO: The directory of your Nextcloud's local external storage.
44 # Uncomment if you use local external storage.
45 #nextcloudLocalExternalDataDir='/var/nextcloud_external_data'
46
47 # TODO: The service name of the web server. Used to start/stop web server (e.g. 'systemctl start <webserverServiceName>')
48 webserverServiceName='nginx'
49
50 # TODO: Your web server user
51 webserverUser='www-data'
52
53 # TODO: The name of the database system (ome of: mysql, mariadb, postgresql)
54 databaseSystem='mariadb'
55
56 # TODO: Your Nextcloud database name
57 nextcloudDatabase='nextcloud_db'
58
59 # TODO: Your Nextcloud database user
60 dbUser='nextcloud_db_user'
61
62 # TODO: The password of the Nextcloud database user
63 dbPassword='mYpAsSw0rd'
64
65 # TODO: The maximum number of backups to keep (when set to 0, all backups are kept)
66 maxNrOfBackups=0
67
68 # File names for backup files
69 # If you prefer other file names, you'll also have to change the NextcloudRestore.sh script.
70 fileNameBackupFileDir='nextcloud-filedir.tar.gz'
71 fileNameBackupDataDir='nextcloud-datadir.tar.gz'
72
73 # TOOD: Uncomment if you use local external storage
74 #fileNameBackupExternalDataDir='nextcloud-external-datadir.tar.gz'
75
76 fileNameBackupDb='nextcloud-db.sql'
77
78 # Function for error messages
79 errorecho() { cat <<< "$@" 1>&2; }
80
81 function DisableMaintenanceMode() {
82 echo "Switching off maintenance mode..."
83 sudo -u "${webserverUser}" php ${nextcloudFileDir}/occ maintenance:mode --off
84 echo "Done"
85 echo
86 }
87
88 # Capture CTRL+C
89 trap CtrlC INT
90
91 function CtrlC() {
92 read -p "Backup cancelled. Keep maintenance mode? [y/n] " -n 1 -r
93 echo
94
95 if ! [[ $REPLY =~ ^[Yy]$ ]]
96 then
97 DisableMaintenanceMode
98 else
99 echo "Maintenance mode still enabled."
100 fi
101
102 exit 1
103 }
104
105 #
106 # Check for root
107 #
108 if [ "$(id -u)" != "0" ]
109 then
110 errorecho "ERROR: This script has to be run as root!"
111 exit 1
112 fi
113
114 #
115 # Check if backup dir already exists
116 #
117 if [ ! -d "${backupdir}" ]
118 then
119 mkdir -p "${backupdir}"
120 else
121 errorecho "ERROR: The backup directory ${backupdir} already exists!"
122 exit 1
123 fi
124
125 #
126 # Set maintenance mode
127 #
128 echo "Set maintenance mode for Nextcloud..."
129 sudo -u "${webserverUser}" php ${nextcloudFileDir}/occ maintenance:mode --on
130 echo "Done"
131 echo
132
133 #
134 # Stop web server
135 #
136 echo "Stopping web server..."
137 systemctl stop "${webserverServiceName}"
138 echo "Done"
139 echo
140
141 #
142 # Backup file directory
143 #
144 echo "Creating backup of Nextcloud file directory..."
145 tar -cpzf "${backupdir}/${fileNameBackupFileDir}" -C "${nextcloudFileDir}" .
146 echo "Done"
147 echo
148
149 #
150 # Backup data directory
151 #
152 echo "Creating backup of Nextcloud data directory..."
153 tar -cpzf "${backupdir}/${fileNameBackupDataDir}" -C "${nextcloudDataDir}" .
154 echo "Done"
155 echo
156
157 # Backup local external storage.
158 # Uncomment if you use local external storage
159 #echo "Creating backup of Nextcloud local external storage directory..."
160 #tar -cpzf "${backupdir}/${fileNameBackupExternalDataDir}" -C "${nextcloudLocalExternalDataDir}" .
161 #echo "Done"
162 #echo
163
164 #
165 # Backup DB
166 #
167 if [ "${databaseSystem,,}" = "mysql" ] || [ "${databaseSystem,,}" = "mariadb" ]; then
168 echo "Backup Nextcloud database (MySQL/MariaDB)..."
169
170 if ! [ -x "$(command -v mysqldump)" ]; then
171 errorecho "ERROR: MySQL/MariaDB not installed (command mysqldump not found)."
172 errorecho "ERROR: No backup of database possible!"
173 else
174 mysqldump --single-transaction -h localhost -u "${dbUser}" -p"${dbPassword}" "${nextcloudDatabase}" > "${backupdir}/${fileNameBackupDb}"
175 fi
176
177 echo "Done"
178 echo
179 elif [ "${databaseSystem,,}" = "postgresql" ]; then
180 echo "Backup Nextcloud database (PostgreSQL)..."
181
182 if ! [ -x "$(command -v pg_dump)" ]; then
183 errorecho "ERROR:PostgreSQL not installed (command pg_dump not found)."
184 errorecho "ERROR: No backup of database possible!"
185 else
186 PGPASSWORD="${dbPassword}" pg_dump "${nextcloudDatabase}" -h localhost -U "${dbUser}" -f "${backupdir}/${fileNameBackupDb}"
187 fi
188
189 echo "Done"
190 echo
191 fi
192
193 #
194 # Start web server
195 #
196 echo "Starting web server..."
197 systemctl start "${webserverServiceName}"
198 echo "Done"
199 echo
200
201 #
202 # Disable maintenance mode
203 #
204 DisableMaintenanceMode
205
206 #
207 # Delete old backups
208 #
209 if [ ${maxNrOfBackups} != 0 ]
210 then
211 nrOfBackups=$(ls -l ${backupMainDir} | grep -c ^d)
212
213 if [[ ${nrOfBackups} > ${maxNrOfBackups} ]]
214 then
215 echo "Removing old backups..."
216 ls -t ${backupMainDir} | tail -$(( nrOfBackups - maxNrOfBackups )) | while read -r dirToRemove; do
217 echo "${dirToRemove}"
218 rm -r "${backupMainDir}/${dirToRemove:?}"
219 echo "Done"
220 echo
221 done
222 fi
223 fi
224
225 echo
226 echo "DONE!"
227 echo "Backup created: ${backupdir}"

patrick-canterino.de