]>
git.p6c8.net - nextcloud-backup-restore.git/blob - NextcloudRestore.sh
7ef3f81563c5b8d8eddcfb7ad18a2c2499abcc8c
4 # Bash script for restoring backups of Nextcloud.
5 # Usage: ./NextcloudRestor.sh <BackupName> (e.g. ./NextcloudRestor.sh 20170910_132703)
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/
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".
17 # TODO: The directory where you store the Nextcloud backups
18 backupMainDir
= "/mnt/Share/NextcloudBackups/"
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"
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"
41 # Function for error messages
42 errorecho
() { cat <<< "$@" 1 >& 2 ; }
45 # Check if parameter given
49 errorecho
"ERROR: No backup name to restore given!"
50 errorecho
"Usage: NextcloudRestore.sh 'BackupDate'"
57 if [ "$(id -u)" != "0" ]
59 errorecho
"ERROR: This script has to be run as root!"
64 # Check if backup dir exists
66 if [ ! -d " ${currentRestoreDir} " ]
68 errorecho
"ERROR: Backup ${restore} not found!"
73 # Set maintenance mode
75 echo "Set maintenance mode for Nextcloud..."
76 cd " ${nextcloudFileDir} "
77 sudo
-u " ${webserverUser} " php occ maintenance
: mode
--on
84 echo "Stopping nginx..."
89 # Delete old Nextcloud direcories
91 echo "Deleting old Nextcloud file directory..."
92 rm -r " ${nextcloudFileDir} "
93 mkdir
-p " ${nextcloudFileDir} "
95 echo "Deleting old Nextcloud data directory..."
96 rm -r " ${nextcloudDataDir} "
97 mkdir
-p " ${nextcloudDataDir} "
101 # Restore file and data directory
103 echo "Restoring Nextcloud file directory..."
104 tar -xpzf " ${currentRestoreDir}/${fileNameBackupFileDir}" -C "${nextcloudFileDir} "
106 echo "Restoring Nextcloud data directory..."
107 tar -xpzf " ${currentRestoreDir}/${fileNameBackupDataDir}" -C "${nextcloudDataDir} "
113 echo "Dropping old Nextcloud DB..."
114 mysql
-h localhost
-u " ${dbUser}" -p"${dbPassword}" -e "DROP DATABASE ${nextcloudDatabase} "
116 echo "Creating new DB for Nextcloud..."
117 mysql
-h localhost
-u " ${dbUser}" -p"${dbPassword}" -e "CREATE DATABASE ${nextcloudDatabase} "
119 echo "Restoring backup DB..."
120 mysql
-h localhost
-u " ${dbUser}" -p"${dbPassword}" "${nextcloudDatabase}" < "${currentRestoreDir}/${fileNameBackupDb} "
126 echo "Starting nginx..."
131 # Set directory permissions
133 echo "Setting directory permissions..."
134 chown
-R " ${webserverUser} " " ${nextcloudFileDir} "
135 chown
-R " ${webserverUser} " " ${nextcloudDataDir} "
139 # Update the system data-fingerprint (see https://docs.nextcloud.com/server/12/admin_manual/configuration_server/occ_command.html#maintenance-commands-label)
141 echo "Updating the system data-fingerprint..."
142 cd " ${nextcloudFileDir} "
143 sudo
-u " ${webserverUser} " php occ maintenance
: data-fingerprint
149 # Restore hardened directory permissions
151 echo "Set hardened directory permissions..."
152 find " ${nextcloudFileDir} " -type f
-print0 |
xargs -0 chmod 0640
153 find " ${nextcloudFileDir} " -type d
-print0 |
xargs -0 chmod 0750
154 chown
-R root
: " ${webserverUser} " " ${nextcloudFileDir} "
155 chown
-R " ${webserverUser}":"${webserverUser}" "${nextcloudFileDir} /apps/"
156 chown
-R " ${webserverUser}":"${webserverUser}" "${nextcloudFileDir} /config/"
157 chown
-R " ${webserverUser}":"${webserverUser}" "${nextcloudFileDir} /themes/"
158 chown
-R " ${webserverUser}":"${webserverUser}" "${nextcloudDataDir} "
159 chown root
: " ${webserverUser} " " ${nextcloudFileDir} /.htaccess"
160 chown root
: " ${webserverUser} " " ${nextcloudDataDir} /.htaccess"
161 chmod 0644 " ${nextcloudFileDir} /.htaccess"
162 chmod 0644 " ${nextcloudDataDir} /.htaccess"
166 # Disbale maintenance mode
168 echo "Switching off maintenance mode..."
169 cd " ${nextcloudFileDir} "
170 sudo
-u " ${webserverUser} " php occ maintenance
: mode
--off
175 echo "Backup ${restore} successfully restored."
patrick-canterino.de