]> git.p6c8.net - nextcloud-backup-restore.git/blob - NextcloudRestore.sh
69b4b003ea5ebabae3bc4c0ea1163aa304d98092
[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-18-04-lts-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. 'systemctl start <webserverServiceName>')
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 systemctl stop "${webserverServiceName}"
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 -xmpzf "${currentRestoreDir}/${fileNameBackupFileDir}" -C "${nextcloudFileDir}"
112 echo "Done"
113 echo
114
115 echo "Restoring Nextcloud data directory..."
116 tar -xmpzf "${currentRestoreDir}/${fileNameBackupDataDir}" -C "${nextcloudDataDir}"
117 echo "Done"
118 echo
119
120 #
121 # Restore database
122 #
123 echo "Dropping old Nextcloud DB..."
124 # MySQL/MariaDB:
125 mysql -h localhost -u "${dbUser}" -p"${dbPassword}" -e "DROP DATABASE ${nextcloudDatabase}"
126
127 # PostgreSQL (uncomment if you are using PostgreSQL as Nextcloud database)
128 #PGPASSWORD="${dbPassword}" psql -h localhost -U "${dbUser}" -d nextcloud -c "DROP DATABASE \"${nextcloudDatabase}\";"
129 echo "Done"
130 echo
131
132 echo "Creating new DB for Nextcloud..."
133 # MySQL/MariaDB:
134 # Use this if the databse from the backup uses UTF8 with multibyte support (e.g. for emoijs in filenames):
135 mysql -h localhost -u "${dbUser}" -p"${dbPassword}" -e "CREATE DATABASE ${nextcloudDatabase} CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci"
136 # Use this if the database from the backup DOES NOT use UTF8 with multibyte support (e.g. for emoijs in filenames):
137 #mysql -h localhost -u "${dbUser}" -p"${dbPassword}" -e "CREATE DATABASE ${nextcloudDatabase}"
138
139 # PostgreSQL (uncomment if you are using PostgreSQL as Nextcloud database)
140 #PGPASSWORD="${dbPassword}" psql -h localhost -U "${dbUser}" -d "${nextcloudDatabase}" -c "CREATE DATABASE \"${nextcloudDatabase}\";"
141 echo "Done"
142 echo
143
144 echo "Restoring backup DB..."
145 # MySQL/MariaDB:
146 mysql -h localhost -u "${dbUser}" -p"${dbPassword}" "${nextcloudDatabase}" < "${currentRestoreDir}/${fileNameBackupDb}"
147
148 # PostgreSQL (uncomment if you are using PostgreSQL as Nextcloud database)
149 #PGPASSWORD="${dbPassword}" pg_restore -c -d "${nextcloudDatabase}" -h localhost -U "${dbUser}" "${currentRestoreDir}/${fileNameBackupDb}"
150 echo "Done"
151 echo
152
153 #
154 # Start web server
155 #
156 echo "Starting web server..."
157 systemctl start "${webserverServiceName}"
158 echo "Done"
159 echo
160
161 #
162 # Set directory permissions
163 #
164 echo "Setting directory permissions..."
165 chown -R "${webserverUser}":"${webserverUser}" "${nextcloudFileDir}"
166 chown -R "${webserverUser}":"${webserverUser}" "${nextcloudDataDir}"
167 echo "Done"
168 echo
169
170 #
171 # Update the system data-fingerprint (see https://docs.nextcloud.com/server/15/admin_manual/configuration_server/occ_command.html#maintenance-commands-label)
172 #
173 echo "Updating the system data-fingerprint..."
174 sudo -u "${webserverUser}" php ${nextcloudFileDir}/occ maintenance:data-fingerprint
175 echo "Done"
176 echo
177
178 #
179 # Disbale maintenance mode
180 #
181 echo "Switching off maintenance mode..."
182 sudo -u "${webserverUser}" php ${nextcloudFileDir}/occ maintenance:mode --off
183 echo "Done"
184 echo
185
186 echo
187 echo "DONE!"
188 echo "Backup ${restore} successfully restored."

patrick-canterino.de