Technitium DNS server zone high-availability

NextCloud Backup & Restore

If you are self-hosting your own NextCloud instance either locally or at public cloud like Oracle Cloud. It is a good idea to do regular backup for disaster recovery or future migration.

Follow below to setup schedule backup and restore your nextcloud instance.

1. Create nextcloud_backup.sh script to backup nextcloud and mysql db.

#!/bin/sh

bk_date=$(date "+%Y-%m-%d") # Current date
bk_source_path="/var/www" # Backup directory path
bk_source_dir="nextcloud" # Backup directory name
bk_dest="/home/ubuntu/backup" # Directory to store the backup
bk_retention="2" # Backup retention for housekeeping
db_user="nextcloud" # MySQL DB user
db_passwd="password123" # MySQL DB password
db_name="nextcloud" # MySQL DB name

# Check backup directory
if [ ! -d ${bk_dest} ]; then
  echo "Missing/wrong backup directory!"
  exit;
fi

# Keep how many of backup copy  
# Example +1 will keep latest 2 copies of backup  
# Uncomment below to enable backup housekeeping  
#find ${bk_dest} -name "*.tar.gz" -type f -mtime +${bk_retention} -delete -o -name "*.bak" -type f -mtime +${bk_retention} -delete  
#find ${bk_dest} -name "nextcloud*" -type f -mtime +${bk_retention} -delete  
 
# Stop apache2 service 
systemctl stop apache2

# MySQL/mariadb database backup
mysqldump --single-transaction --default-character-set=utf8mb4 -h localhost -u ${db_user} -p${db_passwd} ${db_name} > ${bk_dest}/nextcloud-sqlbkp_${bk_date}.bak 

# Backup nextcloud data directory
cd ${bk_source_path} && tar -zcvf ${bk_dest}/nextcloud-bak_${bk_date}.tar.gz ${bk_source_dir} 
#cd ${bk_source_path} && tar -zcvf ${bk_dest}/nextcloud-bak_${bk_date}.tar.gz ${bk_source_dir} > /dev/null 2>&1

# Re-start apache2 service
systemctl start apache2

# Set correct ownership. Replace the user and group ownership base on your setup.
chown ubuntu:ubuntu ${bk_dest}/nextcloud*

2. Create directory to store the backup and allow execution for nextcloud_backup.sh.

mkdir /home/ubuntu/backup
chmod +x /home/ubuntu/nextcloud_backup.sh

3. Create daily cron schedule backup job at 1AM, run schedule job with root permission. Optionally, you can manual backup by running the script as root.

echo 'MAILTO=""' | sudo tee /etc/cron.d/nextcloud_backup
echo '0 1 * * * root /path/nextcloud_backup.sh > /dev/null 2>&1' | sudo tee -a /etc/cron.d/nextcloud_backup

4. Use scp to download/sync your nextcloud backup to secondary backup location or another server for restoration.

# passwordless ssh login with ssl key
scp -r -i /path/ssl.key username@xxx.xxx.xxx.xxx:/path/backup /path/destination
 
# standard ssh with password
scp -r username@xxx.xxx.xxx.xxx:/path/backup /path/destination

5. To restore your nextcloud instance, setup another Ubuntu server with apache2 web server and mysql database.

6. Restore the nextcloud data directory to /var/www directory. Change directory ownership to www-data.

sudo tar –xvzf nextcloud-bak_XXX.tar.gz –C /var/www
sudo chown -R www-data:www-data /var/www/nextcloud

7. Create an empty mysql database for nextcloud. Make sure to use back the same database name and credential same as the original nextcloud setup.

# Delete any existing nextcloud database.
sudo mysql -u root -e "DROP DATABASE nextcloud"
 
# Create a new and empty nextcloud database.
sudo mysql -u root -e "CREATE DATABASE IF NOT EXISTS nextcloud CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci; GRANT ALL PRIVILEGES ON nextcloud.* TO nextcloud@localhost IDENTIFIED BY 'password123'";

8. If you want to change FQDN or IP of your nextcloud instance in the new setup, replace the old FQDN or IP in SQL backup file first before import the data. Remember to update your DNS entry for the new FQDN too.

sed -i 's/192.168.43.78/192.168.43.132/g' nextcloud-sqlbkp_xxx.bak

9. Import the nextcloud data to your new mysql database.

sudo mysql -u root nextcloud < /path/nextcloud-sqlbkp_xxxx.bak 

10. To double confirm data is properly imported, run SQL command to query your nextcloud database.

sudo mysql -u root -e "select * from nextcloud.oc_users;"

11. Access the nextcloud page again and login using your existing credential. Double check nextcloud's security and setup warning and fix it accordingly.


Comments