]>
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-18-04-lts-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 mainBackupDir
= "/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 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"
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 web server..."
88 systemctl stop
" ${webserverServiceName} "
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 -xmpzf " ${currentRestoreDir}/${fileNameBackupFileDir}" -C "${nextcloudFileDir} "
115 echo "Restoring Nextcloud data directory..."
116 tar -xmpzf " ${currentRestoreDir}/${fileNameBackupDataDir}" -C "${nextcloudDataDir} "
123 echo "Dropping old Nextcloud DB..."
125 mysql
-h localhost
-u " ${dbUser}" -p"${dbPassword}" -e "DROP DATABASE ${nextcloudDatabase} "
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}\";"
132 echo "Creating new DB for Nextcloud..."
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}"
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}\";"
144 echo "Restoring backup DB..."
146 mysql
-h localhost
-u " ${dbUser}" -p"${dbPassword}" "${nextcloudDatabase}" < "${currentRestoreDir}/${fileNameBackupDb} "
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}"
156 echo "Starting web server..."
157 systemctl start
" ${webserverServiceName} "
162 # Set directory permissions
164 echo "Setting directory permissions..."
165 chown
-R " ${webserverUser}":"${webserverUser}" "${nextcloudFileDir} "
166 chown
-R " ${webserverUser}":"${webserverUser}" "${nextcloudDataDir} "
171 # Update the system data-fingerprint (see https://docs.nextcloud.com/server/15/admin_manual/configuration_server/occ_command.html#maintenance-commands-label)
173 echo "Updating the system data-fingerprint..."
174 sudo
-u " ${webserverUser} " php
${nextcloudFileDir} / occ maintenance
: data-fingerprint
179 # Disbale maintenance mode
181 echo "Switching off maintenance mode..."
182 sudo
-u " ${webserverUser} " php
${nextcloudFileDir} / occ maintenance
: mode
--off
188 echo "Backup ${restore} successfully restored."
patrick-canterino.de