Backup and Restore the Nextcloud Using Docker
Backing up and restoring your database properly is essential to maintaining the integrity of your Nextcloud instance. Below is a straightforward procedure to back up your PostgreSQL database and restore it inside a Docker container, as recommended by the official Nextcloud documentation.
Creating a Backup of the PostgreSQL Database
The following command creates an SQL dump of the nextcloud database and saves it to a file named with the current date:
PGPASSWORD="strongpassword" pg_dump nextcloud -h nextcloud -U nextcloud -f nextcloud-sqlbkp_$(date +"%Y%m%d").bak
- PGPASSWORD: temporarily sets the password for the nextcloud user.
- pg_dump: the official PostgreSQL tool for exporting data.
- -h nextcloud: the database host (can be the name of the container if using Docker networking).
- -f: specifies the output file name with today’s date.
Restoring the Backup in a Docker Container
Before restoring, we must drop and recreate the database to ensure a clean restoration without conflicts from existing data.
- Access PostgreSQL with the administrative user:
psql -U nextcloud -d postgres
- Then, execute the following commands inside the psql prompt:
DROP DATABASE nextcloud;
CREATE DATABASE nextcloud OWNER nextcloud;
\q
- Now restore the backup using Docker:
docker exec -i nextcloud_postgres bash -c 'PGPASSWORD="strongpassword" psql -U nextcloud -d nextcloud' < nextcloud-sqlbkp_20250624.bak
This command streams the contents of the .bak file into the psql process inside the nextcloud_postgres container.
Official References
For more details on how to perform complete backups and restores in Nextcloud, refer to:
Conclusion
With these commands, you can ensure a reliable backup and recovery routine for your Nextcloud PostgreSQL database running in Docker. Consider automating this process using cron jobs and versioning your backups for extra peace of mind.