]>
git.p6c8.net - nextcloud-backup-restore.git/blob - NextcloudRestore.sh
4 # Bash script for restoring backups of Nextcloud.
6 # - With backup directory specified in the script: ./NextcloudRestore.sh <BackupName> (e.g. ./NextcloudRestore.sh 20170910_132703)
7 # - With backup directory specified by parameter: ./NextcloudRestore.sh <BackupName> <BackupDirectory> (e.g. ./NextcloudRestore.sh 20170910_132703 /media/hdd/nextcloud_backup)
9 # 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/
14 # You have to customize this script (directories, users, etc.) for your actual environment.
15 # All entries which need to be customized are tagged with "TODO".
22 if [ -z " $backupMainDir " ]; then
23 # TODO: The directory where you store the Nextcloud backups (when not specified by args)
24 backupMainDir
= "/mnt/hdd1/nextcloudb_backup"
27 echo "Backup directory: $backupMainDir "
29 currentRestoreDir
= " ${backupMainDir} / ${restore} "
31 # TODO: The directory of your Nextcloud installation (this is a directory under your web root)
32 nextcloudFileDir
= "/var/www/nextcloud"
34 # TODO: The directory of your Nextcloud data directory (outside the Nextcloud file directory)
35 # If your data directory is located under Nextcloud's file directory (somewhere in the web root), the data directory should not be restored separately
36 nextcloudDataDir
= "/var/nextcloud_data"
38 # TODO: The directory of your Nextcloud's local external storage.
39 # Uncomment if you use local external storage.
40 #nextcloudLocalExternalDataDir="/var/nextcloud_external_data"
42 # TODO: The service name of the web server. Used to start/stop web server (e.g. 'systemctl start <webserverServiceName>')
43 webserverServiceName
= "nginx"
45 # TODO: Your Nextcloud database name
46 nextcloudDatabase
= "nextcloud_db"
48 # TODO: Your Nextcloud database user
49 dbUser
= "nextcloud_db_user"
51 # TODO: The password of the Nextcloud database user
52 dbPassword
= "mYpAsSw0rd"
54 # TODO: Your web server user
55 webserverUser
= "www-data"
57 # File names for backup files
58 # If you prefer other file names, you'll also have to change the NextcloudBackup.sh script.
59 fileNameBackupFileDir
= "nextcloud-filedir.tar.gz"
60 fileNameBackupDataDir
= "nextcloud-datadir.tar.gz"
62 # TOOD: Uncomment if you use local external storage
63 #fileNameBackupExternalDataDir="nextcloud-external-datadir.tar.gz"
65 fileNameBackupDb
= "nextcloud-db.sql"
67 # Function for error messages
68 errorecho
() { cat <<< "$@" 1 >& 2 ; }
71 # Check if parameter(s) given
73 if [ $# != "1" ] && [ $# != "2" ]
75 errorecho
"ERROR: No backup name to restore given, or wrong number of parameters!"
76 errorecho
"Usage: NextcloudRestore.sh 'BackupDate' ['BackupDirectory']"
83 if [ "$(id -u)" != "0" ]
85 errorecho
"ERROR: This script has to be run as root!"
90 # Check if backup dir exists
92 if [ ! -d " ${currentRestoreDir} " ]
94 errorecho
"ERROR: Backup ${restore} not found!"
99 # Set maintenance mode
101 echo "Set maintenance mode for Nextcloud..."
102 sudo
-u " ${webserverUser} " php
${nextcloudFileDir} / occ maintenance
: mode
--on
109 echo "Stopping web server..."
110 systemctl stop
" ${webserverServiceName} "
115 # Delete old Nextcloud direcories
119 echo "Deleting old Nextcloud file directory..."
120 rm -r " ${nextcloudFileDir} "
121 mkdir
-p " ${nextcloudFileDir} "
126 echo "Deleting old Nextcloud data directory..."
127 rm -r " ${nextcloudDataDir} "
128 mkdir
-p " ${nextcloudDataDir} "
132 # Local external storage
133 # TOOD: Uncomment if you use local external storage
134 #echo "Deleting old Nextcloud local external storage directory..."
135 #rm -r "${nextcloudLocalExternalDataDir}"
136 #mkdir -p "${nextcloudLocalExternalDataDir}"
141 # Restore file and data directory
145 echo "Restoring Nextcloud file directory..."
146 tar -xmpzf " ${currentRestoreDir}/${fileNameBackupFileDir}" -C "${nextcloudFileDir} "
151 echo "Restoring Nextcloud data directory..."
152 tar -xmpzf " ${currentRestoreDir}/${fileNameBackupDataDir}" -C "${nextcloudDataDir} "
156 # Local external storage
157 # TOOD: Uncomment if you use local external storage
158 #echo "Restoring Nextcloud data directory..."
159 #tar -xmpzf "${currentRestoreDir}/${fileNameBackupExternalDataDir}" -C "${nextcloudLocalExternalDataDir}"
166 echo "Dropping old Nextcloud DB..."
168 mysql
-h localhost
-u " ${dbUser}" -p"${dbPassword}" -e "DROP DATABASE ${nextcloudDatabase} "
170 # PostgreSQL (uncomment if you are using PostgreSQL as Nextcloud database)
171 #sudo -u postgres psql -c "DROP DATABASE ${nextcloudDatabase};"
175 echo "Creating new DB for Nextcloud..."
177 # Use this if the databse from the backup uses UTF8 with multibyte support (e.g. for emoijs in filenames):
178 mysql
-h localhost
-u " ${dbUser}" -p"${dbPassword}" -e "CREATE DATABASE ${nextcloudDatabase} CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci"
179 # Use this if the database from the backup DOES NOT use UTF8 with multibyte support (e.g. for emoijs in filenames):
180 #mysql -h localhost -u "${dbUser}" -p"${dbPassword}" -e "CREATE DATABASE ${nextcloudDatabase}"
182 # PostgreSQL (uncomment if you are using PostgreSQL as Nextcloud database)
183 #sudo -u postgres psql -c "CREATE DATABASE ${nextcloudDatabase} WITH OWNER ${dbUser} TEMPLATE template0 ENCODING \"UTF8\";"
187 echo "Restoring backup DB..."
189 mysql
-h localhost
-u " ${dbUser}" -p"${dbPassword}" "${nextcloudDatabase}" < "${currentRestoreDir}/${fileNameBackupDb} "
191 # PostgreSQL (uncomment if you are using PostgreSQL as Nextcloud database)
192 #sudo -u postgres psql "${nextcloudDatabase}" < "${currentRestoreDir}/${fileNameBackupDb}"
199 echo "Starting web server..."
200 systemctl start
" ${webserverServiceName} "
205 # Set directory permissions
207 echo "Setting directory permissions..."
208 chown
-R " ${webserverUser}":"${webserverUser}" "${nextcloudFileDir} "
209 chown
-R " ${webserverUser}":"${webserverUser}" "${nextcloudDataDir} "
210 # TOOD: Uncomment if you use local external storage
211 #chown -R "${webserverUser}":"${webserverUser}" "${nextcloudLocalExternalDataDir}"
216 # Update the system data-fingerprint (see https://docs.nextcloud.com/server/16/admin_manual/configuration_server/occ_command.html#maintenance-commands-label)
218 echo "Updating the system data-fingerprint..."
219 sudo
-u " ${webserverUser} " php
${nextcloudFileDir} / occ maintenance
: data-fingerprint
224 # Disbale maintenance mode
226 echo "Switching off maintenance mode..."
227 sudo
-u " ${webserverUser} " php
${nextcloudFileDir} / occ maintenance
: mode
--off
233 echo "Backup ${restore} successfully restored."
patrick-canterino.de