Skip to content

Commit

Permalink
Merge pull request #9 from ColoredCow/feat/continous-delivery
Browse files Browse the repository at this point in the history
script to export civicrm
  • Loading branch information
pokhiii committed May 31, 2024
2 parents ad1d46a + 0c78498 commit 3459ff4
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 0 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/import-staging.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Import CiviCRM - Staging
on:
push:
branches: main

jobs:
import:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: SSH and Import
uses: appleboy/ssh-action@master
env:
SSH_BUILD_DIRECTORY: ${{ secrets.SSH_BUILD_DIRECTORY }}
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USERNAME }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
envs: SSH_BUILD_DIRECTORY
script: |
cd $SSH_BUILD_DIRECTORY
git checkout -f main
git pull origin main
./bin/import_civicrm.sh
39 changes: 39 additions & 0 deletions bin/export_civicrm.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/bin/bash
set -e

# Determine the script's directory and infer the project root directory
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"

EXPORT_DIR_NAME="wp-content/civi-exports"
EXPORT_DIR_PATH="$PROJECT_ROOT/$EXPORT_DIR_NAME"
TIMESTAMP=$(date +%s)
ZIP_FILE="${TIMESTAMP}-export.zip"

# Create the exports directory if it doesn't exist
mkdir -p $EXPORT_DIR_PATH

echo "Backing up..."
echo "wp civicrm core backup --backup-dir=$EXPORT_DIR_PATH/civicrm-backup --yes"
wp civicrm core backup --backup-dir=$EXPORT_DIR_PATH/civicrm-backup --yes
echo "Backup complete"

cd $EXPORT_DIR_PATH

cd civicrm-backup

sed -i.bak 's/DEFINER=`[^`]*`@`localhost`/DEFINER=`civicrm`@`localhost`/g' civicrm-db.sql

rm civicrm.zip

cd ..

echo "zip -r $ZIP_FILE civicrm-backup"

zip -r $ZIP_FILE civicrm-backup

rm -rf civicrm-backup

cd $PROJECT_ROOT

echo "Export ${ZIP_FILE} saved successfully to ${EXPORT_DIR_PATH}"
31 changes: 31 additions & 0 deletions bin/import_civicrm.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/bash
set -e

# Determine the script's directory and infer the project root directory
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"

EXPORT_DIR_NAME="wp-content/civi-exports"
EXPORT_DIR_PATH=$PROJECT_ROOT/$EXPORT_DIR_NAME

cd $EXPORT_DIR_NAME

# Get the latest zip file by timestamp
ZIP_FILE=$(ls -t *-export.zip 2>/dev/null | head -n 1)


# Check if there is any zip file
if [ -z "$ZIP_FILE" ]; then
echo "No export zip files found in ${EXPORT_DIR_NAME}."
exit 0
fi

# Unzip the latest zip file
unzip -o $ZIP_FILE

cd ..

# Use yes to automate the confirmation prompt
yes y | wp civicrm core restore --backup-dir=$EXPORT_DIR_PATH/civicrm-backup --yes

echo "Imported ${ZIP_FILE} successfully"
Binary file added wp-content/civi-exports/1717137938-export.zip
Binary file not shown.

0 comments on commit 3459ff4

Please sign in to comment.