]> git.p6c8.net - nextcloud-backup-restore.git/blob - NextcloudBackup.sh
No 'cd' for maintenace mode; added tar parameter 'm' for restore
[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-18-04-lts-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 a 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: The service name of the web server. Used to start/stop web server (e.g. 'service <webserverServiceName> start')
28 webserverServiceName="nginx"
29 # TODO: Your Nextcloud database name
30 nextcloudDatabase="nextcloud_db"
31 # TODO: Your Nextcloud database user
32 dbUser="nextcloud_db_user"
33 # TODO: The password of the Nextcloud database user
34 dbPassword="mYpAsSw0rd"
35 # TODO: Your web server user
36 webserverUser="www-data"
37 # TODO: The maximum number of backups to keep (when set to 0, all backups are kept)
38 maxNrOfBackups=0
39
40 # File names for backup files
41 # If you prefer other file names, you'll also have to change the NextcloudRestore.sh script.
42 fileNameBackupFileDir="nextcloud-filedir.tar.gz"
43 fileNameBackupDataDir="nextcloud-datadir.tar.gz"
44 fileNameBackupDb="nextcloud-db.sql"
45
46 # Function for error messages
47 errorecho() { cat <<< "$@" 1>&2; }
48
49 function DisableMaintenanceMode() {
50 echo "Switching off maintenance mode..."
51 sudo -u "${webserverUser}" php ${nextcloudFileDir}/occ maintenance:mode --off
52 echo "Done"
53 echo
54 }
55
56 # Capture CTRL+C
57 trap CtrlC INT
58
59 function CtrlC() {
60 read -p "Backup cancelled. Keep maintenance mode? [y/n] " -n 1 -r
61 echo
62
63 if ! [[ $REPLY =~ ^[Yy]$ ]]
64 then
65 DisableMaintenanceMode
66 else
67 echo "Maintenance mode still enabled."
68 fi
69
70 exit 1
71 }
72
73 #
74 # Check for root
75 #
76 if [ "$(id -u)" != "0" ]
77 then
78 errorecho "ERROR: This script has to be run as root!"
79 exit 1
80 fi
81
82 #
83 # Check if backup dir already exists
84 #
85 if [ ! -d "${backupdir}" ]
86 then
87 mkdir -p "${backupdir}"
88 else
89 errorecho "ERROR: The backup directory ${backupdir} already exists!"
90 exit 1
91 fi
92
93 #
94 # Set maintenance mode
95 #
96 echo "Set maintenance mode for Nextcloud..."
97 sudo -u "${webserverUser}" php ${nextcloudFileDir}/occ maintenance:mode --on
98 echo "Done"
99 echo
100
101 #
102 # Stop web server
103 #
104 echo "Stopping web server..."
105 service "${webserverServiceName}" stop
106 echo "Done"
107 echo
108
109 #
110 # Backup file and data directory
111 #
112 echo "Creating backup of Nextcloud file directory..."
113 tar -cpzf "${backupdir}/${fileNameBackupFileDir}" -C "${nextcloudFileDir}" .
114 echo "Done"
115 echo
116
117 echo "Creating backup of Nextcloud data directory..."
118 tar -cpzf "${backupdir}/${fileNameBackupDataDir}" -C "${nextcloudDataDir}" .
119 echo "Done"
120 echo
121
122 #
123 # Backup DB
124 #
125 echo "Backup Nextcloud database..."
126 mysqldump --single-transaction -h localhost -u "${dbUser}" -p"${dbPassword}" "${nextcloudDatabase}" > "${backupdir}/${fileNameBackupDb}"
127 echo "Done"
128 echo
129
130 #
131 # Start web server
132 #
133 echo "Starting web server..."
134 service "${webserverServiceName}" start
135 echo "Done"
136 echo
137
138 #
139 # Disable maintenance mode
140 #
141 DisableMaintenanceMode
142
143 #
144 # Delete old backups
145 #
146 if (( ${maxNrOfBackups} != 0 ))
147 then
148 nrOfBackups=$(ls -l ${backupMainDir} | grep -c ^d)
149
150 if (( ${nrOfBackups} > ${maxNrOfBackups} ))
151 then
152 echo "Removing old backups..."
153 ls -t ${backupMainDir} | tail -$(( nrOfBackups - maxNrOfBackups )) | while read dirToRemove; do
154 echo "${dirToRemove}"
155 rm -r ${backupMainDir}/${dirToRemove}
156 echo "Done"
157 echo
158 done
159 fi
160 fi
161
162 echo
163 echo "DONE!"
164 echo "Backup created: ${backupdir}"

patrick-canterino.de