]>
git.p6c8.net - nextcloud-backup-restore.git/blob - NextcloudRestore.sh
4 # Bash script for restoring backups of Nextcloud.
5 # Usage: ./NextcloudRestore.sh <BackupName> (e.g. ./NextcloudRestore.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: The service name of the webserver. Used to start/stop webserver (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 webserver user
35 webserverUser
= "www-data"
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"
43 # Function for error messages
44 errorecho
() { cat <<< "$@" 1 >& 2 ; }
47 # Check if parameter given
51 errorecho
"ERROR: No backup name to restore given!"
52 errorecho
"Usage: NextcloudRestore.sh 'BackupDate'"
59 if [ "$(id -u)" != "0" ]
61 errorecho
"ERROR: This script has to be run as root!"
66 # Check if backup dir exists
68 if [ ! -d " ${currentRestoreDir} " ]
70 errorecho
"ERROR: Backup ${restore} not found!"
75 # Set maintenance mode
77 echo "Set maintenance mode for Nextcloud..."
78 cd " ${nextcloudFileDir} "
79 sudo
-u " ${webserverUser} " php occ maintenance
: mode
--on
87 echo "Stopping webserver..."
88 service
" ${webserverServiceName} " stop
93 # Delete old Nextcloud direcories
95 echo "Deleting old Nextcloud file directory..."
96 rm -r " ${nextcloudFileDir} "
97 mkdir
-p " ${nextcloudFileDir} "
101 echo "Deleting old Nextcloud data directory..."
102 rm -r " ${nextcloudDataDir} "
103 mkdir
-p " ${nextcloudDataDir} "
108 # Restore file and data directory
110 echo "Restoring Nextcloud file directory..."
111 tar -xpzf " ${currentRestoreDir}/${fileNameBackupFileDir}" -C "${nextcloudFileDir} "
115 echo "Restoring Nextcloud data directory..."
116 tar -xpzf " ${currentRestoreDir}/${fileNameBackupDataDir}" -C "${nextcloudDataDir} "
123 echo "Dropping old Nextcloud DB..."
124 mysql
-h localhost
-u " ${dbUser}" -p"${dbPassword}" -e "DROP DATABASE ${nextcloudDatabase} "
128 echo "Creating new DB for Nextcloud..."
129 mysql
-h localhost
-u " ${dbUser}" -p"${dbPassword}" -e "CREATE DATABASE ${nextcloudDatabase} "
133 echo "Restoring backup DB..."
134 mysql
-h localhost
-u " ${dbUser}" -p"${dbPassword}" "${nextcloudDatabase}" < "${currentRestoreDir}/${fileNameBackupDb} "
141 echo "Starting webserver..."
142 service
" ${webserverServiceName} " start
147 # Set directory permissions
149 echo "Setting directory permissions..."
150 chown
-R " ${webserverUser}":"${webserverUser}" "${nextcloudFileDir} "
151 chown
-R " ${webserverUser}":"${webserverUser}" "${nextcloudDataDir} "
156 # Update the system data-fingerprint (see https://docs.nextcloud.com/server/12/admin_manual/configuration_server/occ_command.html#maintenance-commands-label)
158 echo "Updating the system data-fingerprint..."
159 cd " ${nextcloudFileDir} "
160 sudo
-u " ${webserverUser} " php occ maintenance
: data-fingerprint
166 # Disbale maintenance mode
168 echo "Switching off maintenance mode..."
169 cd " ${nextcloudFileDir} "
170 sudo
-u " ${webserverUser} " php occ maintenance
: mode
--off
177 echo "Backup ${restore} successfully restored."
patrick-canterino.de