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

patrick-canterino.de