]>
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: 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
85 echo "Stopping nginx..."
91 # Delete old Nextcloud direcories
93 echo "Deleting old Nextcloud file directory..."
94 rm -r " ${nextcloudFileDir} "
95 mkdir
-p " ${nextcloudFileDir} "
99 echo "Deleting old Nextcloud data directory..."
100 rm -r " ${nextcloudDataDir} "
101 mkdir
-p " ${nextcloudDataDir} "
106 # Restore file and data directory
108 echo "Restoring Nextcloud file directory..."
109 tar -xpzf " ${currentRestoreDir}/${fileNameBackupFileDir}" -C "${nextcloudFileDir} "
113 echo "Restoring Nextcloud data directory..."
114 tar -xpzf " ${currentRestoreDir}/${fileNameBackupDataDir}" -C "${nextcloudDataDir} "
121 echo "Dropping old Nextcloud DB..."
122 mysql
-h localhost
-u " ${dbUser}" -p"${dbPassword}" -e "DROP DATABASE ${nextcloudDatabase} "
126 echo "Creating new DB for Nextcloud..."
127 mysql
-h localhost
-u " ${dbUser}" -p"${dbPassword}" -e "CREATE DATABASE ${nextcloudDatabase} "
131 echo "Restoring backup DB..."
132 mysql
-h localhost
-u " ${dbUser}" -p"${dbPassword}" "${nextcloudDatabase}" < "${currentRestoreDir}/${fileNameBackupDb} "
139 echo "Starting nginx..."
145 # Set directory permissions
147 echo "Setting directory permissions..."
148 chown
-R " ${webserverUser}":"${webserverUser}" "${nextcloudFileDir} "
149 chown
-R " ${webserverUser}":"${webserverUser}" "${nextcloudDataDir} "
154 # Update the system data-fingerprint (see https://docs.nextcloud.com/server/12/admin_manual/configuration_server/occ_command.html#maintenance-commands-label)
156 echo "Updating the system data-fingerprint..."
157 cd " ${nextcloudFileDir} "
158 sudo
-u " ${webserverUser} " php occ maintenance
: data-fingerprint
164 # Disbale maintenance mode
166 echo "Switching off maintenance mode..."
167 cd " ${nextcloudFileDir} "
168 sudo
-u " ${webserverUser} " php occ maintenance
: mode
--off
175 echo "Backup ${restore} successfully restored."
patrick-canterino.de