My solution is to get use of the new docker cp, which is now able to copy data out from containers, not matter if it’s running or not and share a host volume to the exact same location where the database application is creating its database files inside the container. This double solution works without a data-only container, straight from the original database container.
So my systemd init script is taking the job of backuping the database into an archive on the host. I placed a timestamp in the filename to never rewrite a file.
It’s doing it on the ExecStartPre:
ExecStartPre=-/usr/bin/docker cp lanti-debian-mariadb:/var/lib/mysql /home/core/sql ExecStartPre=-/bin/bash -c ‘/usr/bin/tar -zcvf /home/core/sql/sqlbackup_$$(date +%%Y-%%m-%%d_%%H-%%M-%%S)_ExecStartPre.tar.gz /home/core/sql/mysql –remove-files’
And it is doing the same thing on ExecStopPost too:
ExecStopPost=-/usr/bin/docker cp lanti-debian-mariadb:/var/lib/mysql /home/core/sql ExecStopPost=-/bin/bash -c ‘tar -zcvf /home/core/sql/sqlbackup_$$(date +%%Y-%%m-%%d_%%H-%%M-%%S)_ExecStopPost.tar.gz /home/core/sql/mysql –remove-files’
Plus I exposed a folder from the host as a volume to the exact same location where the database is stored:
mariadb: build: ./mariadb volumes: – $HOME/server/mysql/:/var/lib/mysql/:rw
It works great on my VM (I building a LEMP stack for myself): https://github.com/DJviolin/LEMP
But I just don’t know if is it a “bulletproof” solution when your life depends on it actually (for example, webshop with transactions in any possible miliseconds)?
At 20 min 20 secs from this official Docker keynote video, the presenter does the same thing with the database:
“For the database we have a volume, so we can make sure that, as the database goes up and down, we don’t loose data, when the database container stopped.”