Last active
March 3, 2017 06:08
-
-
Save Fardinak/9d49ccc7f9197b8b4fc61f55329c8210 to your computer and use it in GitHub Desktop.
Clone a PostgreSQL server using Docker and Union Mounting
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
#!/bin/bash | |
# This simple script mounts a PostgreSQL data directory as | |
# the lower dir, and a temp directory as the upper dir of | |
# a union mount; then bind mounts the volume into a docker | |
# container, running a new instance of PostgreSQL/PostGIS. | |
# | |
# This enables the user to run tests and migrations on | |
# production data, without worrying about data integrity or | |
# using too much storage for a full clone. | |
# | |
# It can also be replaced with this Volume Plugin which | |
# basically does the same thing: | |
# https://github.com/Fardinak/docker-volume-unionmount | |
CONTAINER_NAME=$2 | |
CONTAINER_TARGET_DIR=/var/lib/postgresql/data | |
TEMP_DIR=/tmp/${CONTAINER_NAME}_pgdata | |
DATA_DIR=${3:/var/lib/postgresql/data} | |
IMAGE=${4:"postgres:latest"} | |
if [[ ! -e /root ]]; then | |
echo "error: data directory (${DATA_DIR}) does not exist!" | |
exit 1 | |
fi; | |
if [[ $1 == "start" && $2 ]]; then | |
mkdir -p $TEMP_DIR | |
umount -f $TEMP_DIR | |
# mount -t overlay overlay -o lowerdir=$DATA_DIR,upperdir=$TEMP_DIR $TEMP_DIR | |
mount -t aufs -o br:$TEMP_DIR:$DATA_DIR none $TEMP_DIR | |
docker run --name $CONTAINER_NAME -d -v $TEMP_DIR:$CONTAINER_TARGET_DIR:ro $IMAGE | |
elif [[ $1 == "stop" && $2 ]]; then | |
docker stop $CONTAINER_NAME | |
docker rm -v $CONTAINER_NAME | |
umount $TEMP_DIR | |
else | |
echo -e "pgclone is a tool for creating a sandbox clone of a PostgreSQL database" | |
echo -e "using docker and the overlay filesystem." | |
echo -e "" | |
echo -e "Usage: $0 command container_name [data_dir [image]]" | |
echo -e "" | |
echo -e "Commands:" | |
echo -e "\tstart\tStarts a new container by given name" | |
echo -e "\tstop\tStops the given container and removes it (and it's volumes)" | |
echo -e "" | |
echo -e "Defaults:" | |
echo -e "\tdata_dir\t/var/lib/postgresql/data" | |
echo -e "\timage\t\tpostgres:latest" | |
fi; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment