]>
git.p6c8.net - nextcloud-backup-restore.git/blob - NextcloudRestore.sh
3903e8aa973459be939a94ebe0f29b953789bec8
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_ackups"
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 given
75 errorecho
"ERROR: No backup name to restore given!"
76 errorecho
"Usage: NextcloudRestore.sh 'BackupDate'"
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 cd " ${nextcloudFileDir} "
103 sudo
-u " ${webserverUser} " php occ maintenance
: mode
--on
111 echo "Stopping web server..."
112 systemctl stop
" ${webserverServiceName} "
117 # Delete old Nextcloud direcories
121 echo "Deleting old Nextcloud file directory..."
122 rm -r " ${nextcloudFileDir} "
123 mkdir
-p " ${nextcloudFileDir} "
128 echo "Deleting old Nextcloud data directory..."
129 rm -r " ${nextcloudDataDir} "
130 mkdir
-p " ${nextcloudDataDir} "
134 # Local external storage
135 # TOOD: Uncomment if you use local external storage
136 #echo "Deleting old Nextcloud local external storage directory..."
137 #rm -r "${nextcloudLocalExternalDataDir}"
138 #mkdir -p "${nextcloudLocalExternalDataDir}"
143 # Restore file and data directory
147 echo "Restoring Nextcloud file directory..."
148 tar -xmpzf " ${currentRestoreDir}/${fileNameBackupFileDir}" -C "${nextcloudFileDir} "
153 echo "Restoring Nextcloud data directory..."
154 tar -xmpzf " ${currentRestoreDir}/${fileNameBackupDataDir}" -C "${nextcloudDataDir} "
158 # Local external storage
159 # TOOD: Uncomment if you use local external storage
160 #echo "Restoring Nextcloud data directory..."
161 #tar -xmpzf "${currentRestoreDir}/${fileNameBackupExternalDataDir}" -C "${nextcloudLocalExternalDataDir}"
168 echo "Dropping old Nextcloud DB..."
170 mysql
-h localhost
-u " ${dbUser}" -p"${dbPassword}" -e "DROP DATABASE ${nextcloudDatabase} "
172 # PostgreSQL (uncomment if you are using PostgreSQL as Nextcloud database)
173 #sudo -u postgres psql -c "DROP DATABASE ${nextcloudDatabase};"
177 echo "Creating new DB for Nextcloud..."
179 # Use this if the databse from the backup uses UTF8 with multibyte support (e.g. for emoijs in filenames):
180 mysql
-h localhost
-u " ${dbUser}" -p"${dbPassword}" -e "CREATE DATABASE ${nextcloudDatabase} CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci"
181 # Use this if the database from the backup DOES NOT use UTF8 with multibyte support (e.g. for emoijs in filenames):
182 #mysql -h localhost -u "${dbUser}" -p"${dbPassword}" -e "CREATE DATABASE ${nextcloudDatabase}"
184 # PostgreSQL (uncomment if you are using PostgreSQL as Nextcloud database)
185 #sudo -u postgres psql -c "CREATE DATABASE ${nextcloudDatabase} WITH OWNER ${dbUser} TEMPLATE template0 ENCODING \"UTF8\";"
189 echo "Restoring backup DB..."
191 mysql
-h localhost
-u " ${dbUser}" -p"${dbPassword}" "${nextcloudDatabase}" < "${currentRestoreDir}/${fileNameBackupDb} "
193 # PostgreSQL (uncomment if you are using PostgreSQL as Nextcloud database)
194 #sudo -u postgres psql "${nextcloudDatabase}" < "${currentRestoreDir}/${fileNameBackupDb}"
201 echo "Starting web server..."
202 systemctl start
" ${webserverServiceName} "
207 # Set directory permissions
209 echo "Setting directory permissions..."
210 chown
-R " ${webserverUser}":"${webserverUser}" "${nextcloudFileDir} "
211 chown
-R " ${webserverUser}":"${webserverUser}" "${nextcloudDataDir} "
212 # TOOD: Uncomment if you use local external storage
213 #chown -R "${webserverUser}":"${webserverUser}" "${nextcloudLocalExternalDataDir}"
218 # Update the system data-fingerprint (see https://docs.nextcloud.com/server/15/admin_manual/configuration_server/occ_command.html#maintenance-commands-label)
220 echo "Updating the system data-fingerprint..."
221 sudo
-u " ${webserverUser} " php
${nextcloudFileDir} / occ maintenance
: data-fingerprint
226 # Disbale maintenance mode
228 echo "Switching off maintenance mode..."
229 sudo
-u " ${webserverUser} " php
${nextcloudFileDir} / occ maintenance
: mode
--off
235 echo "Backup ${restore} successfully restored."
patrick-canterino.de