Last active
October 17, 2022 21:02
-
-
Save Genarito/5e23a4129fc023f0c62af11d2304c0dd to your computer and use it in GitHub Desktop.
Backups the Docker volume, updates the Postgres version and finally restores the data in the new Postgres version
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Backup the data. Taken from https://stackoverflow.com/a/67568671/7058363 | |
docker volume create --name backup_volume | |
docker container run --rm -it -v [old volume]:/from -v backup_volume:/to alpine ash -c "cd /from ; cp -av . /to" | |
# Export Postgres data | |
docker exec -t [name of DB container] pg_dump [db name] | gzip > exported_postgres_data.sql.gz | |
# MANUAL: stop the Docker container to which the volume is attached... | |
# Remove the old volume to prevent issues starting Postgres service (with the new version) on the same volume | |
docker volume rm [old volume] | |
# If needed, create the empty volume again | |
docker volume create --name="[old volume]" | |
# MANUAL: now you can update Posgres version in docker-compose.yml file or K8S service... | |
# Import data | |
zcat exported_postgres_data.sql.gz | docker exec -i [name of DB container] psql [db name] | |
# If everything went well, remove the backup volume and the exported file | |
docker volume rm backup_volume | |
rm ./exported_postgres_data.sql.gz |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment