]> git.p6c8.net - nextcloud-backup-restore.git/blob - NextcloudRestore.sh
Hint when the data dir is located under the file dir
[nextcloud-backup-restore.git] / NextcloudRestore.sh
1 #!/bin/bash
2
3 #
4 # Bash script for restoring backups of Nextcloud.
5 # Usage: ./NextcloudRestor.sh <BackupName> (e.g. ./NextcloudRestor.sh 20170910_132703)
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-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 # TODO: The directory where you store the Nextcloud backups
18 backupMainDir="/mnt/Share/NextcloudBackups/"
19 restore=$1
20 currentRestoreDir="${mainBackupdir}/${restore}"
21 # TODO: The directory of your Nextcloud installation (this is a directory under your web root)
22 nextcloudFileDir="/var/www/nextcloud"
23 # TODO: The directory of your Nextcloud data directory (outside the Nextcloud file directory)
24 # If your data directory is located under Nextcloud's file directory (somewhere in the web root), the data directory should not be restored separately
25 nextcloudDataDir="/var/nextcloud_data"
26 # TODO: Your Nextcloud database name
27 nextcloudDatabase="nextcloud_db"
28 # TODO: Your Nextcloud database user
29 dbUser="nextcloud_db_user"
30 # TODO: The password of the Nextcloud database user
31 dbPassword="mYpAsSw0rd"
32 # TODO: Your webserver user
33 webserverUser="www-data"
34
35 # File names for backup files
36 # If you prefer other file names, you'll also have to change the NextcloudBackup.sh script.
37 fileNameBackupFileDir="nextcloud-filedir.tar.gz"
38 fileNameBackupDataDir="nextcloud-datadir.tar.gz"
39 fileNameBackupDb="nextcloud-db.sql"
40
41 # Function for error messages
42 errorecho() { cat <<< "$@" 1>&2; }
43
44 #
45 # Check if parameter given
46 #
47 if [ $# != "1" ]
48 then
49 errorecho "ERROR: No backup name to restore given!"
50 errorecho "Usage: NextcloudRestore.sh 'BackupDate'"
51 exit 1
52 fi
53
54 #
55 # Check for root
56 #
57 if [ "$(id -u)" != "0" ]
58 then
59 errorecho "ERROR: This script has to be run as root!"
60 exit 1
61 fi
62
63 #
64 # Check if backup dir exists
65 #
66 if [ ! -d "${currentRestoreDir}" ]
67 then
68 errorecho "ERROR: Backup ${restore} not found!"
69 exit 1
70 fi
71
72 #
73 # Set maintenance mode
74 #
75 echo "Set maintenance mode for Nextcloud..."
76 cd "${nextcloudFileDir}"
77 sudo -u "${webserverUser}" php occ maintenance:mode --on
78 cd ~
79 echo "Done"
80
81 #
82 # Stop webserver
83 #
84 echo "Stopping nginx..."
85 service nginx stop
86 echo "Done"
87
88 #
89 # Delete old Nextcloud direcories
90 #
91 echo "Deleting old Nextcloud file directory..."
92 rm -r "${nextcloudFileDir}"
93 mkdir -p "${nextcloudFileDir}"
94 echo "Done"
95 echo "Deleting old Nextcloud data directory..."
96 rm -r "${nextcloudDataDir}"
97 mkdir -p "${nextcloudDataDir}"
98 echo "Done"
99
100 #
101 # Restore file and data directory
102 #
103 echo "Restoring Nextcloud file directory..."
104 tar -xpzf "${currentRestoreDir}/${fileNameBackupFileDir}" -C "${nextcloudFileDir}"
105 echo "Done"
106 echo "Restoring Nextcloud data directory..."
107 tar -xpzf "${currentRestoreDir}/${fileNameBackupDataDir}" -C "${nextcloudDataDir}"
108 echo "Done"
109
110 #
111 # Restore database
112 #
113 echo "Dropping old Nextcloud DB..."
114 mysql -h localhost -u "${dbUser}" -p"${dbPassword}" -e "DROP DATABASE ${nextcloudDatabase}"
115 echo "Done"
116 echo "Creating new DB for Nextcloud..."
117 mysql -h localhost -u "${dbUser}" -p"${dbPassword}" -e "CREATE DATABASE ${nextcloudDatabase}"
118 echo "Done"
119 echo "Restoring backup DB..."
120 mysql -h localhost -u "${dbUser}" -p"${dbPassword}" "${nextcloudDatabase}" < "${currentRestoreDir}/${fileNameBackupDb}"
121 echo "Done"
122
123 #
124 # Start webserver
125 #
126 echo "Starting nginx..."
127 service nginx start
128 echo "Done"
129
130 #
131 # Set directory permissions
132 #
133 echo "Setting directory permissions..."
134 chown -R "${webserverUser}" "${nextcloudFileDir}"
135 chown -R "${webserverUser}" "${nextcloudDataDir}"
136 echo "Done"
137
138 #
139 # Update the system data-fingerprint (see https://docs.nextcloud.com/server/12/admin_manual/configuration_server/occ_command.html#maintenance-commands-label)
140 #
141 echo "Updating the system data-fingerprint..."
142 cd "${nextcloudFileDir}"
143 sudo -u "${webserverUser}" php occ maintenance:data-fingerprint
144 cd ~
145 echo "Done"
146
147
148 #
149 # Restore hardened directory permissions
150 #
151 echo "Set hardened directory permissions..."
152 find "${nextcloudFileDir}" -type f -print0 | xargs -0 chmod 0640
153 find "${nextcloudFileDir}" -type d -print0 | xargs -0 chmod 0750
154 chown -R root:"${webserverUser}" "${nextcloudFileDir}"
155 chown -R "${webserverUser}":"${webserverUser}" "${nextcloudFileDir}/apps/"
156 chown -R "${webserverUser}":"${webserverUser}" "${nextcloudFileDir}/config/"
157 chown -R "${webserverUser}":"${webserverUser}" "${nextcloudFileDir}/themes/"
158 chown -R "${webserverUser}":"${webserverUser}" "${nextcloudDataDir}"
159 chown root:"${webserverUser}" "${nextcloudFileDir}/.htaccess"
160 chown root:"${webserverUser}" "${nextcloudDataDir}/.htaccess"
161 chmod 0644 "${nextcloudFileDir}/.htaccess"
162 chmod 0644 "${nextcloudDataDir}/.htaccess"
163 echo "Done"
164
165 #
166 # Disbale maintenance mode
167 #
168 echo "Switching off maintenance mode..."
169 cd "${nextcloudFileDir}"
170 sudo -u "${webserverUser}" php occ maintenance:mode --off
171 cd ~
172 echo "Done"
173
174 echo "DONE!"
175 echo "Backup ${restore} successfully restored."

patrick-canterino.de