PaRaD1SE

Record a Docker-Postgres Database Migration

Published: 2022/2/26 Views: 6962

Categories: 

Development

Smart use of intermediate data volumes to achieve data migration between containers and hosts

docker_postgresql.png

Dockerfile multi-stage build

Recently, when I was tearing the source code of Gitea by hand, I found that there is such a fragment in the Dockerfile of the project:


After some searching, found that docker supports multi-stage builds. The so-called multi-stage construction is in a Dockerfile, which can easily use various ready-made integrated environments to compile the files required by the final running environment by building an intermediate image. The final environment can be very clean, only the files needed for the final run, without the need to install the compilation environment.

Docker version is too low and CentOS system stops maintenance

I finished modifying my Dockerfile, and at the same time refactored my website project structure, and put the front-end and back-end into a code repository. After pushing to the server, I unexpectedly found that my project could not be built normally because my Docker version was too low. And from my version update to the latest Docker version, I need to uninstall the old version and then install the new one, the data of my website is in the Docker data volume, it is impossible to directly reinstall. There will not be good results. Just recently CentOS announces that it is out of maintenance. There will be more and more bugs in a system that stops maintenance, and the software support rate will be lower and lower. So I decided to change to a Ubuntu server.

Wild O&M Road

Replacing a server means data backup and recovery, unlike in ordinary systems, dragging data in and out of a USB flash drive is called backup and recovery. In Docker, data is placed in the Docker data volume and dynamically mounted to the specified container when the container starts. How to get it out of the data volume and put it back in is an art. The general process can be divided into, firstly package and compress the static files,

backup.sh


For database files, do not copy them directly, but use the backup and restore commands corresponding to the database. I am using PostgreSQL.


Once the files are backed up, you can use the scp command to transfer the files directly to the new server


Don't worry, you can copy it to your computer first.


Of course, to run these two commands, you need to set up the login key pair first.

After transferring the backup file, you can finally restore the data on the new server. After building the container on the new server, start the container in a hurry. After the container is started, it is inevitable that new data will be generated, especially the database. After the container is started, it is likely to cause conflicts.

restore.sh


Then start the container again, and it will feel exactly the same as on the old server!

Optimize the process and enjoy life

In practice, these scripts are all stored in files, which can be executed directly, or the specific logic of the script can be written by using the Makefile of GNUMake. Some people will say Makefile what is it? If the .sh file is used to execute linux shell commands together, then Makefile is a more fancy shell command executor. You can write files to make a series of stinky and long shell commands to do what you want in just one or two words. It's time to finally have a cup of coffee.

Tags:

Docker
Development
Website
PostgreSQL

Previous

Next