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

patrick-canterino.de