]> git.p6c8.net - nextcloud-backup-restore.git/blob - NextcloudBackup.sh
f1e591b85cfad282089e32b85c69cebe1047133d
[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 cd "${nextcloudFileDir}"
52 sudo -u "${webserverUser}" php occ maintenance:mode --off
53 cd ~
54 echo "Done"
55 echo
56 }
57
58 # Capture CTRL+C
59 trap CtrlC INT
60
61 function CtrlC() {
62 read -p "Backup cancelled. Keep maintenance mode? [y/n] " -n 1 -r
63 echo
64
65 if ! [[ $REPLY =~ ^[Yy]$ ]]
66 then
67 DisableMaintenanceMode
68 else
69 echo "Maintenance mode still enabled."
70 fi
71
72 exit 1
73 }
74
75 #
76 # Check for root
77 #
78 if [ "$(id -u)" != "0" ]
79 then
80 errorecho "ERROR: This script has to be run as root!"
81 exit 1
82 fi
83
84 #
85 # Check if backup dir already exists
86 #
87 if [ ! -d "${backupdir}" ]
88 then
89 mkdir -p "${backupdir}"
90 else
91 errorecho "ERROR: The backup directory ${backupdir} already exists!"
92 exit 1
93 fi
94
95 #
96 # Set maintenance mode
97 #
98 echo "Set maintenance mode for Nextcloud..."
99 cd "${nextcloudFileDir}"
100 sudo -u "${webserverUser}" php occ maintenance:mode --on
101 cd ~
102 echo "Done"
103 echo
104
105 #
106 # Stop web server
107 #
108 echo "Stopping web server..."
109 service "${webserverServiceName}" stop
110 echo "Done"
111 echo
112
113 #
114 # Backup file and data directory
115 #
116 echo "Creating backup of Nextcloud file directory..."
117 tar -cpzf "${backupdir}/${fileNameBackupFileDir}" -C "${nextcloudFileDir}" .
118 echo "Done"
119 echo
120
121 echo "Creating backup of Nextcloud data directory..."
122 tar -cpzf "${backupdir}/${fileNameBackupDataDir}" -C "${nextcloudDataDir}" .
123 echo "Done"
124 echo
125
126 #
127 # Backup DB
128 #
129 echo "Backup Nextcloud database..."
130 mysqldump --single-transaction -h localhost -u "${dbUser}" -p"${dbPassword}" "${nextcloudDatabase}" > "${backupdir}/${fileNameBackupDb}"
131 echo "Done"
132 echo
133
134 #
135 # Start web server
136 #
137 echo "Starting web server..."
138 service "${webserverServiceName}" start
139 echo "Done"
140 echo
141
142 #
143 # Disable maintenance mode
144 #
145 DisableMaintenanceMode()
146
147 #
148 # Delete old backups
149 #
150 if (( ${maxNrOfBackups} != 0 ))
151 then
152 nrOfBackups=$(ls -l ${backupMainDir} | grep -c ^d)
153
154 if (( ${nrOfBackups} > ${maxNrOfBackups} ))
155 then
156 echo "Removing old backups..."
157 ls -t ${backupMainDir} | tail -$(( nrOfBackups - maxNrOfBackups )) | while read dirToRemove; do
158 echo "${dirToRemove}"
159 rm -r ${backupMainDir}/${dirToRemove}
160 echo "Done"
161 echo
162 done
163 fi
164 fi
165
166 echo
167 echo "DONE!"
168 echo "Backup created: ${backupdir}"

patrick-canterino.de