LINUX, FOSS AND LIBRARY TECHNOLOGY ENTHUSIAST

Sunday, October 20, 2024

Effortless Backup and Restore for Docker Services

0 comments
Managing Docker containers for critical applications like websites or microservices can be a smooth experience with a few well-planned strategies. However, it’s crucial to ensure you have a reliable backup and restore process in place. In this post, I will walk you through how to back up and restore Docker services using docker-compose. We'll use a real-world example of backing up and restoring a Docker-based "homepage" service.

Backing up and restoring a Docker container like MariaDB involves a few other steps

Why Backup & Restore?

Backups are essential to protect your application data and configurations. In case of data loss, corruption, or errors due to updates, backups provide the ability to revert to a previous working state quickly. Docker services often contain volumes or configurations that need to be preserved. Without a proper backup system, losing such data could be disastrous.

Backup Procedure for Docker Services

Let's assume you have a Docker service named "homepage" and you want to back up its data.

Step 1: Stop the Docker Containers

Before creating a backup, it's always a good idea to stop the running containers. This ensures that no write operations are occurring during the backup process.

Navigate to the directory where the docker-compose file for the 'homepage' service is located. I organize all my Docker Compose files (docker-compose.yml) in separate directories within the 'docker' directory, and then run docker compose up -d from the respective directory.

cd /home/mahesh/docker/homepage

Stop the Docker containers for the 'homepage' service

docker-compose stop

Stopping the containers ensures data integrity during the backup process.

Step 2: Create a Backup Archive

Once the containers are stopped, you can create a compressed archive of your Docker project directory.

Navigate to your home directory

cd /home/mahesh/

Create a compressed backup archive of the 'homepage' service directory with a timestamp

tar -czvf docker-backup-$(date +'%F').tar.gz /home/mahesh/docker/homepage

The $(date +'%F') command dynamically adds the current date to the backup file name, making it easier to manage multiple backups.

Step 3: Restart Docker Containers

After the backup process is complete, you can restart the Docker containers.

Navigate back to your docker-compose directory

cd /home/mahesh/docker/homepage

Start the containers

docker-compose start

Your service should be back up and running, and you now have a full backup of the Docker service, ready to restore if needed.

Restore Procedure for Docker Services

Restoring the service from a backup is simple and requires only a few steps.

Step 1: Install your docker container in my case , homepage


Step 1: Stop the Docker Containers

Before restoring, stop the running containers to avoid conflicts.

Navigate to the docker-compose directory

cd /home/mahesh/docker/homepage

Stop the running containers

docker-compose stop

Step 2: Remove the Current Files

To ensure a clean restore, remove all the current files in the homepage directory.

Remove all files and directories in the 'homepage' folder

rm -rf /home/mahesh/docker/homepage/*

Step 3: Extract the Backup Archive

Once the directory is cleared, extract the backup archive to the original location.

Extract the backup archive (replace YYYY-MM-DD with the actual date of the backup)

tar -xzvf /home/mahesh/docker-backup-YYYY-MM-DD.tar.gz -C /

Ensure you replace YYYY-MM-DD with the correct date of the backup file you are restoring.

Step 4: Start the Docker Containers

Now, restart the containers to bring the service back online.

Navigate to the docker-compose directory

cd /home/mahesh/docker/homepage

Start the containers

docker-compose start

Your service is now restored to the state it was in when the backup was created.

Best Practices for Docker Backup and Restore
  • Automate Backups: Use cron jobs or similar scheduling tools to automate backups. You can run the backup script at regular intervals.
  • Store Backups Safely: Keep your backup archives in a safe, redundant location. Consider using cloud storage or remote servers

No comments:

Post a Comment