Last active
September 1, 2017 19:47
-
-
Save osvalr/76752e7cd1ea8f23506040d8ed401d0a to your computer and use it in GitHub Desktop.
Restore a psql dump in a randomed named database inside a container
This file contains 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 | |
# Tested in archlinux | |
function restore_2_container() { | |
# Container name | |
container_name=$1 | |
# Random database name | |
database_name="`shuf -n1 /usr/share/dict/cracklib-small`_`shuf -n1 /usr/share/dict/cracklib-small`" | |
if [ -z $container_name ]; then | |
echo "Container name missing" | |
return 1 | |
fi | |
if ! [ -f database_dump.sql ]; then | |
echo "database_dump.sql Missing: is `pwd` is a backup directory?" | |
return 1 | |
fi | |
start_db="docker exec -t ${container_name} bash -c 'service postgresql start'" | |
eval $start_db | |
echo "Copying backup and filestore" | |
cp_and_restore="docker cp filestore ${container_name}:/home/odoo/data_dir/filestore/${database_name} && docker cp database_dump.sql ${container_name}:/home/odoo/ && docker exec -it ${container_name} bash -c 'createdb $database_name && psql $database_name < /home/odoo/database_dump.sql'" | |
eval $cp_and_restore | |
echo "Updating odoo user permissions" | |
update_permissions="docker exec --user root -t ${container_name} bash -c 'chown odoo. -R ~/data_dir/filestore/${database_name}'" | |
eval $update_permissions | |
echo "Restored database_dump.sql in ${container_name} with the DB name: '${database_name}'" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment