Nextcloud Backup and Restore

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. :wink:

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

7 Likes