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

patrick-canterino.de