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

patrick-canterino.de