]> git.p6c8.net - nextcloud-backup-restore.git/blob - NextcloudRestore.sh
fix double closing curly bracket
[nextcloud-backup-restore.git] / NextcloudRestore.sh
1 #!/bin/bash
2
3 #
4 # Bash script for restoring backups of Nextcloud.
5 # Usage: ./NextcloudRestore.sh <BackupName> (e.g. ./NextcloudRestore.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 echo
81
82 #
83 # Stop webserver
84 #
85 echo "Stopping nginx..."
86 service nginx stop
87 echo "Done"
88 echo
89
90 #
91 # Delete old Nextcloud direcories
92 #
93 echo "Deleting old Nextcloud file directory..."
94 rm -r "${nextcloudFileDir}"
95 mkdir -p "${nextcloudFileDir}"
96 echo "Done"
97 echo
98
99 echo "Deleting old Nextcloud data directory..."
100 rm -r "${nextcloudDataDir}"
101 mkdir -p "${nextcloudDataDir}"
102 echo "Done"
103 echo
104
105 #
106 # Restore file and data directory
107 #
108 echo "Restoring Nextcloud file directory..."
109 tar -xpzf "${currentRestoreDir}/${fileNameBackupFileDir}" -C "${nextcloudFileDir}"
110 echo "Done"
111 echo
112
113 echo "Restoring Nextcloud data directory..."
114 tar -xpzf "${currentRestoreDir}/${fileNameBackupDataDir}" -C "${nextcloudDataDir}"
115 echo "Done"
116 echo
117
118 #
119 # Restore database
120 #
121 echo "Dropping old Nextcloud DB..."
122 mysql -h localhost -u "${dbUser}" -p"${dbPassword}" -e "DROP DATABASE ${nextcloudDatabase}"
123 echo "Done"
124 echo
125
126 echo "Creating new DB for Nextcloud..."
127 mysql -h localhost -u "${dbUser}" -p"${dbPassword}" -e "CREATE DATABASE ${nextcloudDatabase}"
128 echo "Done"
129 echo
130
131 echo "Restoring backup DB..."
132 mysql -h localhost -u "${dbUser}" -p"${dbPassword}" "${nextcloudDatabase}" < "${currentRestoreDir}/${fileNameBackupDb}"
133 echo "Done"
134 echo
135
136 #
137 # Start webserver
138 #
139 echo "Starting nginx..."
140 service nginx start
141 echo "Done"
142 echo
143
144 #
145 # Set directory permissions
146 #
147 echo "Setting directory permissions..."
148 chown -R "${webserverUser}":"${webserverUser}" "${nextcloudFileDir}"
149 chown -R "${webserverUser}":"${webserverUser}" "${nextcloudDataDir}"
150 echo "Done"
151 echo
152
153 #
154 # Update the system data-fingerprint (see https://docs.nextcloud.com/server/12/admin_manual/configuration_server/occ_command.html#maintenance-commands-label)
155 #
156 echo "Updating the system data-fingerprint..."
157 cd "${nextcloudFileDir}"
158 sudo -u "${webserverUser}" php occ maintenance:data-fingerprint
159 cd ~
160 echo "Done"
161 echo
162
163 #
164 # Disbale maintenance mode
165 #
166 echo "Switching off maintenance mode..."
167 cd "${nextcloudFileDir}"
168 sudo -u "${webserverUser}" php occ maintenance:mode --off
169 cd ~
170 echo "Done"
171 echo
172
173 echo
174 echo "DONE!"
175 echo "Backup ${restore} successfully restored."

patrick-canterino.de