Docker container image to simplify and automate backup of a self-hosted nextcloud service.
10K+
Refer to https://gitlab.com/clewsy/ncbu
Once configured, this docker container automates backup of a nextcloud instance (data and database) and also provides a simple way to restore from said backup.
The following commands will build the container image on the local host (assuming git and docker are installed):
$ git clone https://gitlab.com/clewsy/ncbu
$ cd ncbu
$ docker build -t registry.gitlab.com/clewsy/ncbu .
Or the image can be pulled directly from the GitLab container registry:
$ docker pull registry.gitlab.com/clewsy/ncbu
The GitLab container registry is recommended, but alternatively the image can be pulled directly from dockerhub:
$ docker pull clewsy/ncbu
The container can be created fom the command line, for example:
$ docker run \
--name nextcloud-bu \
--env NEXTCLOUD_CONTAINER=nextcloud \
--env NEXTCLOUD_DATABASE_CONTAINER=nextcloud-db \
--env NEXTCLOUD_BACKUP_CRON="0 0 * * *" \
--volume /etc/localtime:/etc/localtime:ro \
--volume /var/run/docker.sock:/var/run/docker.sock:ro \
--volume nextcloud-app:/mnt/nextcloud-app \
--volume nextcloud-db:/mnt/nextcloud-db \
--volume /home/docker/nextcloud-bu:/backup \
--detach \
registry.gitlab.com/clewsy/ncbu
However, this backup method is intended to be implemented with a docker-compose.yml file within which additional containers are configured for nextcloud and a database. The ncbu container can be configured with the following parameters:
The following example docker-compose.yml file is configured in such a way that the nextcloud and database (mariadb) containers use docker to manage their volumes. The ncbu container (nextcloud-bu) will therefore sync both of these Docker-managed volumes to ./nextcloud-bu/nextcloud_app and ./nextcloud-bu/nextcloud_db respectively. The backup in this example will occur every day at 0100hrs.
Notes:
$ cat ./nextcloud-bu/ncbu.log.version: '3'
services:
######################################### Nginx-proxy container
nginx-proxy:
image: nginxproxy/nginx-proxy:alpine
container_name: nginx-proxy
networks:
- your.site_network
ports:
- 80:80
- 443:443
volumes:
- ./nginx-proxy/conf.d:/etc/nginx/conf.d:rw
- ./nginx-proxy/vhost.d:/etc/nginx/vhost.d
- ./nginx-proxy/html:/usr/share/nginx/html
- ./nginx-proxy/dhparam:/etc/nginx/dhparam
- ./nginx-proxy/certs:/etc/nginx/certs:ro
- /var/run/docker.sock:/tmp/docker.sock:ro
- /etc/localtime:/etc/localtime:ro
restart: unless-stopped
######################################### Nginx-proxy-acme-companion container
nginx-proxy-acme:
image: nginxproxy/acme-companion
container_name: nginx-proxy-acme
networks:
- your.site_network
depends_on:
- nginx-proxy
environment:
- NGINX_PROXY_CONTAINER=nginx-proxy
volumes:
- ./nginx-proxy/vhost.d:/etc/nginx/vhost.d
- ./nginx-proxy/html:/usr/share/nginx/html
- ./nginx-proxy/certs:/etc/nginx/certs
- ./nginx-proxy/acme.sh:/etc/acme.sh
- /var/run/docker.sock:/var/run/docker.sock:ro
- /etc/localtime:/etc/localtime:ro
restart: unless-stopped
######################################### MariaDB database (for nextcloud) container
nextcloud-db:
image: linuxserver/mariadb
container_name: nextcloud-db
networks:
- your.site_network
environment:
- PUID=1000
- PGID=1000
- MYSQL_ROOT_PASSWORD=${MARIADB_NEXTCLOUD_MYSQL_ROOT_PASSWORD}
- MYSQL_USER=nextcloud
- MYSQL_PASSWORD=${MARIADB_NEXTCLOUD_MYSQL_PASSWORD}
- MYSQL_DATABASE=nextcloud
volumes:
- nextcloud-db:/config
- /etc/localtime:/etc/localtime:ro
restart: unless-stopped
######################################### Nextcloud web app container
nextcloud:
image: nextcloud
container_name: nextcloud
networks:
- your.site_network
depends_on:
- nginx-proxy
- nginx-proxy-acme
- nextcloud-db
environment:
- OVERWRITEPROTOCOL=https
- VIRTUAL_PORT=80
- VIRTUAL_HOST=${NEXTCLOUD_URL}
- LETSENCRYPT_HOST=${NEXTCLOUD_URL}
- LETSENCRYPT_EMAIL=${LETSENCRYPT_EMAIL}
volumes:
- nextcloud-app:/var/www/html
- /etc/localtime:/etc/localtime:ro
restart: unless-stopped
######################################### Nextcloud cronjob container (for periodically running cron.php)
nextcloud-cron:
image: rcdailey/nextcloud-cronjob
container_name: nextcloud-cron
network_mode: none
depends_on:
- nextcloud
environment:
- NEXTCLOUD_CONTAINER_NAME=nextcloud
- NEXTCLOUD_CRON_MINUTE_INTERVAL=5
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
- /etc/localtime:/etc/localtime:ro
restart: unless-stopped
######################################### Nextcloud backup container (for periodically copying data and database)
nextcloud-bu:
image: registry.gitlab.com/clewsy/ncbu
container_name: nextcloud-bu
network_mode: none
depends_on:
- nextcloud
- nextcloud-db
environment:
- NEXTCLOUD_EXEC_USER=www-data # Name of the user that can execute the occ command in the nextcloud container (www-data by default).
- NEXTCLOUD_CONTAINER=nextcloud # Name of the nextcloud container.
- NEXTCLOUD_DATABASE_CONTAINER=nextcloud-db # Name of the nextcloud database container.
- NEXTCLOUD_BACKUP_CRON=0 0 * * * # Run at midnight.
volumes:
- /var/run/docker.sock:/var/run/docker.sock # Allows container to access another container.
- /etc/localtime:/etc/localtime:ro # Use to sync time so that the crond runs as expected.
- nextcloud-app:/mnt/nextcloud_app # Must match the docker-managed nextcloud app volume (/var/www/html).
- nextcloud-db:/mnt/nextcloud_db # Must match the docker-managed nextcloud database volume (/var/lib/mysql).
- ./nextcloud-bu:/backup # Convenient location for the backup.
restart: unless-stopped
######################################### Docker-managed volumes
volumes:
nextcloud-app:
nextcloud-db:
######################################### Docker-managed networks
networks:
your.site_network:
The backup script (ncbu) can be run manually from within the container. Alternatively, it can be run at any time with the following docker exec command:
$ docker exec nextcloud-bu ncbu
If different, change nextcloud-bu to the appropriate container name.
The process to restore nextcloud and the associated database from backups used the restore script (ncbu_restore) as follows:
$ docker-compose up -d
$ docker exec nextcloud-bu ncbu_restore
$ docker-compose down
$ docker-compose up -d
A healthcheck script (ncbu_healthcheck) is executed every ten minutes. To determine the status/health of a running container, use the command:
$ docker ps
The output will include a Status column. Here the ncbu contaioner should be noted as healthy if all is well. An unhealthy status means one of two things:
Output of the various scripts is logged to a file within the container's /backup directory. The logfile will also include the verbose output from the rsync commands.
In the example yml file above, since the container's /backup directory is mapped to ./nextcloud-bu, this is where the logfile can be read on the host machine.
The ncbu and ncbu_restore scripts also trigger the logrotate command. The logfile will be rotated and archived if the file size exceeds 1M. In this scenario the logfile can be viewed from the host machine by simply using the cat command. Example logs shown below:
$ cat /home/docker/nextcloud-bu/ncbu.log
2021-06-18 00:00:00 - Running ncbu (nextcloud backup)...
2021-06-18 00:00:00 - Putting nextcloud into maintenance mode...
2021-06-18 00:00:00 - Nextcloud data backup: Syncing nextcloud volume to /backup...
2021/06/18 00:00:00 [35874] building file list
2021/06/18 00:00:01 [35874] >f..t...... config/config.php
2021/06/18 00:00:02 [35874] >f.st...... data/nextcloud.log
2021/06/18 00:00:02 [35874] >f.st...... data/appdata_oc037zsrujze/appstore/apps.json
2021/06/18 00:00:02 [35874] .d..t...... data/appdata_oc037zsrujze/preview/0/6/c/d/4/1/6/
...
2021/06/18 00:00:06 [35874] sent 67.01M bytes received 44.01K bytes 10.32M bytes/sec
2021/06/18 00:00:06 [35874] total size is 34.27G speedup is 511.07
2021-06-18 00:00:06 - Finished nextcloud data sync.
2021-06-18 00:00:06 - Setting permission of nextcloud data directory to :33
2021-06-18 00:00:07 - Ensure user on host machine is part of group id GID=33 for read access to backup.
2021-06-18 00:00:07 - Nextcloud database backup (physical copy): Syncing nextcloud-db volume to /backup...
2021/06/18 00:00:07 [35901] building file list
2021/06/18 00:00:07 [35901] >f..t...... databases/ib_logfile0
2021/06/18 00:00:08 [35901] >f..t...... databases/ib_logfile1
...
2021/06/18 00:00:10 [35901] >f.st...... log/mysql/mariadb-bin.index
2021/06/18 00:00:10 [35901] sent 386.56M bytes received 436 bytes 110.45M bytes/sec
2021/06/18 00:00:10 [35901] total size is 878.59M speedup is 2.27
2021-06-18 00:00:10 - Finished nextcloud database sync.
2021-06-18 00:00:10 - Taking nextcloud out of maintenance mode...
2021-06-18 00:00:10 - Rotating logfile if required...
2021-06-18 00:00:10 - All done.
Content type
Image
Digest
Size
18.5 MB
Last updated
almost 5 years ago
docker pull clewsy/ncbu