install restic as root
sudo -i
wget https://github.com/restic/restic/releases/download/v0.9.4/restic_0.9.4_linux_amd64.bz2
bunzip2 restic_0.9.4_linux_amd64.bz2
mv restic_0.9.4_linux_amd64 /usr/local/bin/restic
chmod 0755 /usr/local/bin/restic
mount the backup directory to /mnt/nc-backup
set two environment variables
export RESTIC_REPOSITORY="/var/nc-backup"
export RESTIC_PASSWORD="oJSesetLzRgphy9UwmWxi50oE0dkYa16"
initialize restic
restic init
create a backup script /usr/local/bin/backup_nextcloud.sh
#!/bin/bash
export RESTIC_REPOSITORY="/mnt/nc-backup"
export RESTIC_PASSWORD="oJSesetLzRgphy9UwmWxi50oE0dkYa16"
# abort entire script if any command fails
set -e
# Make sure nextcloud is enabled when we are done
trap "sudo -u www-data php /var/www/nextcloud/occ maintenance:mode --off" EXIT
# set nextcloud to maintenance mode
sudo -u www-data php /var/www/nextcloud/occ maintenance:mode --on
# unlock restic
/usr/local/bin/restic unlock
# backup the database
sudo -u postgres pg_dump -c -U postgres nextcloud_db | /usr/local/bin/restic backup --stdin --stdin-filename db_postgres_nextcloud.sql
# backup the data dir
/usr/local/bin/restic backup /mnt/nc-data /var/www/nextcloud
# turn maintenance mode off
sudo -u www-data php /var/www/nextcloud/occ maintenance:mode --off
# delete trap
trap "" EXIT
# clean up backup dir
/usr/local/bin/restic forget --keep-daily 7 --keep-weekly 5 --keep-monthly 12 --keep-yearly 75
/usr/local/bin/restic prune
you may want to add other files and directories to the line
# backup the data dir
/usr/local/bin/restic backup /mnt/nc-data /var/www/nextcloud
e.g. the apache2 config
for more infos about restic look here: https://restic.readthedocs.io/en/latest
restore (single file): you βmountβ the backups to /mnt/restore with
mkdir /mnt/restore
export RESTIC_REPOSITORY="/var/nc-backup"
export RESTIC_PASSWORD="oJSesetLzRgphy9UwmWxi50oE0dkYa16"
/usr/local/bin/restic mount /mnt/restore
now you can navigate through the backups and look for the file you want to restore and copy it back to /mnt/nc-data/<username>/files
/mnt
βββ hosts
β βββ ip-172-31-68-249
β βββ 2019-04-14T07:29:31Z
β β βββ db_postgres_nextcloud.sql
β βββ 2019-04-14T07:29:32Z
β β βββ var
β β βββ nc-data
β βββ 2019-04-14T07:29:33Z
β β βββ var
β β βββ www
β βββ latest -> 2019-04-14T07:29:33Z
βββ ids
β βββ 0d0ba44a
β β βββ var
β β βββ nc-data
β β βββ admin
β β βββ appdata_ocjb4w2lzfyj
β β βββ audit.log
β β βββ files_external
β β βββ index.html
β β βββ nextcloud.log
β βββ 401c5e8e
β β βββ var
β β βββ www
β β βββ nextcloud
β βββ 827fbdde
β βββ db_postgres_nextcloud.sql
βββ snapshots
β βββ 2019-04-14T07:29:31Z
β β βββ db_postgres_nextcloud.sql
β βββ 2019-04-14T07:29:32Z
β β βββ var
β β βββ nc-data
β β βββ admin
β β βββ appdata_ocjb4w2lzfyj
β β βββ audit.log
β β βββ files_external
β β βββ index.html
β β βββ nextcloud.log
β βββ 2019-04-14T07:29:33Z
β β βββ var
β β βββ www
β β βββ nextcloud
β βββ latest -> 2019-04-14T07:29:33Z
βββ tags
donβt forget to run
sudo -u www-data php /var/www/nextcloud/occ file:scan --all
full restore:
install nextcloud and restic
stop postgres, redis, php, apache2
delete /mnt/nc-data and /var/www/nextcloud
find the snapshot id of the latest backup restic snapshots
restore the directories restic restore <snapshot-id>
or restic restore latest --target /
restore the database psql nextcloud_db < db_postgres_nextcloud.sql
restart everything or reboot
more or less like this. you should try and exercise this on a new maschine a couple of times.
this has nothing to do with nextcloud. to find a good backup and recovery strategy is a general task in IT.
p.s.: I wouldnβt backup to a mounted file system. because it wouldnβt cover sudo rm -rf /
you should look if can use sftp or the restic REST server to connect to your freenas.
further reading: http://plone.4aero.com/Members/lmarzke/talks/restic/
and to setup a test env for restore: β GitHub - ReinerNippes/nextcloud: Ansible playbook to install nextcloud, php, nginx or apache, mariadb or postgres, redis-server, onlyoffice or collabora office