I didn’t go through the full process of setting this up, especially did not go through making a letsencrypt account, as I don’t have a domain to use, so I used a local domain (which don’t work with letsencrypt). But I believe I got most of what is necessary to start the backup and restore processes. Should be pretty easy… hopefully.
One thing to note: do not blindly copy-paste those, replace stuff under <> with your own.
First, here are the original setup steps, just for reference:
sudo apt update
sudo apt dist-upgrade
sudo apt install wget mariadb-server php php-apcu php-bcmath php-cli php-common php-curl php-gd php-gmp php-imagick php-intl php-mbstring php-mysql php-zip php-xml unzip python3-certbot-apache
wget https://download.nextcloud.com/server/releases/nextcloud-23.0.3.zip
sudo mysql_secure_installation
# enter
# y
# set pass
# y
# y
# y
# y
sudo mariadb
CREATE DATABASE nextcloud;
GRANT ALL PRIVILEGES ON nextcloud.* TO 'nextcloud'@'localhost' IDENTIFIED BY 'OURpassword';
FLUSH PRIVILEGES;
sudo phpenmod bcmath gmp imagick intl
unzip nextcloud*.zip
mv nextcloud ubu-nc.local
sudo chown -R www-data:www-data ubu-nc.local
sudo mv ubu-nc.local /var/www/
sudo a2dissite 000-default.conf
sudo systemctl reload apache2
sudo nvim /etc/apache2/sites-available/001-ubu-nc.local.conf
sudo nvim /etc/php/7.4/apache2/php.ini
# memory_limit = 512M
# upload_max_filesize = 200M
# max_execution_time = 360
# post_max_size = 200
# date.timezone = America/New_York
# opcache.enable=1
# opcache.interned_strings_buffer=8
# opcache.max_accelerated_files=10000
# opcache.memory_consumption=128
# opcache.save_comments=1
# opcache.revalidate_freq=1
sudo a2enmod dir env headers mime rewrite ssl
sudo systemctl restart apache2
# access nextcloud
# set a user and pass
# insert nextcloud mariadb user and pass / db name nextcloud / install recommended apps
sudo nvim /var/www/ubu-nc.local/config/config.php
# add:
# 'memcache.local' => '\0C\Memcache\APCu',
sudo chmod 660 /var/www/ubu-nc.local/config/config.php
sudo chown root:www-data /var/www/ubu-nc.local/config/config.php
sudo php /var/www/ubu-nc.local/occ db:add-missing-indices
sudo certbot --apache -d ubu-nc.local
Ok, all good thus far. Let’s create a new VM now. Ubuntu 20.04 comes with rsync by default, but if which rsync
returns nothing, do a sudo apt update && sudo apt install rsync
on both the Pi and the VM. After that, make sure you update both the Pi and the VM by running sudo apt update && sudo apt dist-upgrade
. I would say keeping them in-line is pretty important. Reboot both just to make sure everything is running the latest stuff.
On the RPi do the following:
sudo systemctl stop apache2
sudo mysqldump --all-databases > nextcloud.sql
sudo tar -cjvf nextcloud.tar.bz2 /etc/apache2 /etc/ssl /etc/letsencryptl /etc/php /var/www /home/<user>/nextcloud.sql
rsync nextcloud.tar.bz2 <user>@<VM>:/home/<user>/
# note: you may need to do step 1 and 2 on the VM to be able to transfer the archive through rsync
On the new VM, let’s do the following after you have created your user (refer to the original install for the adduser command and usermod and passwd and stuff):
sudo apt update
sudo apt install wget mariadb-server php php-apcu php-bcmath php-cli php-common php-curl php-gd php-gmp php-imagick php-intl php-mbstring php-mysql php-zip php-xml unzip openssh-server python3-certbot-apache
sudo mysql_secure_installation
# enter
# y
# set pass
# y
# y
# y
# y
sudo systemctl stop apache2
# ubuntu, why you always have to enable and start everything after they
# get installed, it's ridiculous and arguably a security liability!
tar -xjvf nextcloud.tar.bz2
sudo mv /etc/ssl /etc/ssl-old
sudo mv /etc/apache2 /etc/apache2-old
sudo mv /etc/letsencrypt /etc/letsencrypt-old
sudo mv /etc/php /etc/php-old
sudo mv etc/* /etc/
sudo rm -rf /home/<user>/etc
sudo mv var/www/ubu-nc.local /var/www/
sudo rm -rf /home/<user>/var/
sudo mariadb < nextcloud.sql
sudo systemctl start apache2
# optional in case everything works:
sudo rm -rf /etc/*-old
sudo rm -rf /home/<user>/nextcloud*
Technically, now you should have nextcloud back up and running. But you may need to run:
sudo phpenmod bcmath gmp imagick intl
sudo a2enmod dir env headers mime rewrite ssl
sudo php /var/www/ubu-nc.local/occ db:add-missing-indices
In theory, those should already be applied, as we copied the settings from /etc, but I’m not sure if there’s anything else lying in /var/lib/. It’s no harm if you run the command again after those have been applied, it will just ensure those settings are enabled.
Given the nature of this post, I deducted that you have no backups. Here’s how you do backups of nextcloud using the classic sysadmin method (again, I don’t know if nextcloud has an auto-backup option, but I highly doubt it given how it’s configured as a simple website, and even if it did, I don’t think it would create backups of the SSL certificates):
sudo mysqldump --all-databases > nextcloud.sql
sudo tar -cjvf nextcloud.tar.bz2 /etc/apache2 /etc/ssl /etc/letsencryptl /etc/php /var/www /home/<user>/nextcloud.sql
Now, you can use the commands above to automate backups, maybe put them in a crontab or use systemd timers. Keep in mind that this just saves files locally, so it’s best if you mount a NFS or SSHFS path and do the backups to that mount point, so that in case of a catastrophic storage failure, you get an actual backup of your files and configs. Use the same steps to restore from backup. This should not have an RTO of more than 10 minutes without the rsync / copy of the archive and extract time.