Update docker-image-test.yml #9
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
name: Test Backup | |
on: | |
push: | |
branches: | |
- main # Change this to your default branch if needed | |
pull_request: | |
branches: | |
- main # Change this to your default branch if needed | |
jobs: | |
test: | |
runs-on: ubuntu-latest | |
services: | |
postgres: | |
image: postgres:14 | |
env: | |
POSTGRES_USER: your_db_user | |
POSTGRES_PASSWORD: your_db_password | |
POSTGRES_DB: your_database_name | |
ports: | |
- 5432:5432 | |
options: >- | |
--health-cmd "pg_isready -U your_db_user" | |
--health-interval 10s | |
--health-timeout 5s | |
--health-retries 5 | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v2 | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v1 | |
- name: Build Docker Image | |
run: docker build -t pg_backup_image . | |
- name: Create Sample Database and Insert Data | |
run: | | |
echo "Creating sample database and inserting data..." | |
until pg_isready -h localhost -U your_db_user; do | |
echo "Waiting for PostgreSQL to be ready..." | |
sleep 5 | |
done | |
# Set PGPASSWORD to allow the psql command to connect | |
export PGPASSWORD=your_db_password | |
psql -h localhost -U your_db_user -d your_database_name -c "CREATE TABLE test_table (id SERIAL PRIMARY KEY, name VARCHAR(50));" | |
psql -h localhost -U your_db_user -d your_database_name -c "INSERT INTO test_table (name) VALUES ('Test Data 1'), ('Test Data 2');" | |
- name: Set CRONTIME | |
run: echo "CRONTIME='* * * * *'" >> $GITHUB_ENV | |
- name: Make backup mount point | |
run: mkdir ~/backup | |
- name: Start Cron Job | |
run: | | |
echo "Setting up cron job..." | |
docker run -d \ | |
--name pg_backup_container \ | |
-e PGUSER=your_db_user \ | |
-e PGDATABASE=your_database_name \ | |
-e PGPASSWORD=your_db_password \ | |
-e PGHOST=localhost \ | |
-e CRONTIME="${CRONTIME}" \ | |
-v ~/backup:/backup \ | |
pg_backup_image | |
echo "Waiting 2 minutes for cron job to run..." | |
sleep 90 # Wait for 90 seconds to allow the cron job to execute | |
# see if there is anything in the backup directory | |
- name: Check for Backups | |
run: | | |
ls ~/backup | |
- name: Verify Backup | |
run: | | |
echo "Verifying backup..." | |
# Check if the backup file exists (old ref docker run --rm pg_backup_image) | |
BACKUP_FILE=$(ls ~/backup | head -n 1) | |
echo "Found backup file: $BACKUP_FILE" # Log the found file name | |
if [ -z "$BACKUP_FILE" ]; then | |
echo "No backup file found!" | |
exit 1 | |
fi | |
echo "Verifying contents of $BACKUP_FILE..." | |
# Restore the backup and verify | |
docker run --rm \ | |
-e PGUSER=your_db_user \ | |
-e PGPASSWORD=your_db_password \ | |
-e PGDATABASE=your_database_name \ | |
-e PGHOST=localhost \ | |
pg_backup_image sh -c "pg_restore -d your_database_name -U your_db_user /backup/$BACKUP_FILE && psql -h localhost -U your_db_user -d your_database_name -c 'SELECT * FROM test_table;'" |