Bobcares

Nextcloud Docker Backup Restore: How to?

by | Oct 13, 2022

 Let us learn more about the Nextcloud docker backup restore and how to set up the backup. At Bobcares our Docker hosting support services can guide you through the process.

Nextcloud using docker backup and update

nextcloud docker backup restore

We can backup and update a Nextcloud instance using docker compose. We have to consider some of the elements that make them important. Here are a few examples:

  • Firstly, the failure of the Raspberry Pi.
  • A hacker or a program attacking the system.
  • Deleting data by accident or on purpose as a result of an application or human mistake.
  • Natural causes or disasters.

We can set up and select the location, Retention time, and when to backup. This improves data security.

Backup the docker volume

To back up a Docker data volume, start a new container. Start it with the volume to run the backup command. The data for the volume is available in the Docker data list. However, the Docker daemon should handle the volumes. Let us now go through the necessary settings to set up the backup.

After that,  we have to confirm the SSH public key. Create an SSH keypair and submit the public key to the remote server to log in without the login details.

Docker image for the container

For the next step to set up the Nextcloud docker backup restore we require a Docker image. The image should include the following tools: OpenSSH and rsync. As a result, we use the Alpine Docker image and install the packages. The Dockerfile is as follows:

FROM alpine:latest
RUN apk update && apk add openssh rsync

Docker-compose

After that, we must define the data volume and the command to use the actual backup. We can do this with a lengthy command line statement, however, it is better to use docker compose for clarity.

We can create the docker compose yml file in the same folder as the Dockerfile. Follow the command lines given below to set this up:

version: '3'
services:
backup-ncdata:
build: .
volumes:
- ncdata:/data:ro
- /home/pi/.ssh/id_rsa:/root/.ssh/id_rsa:ro
command: rsync -axh -e "ssh -p 22 -o StrictHostKeyChecking=no"
--rsync-path="rsync
--fake-super"
--links --delete
--info=progress2 "/data/" "user@abcd.com:ncdata" volumes: ncdata: external: true

After that, to back up the volume ncdata, we have to define a single service backup ncdata.

  • build. This shows that the service image is to create from the Dockerfile in the same folder.
  • ncdata:/data:ro. Read only volume ncdata scale in the container at /data to backup.
  • /home/pi/.ssh/id_rsa:/root/.ssh/id_rsa:ro : Use the host’s SSH private key.
  • command: ...". This is for backing up the data with rsync. The options are present in the rsync document.
  • --rsync-path="rsync --ab-cd". This is critical to keep the owner and group of a file. We can do this if there are no root functions on the remote server.
  • --info=progress2. Finally, this command line will show the whole progress.
  • Finally, to execute the backup, enter the following command in the docker compose file’s folder:
 run docker-compose backup-ncdata —rm.

After the backup, the —rm option removes the container.

Backup script

It is better to enable the repair mode during backup to avoid data issues or errors. Everything is present in the shell script backup NextCloud sh. Replace home pi backup with the location of the Dockerfile and docker compose file.

#!/bin/bash
# Enable maintenance mode
docker exec -i nextcloudpi sudo -u www-data php /var/www/nextcloud/occ maintenance:mode --on
# Backup the ncdata docker volume
# Full path to docker-compose, otherwise cron job fails
/usr/local/bin/docker-compose -f /home/pi/backup/docker-compose.yml run --rm backup-ncdata

# Disable maintenance mode docker exec -i nextcloudpi sudo -u www-data php /var/www/nextcloud/occ maintenance:mode --off.

Cron job

To execute the backup every night, open the crontab using crontab -e. Add the following line to begin the backup at 3 a.m. every day:

0 3 * * * /home/pi/backup/backup-nextcloud.sh

Restore

We don’t have one unless we restore the data from the backup. It will reverse the whole procedure. Restore the data to a docker volume. After that use the volume to launch a new NextcloudPi. Build a new volume by running docker volume:

create name ncdata restore.

Here is the docker compose yml  file to restore the two ncdata services. The restore service copies the data to a Docker volume. The nextcloudpi service will execute Nextcloud with the recovered data.

version: '3'
services:
restore-ncdata:
build:
volumes:
- ncdata-restore:/data
- /home/pi/.ssh/id_rsa:/root/.ssh/id_rsa:ro
command: rsync -axh -e "ssh -p 22 -o StrictHostKeyChecking=no" --rsync-path="rsync --fake-super" --links --delete --info=progress2 "user@abcd.com:ncdata/" "/data"


nextcloudpi:
image: ownyourbits/nextcloudpi-armhf
command: "192.168.2.212"
ports:
- "802:80"
- "4432:443"
- "44432:4443"
volumes:
- ncdata-restore:/data
- /etc/localtime:/etc/localtime:ro container_name: nextcloudpi-restored restart: unless-stopped

volumes:
ncdata-restore:
external: true

After that step, Docker-compose run rm restore ncdata will restore the data. Then, docker compose up -d a new NextcloudPi with the recovered data.

[Need assistance with similar queries? We are here to help]

Conclusion

To sum up, we have now learned how to set up the NextCloud docker backup restore. Need help with docker? Get in touch with our Docker Hosting support services.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie
_clck, _clsk, CLID, ANONCHK, MR, MUID, SM

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

_reb2bgeo - The visitor's geographical location

_reb2bloaded - Whether or not the script loaded for the visitor

_reb2bref - The referring URL for the visit

_reb2bsessionID - The visitor's RB2B session ID

_reb2buid - The visitor's RB2B user ID

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid
_reb2bgeo, _reb2bloaded, _reb2bref, _reb2bsessionID, _reb2buid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF