|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +CONTAINER=jami_db |
| 4 | +SERVICE_NAME=db |
| 5 | +DB_NAME=postgres |
| 6 | + |
| 7 | +# JAMI_DB=jami-db |
| 8 | + |
| 9 | +# current_date=$(date +"%Y%m%d") |
| 10 | +# JAMI_FILE_NAME="jami_${current_date}.sql.gz" |
| 11 | + |
| 12 | +FOLDER_NAME="jami" |
| 13 | + |
| 14 | +PROJECT_ROOT=$(dirname $(dirname $(realpath "$0"))) |
| 15 | +BACKUPS=$PROJECT_ROOT/backups/ |
| 16 | +DOCKER_COMPOSE=$PROJECT_ROOT/docker-compose.yml |
| 17 | + |
| 18 | +S3_CONF=~/.s3cfg |
| 19 | + |
| 20 | +retry () { |
| 21 | + for i in {1..60} |
| 22 | + do |
| 23 | + $@ && break || echo "Retry attempt $i failed, waiting..." && sleep 3; |
| 24 | + done |
| 25 | +} |
| 26 | + |
| 27 | +if [ ! -f "$S3_CONF" ]; then |
| 28 | + echo "" |
| 29 | + echo "!! No config file for s3 bucket !!" |
| 30 | + echo "Create file for path ~/.s3cfg and copy the credetials from version.helsinki.fi" |
| 31 | + echo "" |
| 32 | + return 0 |
| 33 | +fi |
| 34 | + |
| 35 | +echo "Creating backups folder" |
| 36 | +mkdir -p ${BACKUPS} |
| 37 | + |
| 38 | +echo "Listing available backups in S3 bucket..." |
| 39 | +backup_files=$(s3cmd -c "$S3_CONF" ls "s3://psyduck/${FOLDER_NAME}/" | awk '{print $4}' | grep '\.sql\.gz$') |
| 40 | + |
| 41 | +if [ -z "$backup_files" ]; then |
| 42 | + echo "No backup files found in S3 bucket!" |
| 43 | + exit 1 |
| 44 | +fi |
| 45 | + |
| 46 | +echo "Available backups:" |
| 47 | +select chosen_backup in $backup_files; do |
| 48 | + if [ -n "$chosen_backup" ]; then |
| 49 | + echo "You selected: $chosen_backup" |
| 50 | + FILE_NAME=$(basename "$chosen_backup") |
| 51 | + break |
| 52 | + else |
| 53 | + echo "Invalid selection. Please select a valid backup number." |
| 54 | + fi |
| 55 | +done |
| 56 | + |
| 57 | +echo "Fetching the selected dump: $FILE_NAME" |
| 58 | +s3cmd -c "$S3_CONF" get "$chosen_backup" "$BACKUPS" |
| 59 | + |
| 60 | +if [ ! -f "${BACKUPS}${FILE_NAME}" ]; then |
| 61 | + echo "Download failed or file not found: ${BACKUPS}${FILE_NAME}" |
| 62 | + exit 1 |
| 63 | +fi |
| 64 | + |
| 65 | +echo "Removing database and related volume" |
| 66 | +docker-compose -f $DOCKER_COMPOSE down -v |
| 67 | + |
| 68 | +echo "Starting postgres in the background" |
| 69 | +docker-compose -f $DOCKER_COMPOSE up -d $SERVICE_NAME |
| 70 | + |
| 71 | +retry docker-compose -f $DOCKER_COMPOSE exec $SERVICE_NAME pg_isready --dbname=$DB_NAME |
| 72 | + |
| 73 | +echo "Populating ${FOLDER_NAME}" |
| 74 | +docker exec -i $CONTAINER /bin/bash -c "gunzip | psql -U postgres" < ${BACKUPS}${FILE_NAME} |
0 commit comments