]> git.p6c8.net - nextcloud-backup-restore.git/blob - NextcloudRestore.sh
Added scripts for backup/restore
[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 # Set directory permissions
124 #
125 echo "Setting directory permissions..."
126 chown -R "${webserverUser}" "${nextcloudFileDir}"
127 chown -R "${webserverUser}" "${nextcloudDataDir}"
128 echo "Done"
129
130 #
131 # Start webserver
132 #
133 echo "Starting nginx..."
134 service nginx start
135 echo "Done"
136
137 #
138 # Restore hardened directory permissions
139 #
140 echo "Set hardened directory permissions..."
141 find "${nextcloudFileDir}" -type f -print0 | xargs -0 chmod 0640
142 find "${nextcloudFileDir}" -type d -print0 | xargs -0 chmod 0750
143 chown -R root:"${webserverUser}" "${nextcloudFileDir}"
144 chown -R "${webserverUser}":"${webserverUser}" "${nextcloudFileDir}/apps/"
145 chown -R "${webserverUser}":"${webserverUser}" "${nextcloudFileDir}/config/"
146 chown -R "${webserverUser}":"${webserverUser}" "${nextcloudFileDir}/themes/"
147 chown -R "${webserverUser}":"${webserverUser}" "${nextcloudDataDir}"
148 chown root:"${webserverUser}" "${nextcloudFileDir}/.htaccess"
149 chown root:"${webserverUser}" "${nextcloudDataDir}/.htaccess"
150 chmod 0644 "${nextcloudFileDir}/.htaccess"
151 chmod 0644 "${nextcloudDataDir}/.htaccess"
152 echo "Done"
153
154 #
155 # Disbale maintenance mode
156 #
157 echo "Switching off maintenance mode..."
158 cd "${nextcloudFileDir}"
159 sudo -u "${webserverUser}" php occ maintenance:mode --off
160 cd ~
161 echo "Done"
162
163 #
164 # Update the system data-fingerprint (see https://docs.nextcloud.com/server/12/admin_manual/configuration_server/occ_command.html#maintenance-commands-label)
165 #
166 echo "Updating the system data-fingerprint..."
167 cd "${nextcloudFileDir}"
168 sudo -u "${webserverUser}" php occ maintenance:data-fingerprint
169 cd ~
170 echo "Done"
171
172 echo "DONE!"
173 echo "Backup ${restore} successfully restored."

patrick-canterino.de