]>
git.p6c8.net - nextcloud-backup-restore.git/blob - NextcloudRestore.sh
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 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"
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"
40 # Function for error messages
41 errorecho
() { cat <<< "$@" 1 >& 2 ; }
44 # Check if parameter given
48 errorecho
"ERROR: No backup name to restore given!"
49 errorecho
"Usage: NextcloudRestore.sh 'BackupDate'"
56 if [ "$(id -u)" != "0" ]
58 errorecho
"ERROR: This script has to be run as root!"
63 # Check if backup dir exists
65 if [ ! -d " ${currentRestoreDir} " ]
67 errorecho
"ERROR: Backup ${restore} not found!"
72 # Set maintenance mode
74 echo "Set maintenance mode for Nextcloud..."
75 cd " ${nextcloudFileDir} "
76 sudo
-u " ${webserverUser} " php occ maintenance
: mode
--on
83 echo "Stopping nginx..."
88 # Delete old Nextcloud direcories
90 echo "Deleting old Nextcloud file directory..."
91 rm -r " ${nextcloudFileDir} "
92 mkdir
-p " ${nextcloudFileDir} "
94 echo "Deleting old Nextcloud data directory..."
95 rm -r " ${nextcloudDataDir} "
96 mkdir
-p " ${nextcloudDataDir} "
100 # Restore file and data directory
102 echo "Restoring Nextcloud file directory..."
103 tar -xpzf " ${currentRestoreDir}/${fileNameBackupFileDir}" -C "${nextcloudFileDir} "
105 echo "Restoring Nextcloud data directory..."
106 tar -xpzf " ${currentRestoreDir}/${fileNameBackupDataDir}" -C "${nextcloudDataDir} "
112 echo "Dropping old Nextcloud DB..."
113 mysql
-h localhost
-u " ${dbUser}" -p"${dbPassword}" -e "DROP DATABASE ${nextcloudDatabase} "
115 echo "Creating new DB for Nextcloud..."
116 mysql
-h localhost
-u " ${dbUser}" -p"${dbPassword}" -e "CREATE DATABASE ${nextcloudDatabase} "
118 echo "Restoring backup DB..."
119 mysql
-h localhost
-u " ${dbUser}" -p"${dbPassword}" "${nextcloudDatabase}" < "${currentRestoreDir}/${fileNameBackupDb} "
123 # Set directory permissions
125 echo "Setting directory permissions..."
126 chown
-R " ${webserverUser} " " ${nextcloudFileDir} "
127 chown
-R " ${webserverUser} " " ${nextcloudDataDir} "
133 echo "Starting nginx..."
138 # Restore hardened directory permissions
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"
155 # Disbale maintenance mode
157 echo "Switching off maintenance mode..."
158 cd " ${nextcloudFileDir} "
159 sudo
-u " ${webserverUser} " php occ maintenance
: mode
--off
164 # Update the system data-fingerprint (see https://docs.nextcloud.com/server/12/admin_manual/configuration_server/occ_command.html#maintenance-commands-label)
166 echo "Updating the system data-fingerprint..."
167 cd " ${nextcloudFileDir} "
168 sudo
-u " ${webserverUser} " php occ maintenance
: data-fingerprint
173 echo "Backup ${restore} successfully restored."
patrick-canterino.de