0

I have a MYSQL database container running on a centos server. How do I automate backing up the database outside the container?

Philip Mutua
  • 6,016
  • 12
  • 41
  • 84
  • just use [volumes](https://docs.docker.com/storage/volumes/) to persist your database out side containers. also check this: https://stackoverflow.com/questions/39175194/docker-compose-persistent-data-mysql – ROOT Mar 18 '20 at 07:45

1 Answers1

0

as mentioned in the comments, make sure you use volumes for data folder.

for backing up:

  1. create a bash script on the host machine and make it executable:
#!/bin/bash
DATE=$(date '+%Y-%m-%d_%H-%M-%S')
docker exec <container name> /usr/bin/mysqldump -u <putdatabaseusername here> -p<PutyourpasswordHere> --all-databases > /<path to desired backup location>/$DATE.sql
if [[ $? == 0 ]]; then
    find /<path to desired backup location>/ -mtime +10 -exec rm {} \;
fi

change the following:

<container name> to actual db container name

<putdatabaseusername here> to db user

<PutyourpasswordHere> to the db password

create a directory for backup files and replace /<path to desired backup location>/ to the actual path

  1. create a cronjob on host machine that executes the script in desired time/period

note that this script will retain backups for 10 days, change the number to reflect your needs.

Important note: this script is storing the password in the file, use a secure way in production

Farhad Farahi
  • 35,528
  • 7
  • 73
  • 70