Skip to content

Instantly share code, notes, and snippets.

@MegaphoneJon
Created February 5, 2025 21:45
Show Gist options
  • Select an option

  • Save MegaphoneJon/0d270a87b852a743b4dec90228fbf10e to your computer and use it in GitHub Desktop.

Select an option

Save MegaphoneJon/0d270a87b852a743b4dec90228fbf10e to your computer and use it in GitHub Desktop.
This is not really generalized but I'm keeping it here in case I need this again.
#!/bin/bash
#Using `export` so it's available to borg
export REPOSITORY="[email protected]:your-reponame"
export BORG_PASSPHRASE='your passphrase'
export REMOTE_PATH=/usr/local/bin/borg1/borg1
TEMPDIR=/tmp/sqldump
MYSQL_CREDS=`/usr/bin/pwd`/mysqlcreds.cnf
#change this to "full" or "fast" to set the default. Is overridden if specified on the command line.
SYNC_TYPE=fast
SYNC_DB=true
SYNC_FILES=true
ECHO=/usr/bin/echo
print_help() {
$ECHO ""
$ECHO "Restore Civi/CMS backups from rsync.net"
$ECHO ""
$ECHO " --help Display this help screen"
$ECHO " --full Restore the entire database"
$ECHO " --fast For faster restores, strip out big but rarely used in dev Civi tables."
$ECHO " --no-db Do not sync the databases."
$ECHO " --no-files Do not sync the file system."
}
while test -n "$1"; do
case "$1" in
--help|-h)
print_help
exit $STATE_OK
;;
--full)
SYNC_TYPE=full
shift
;;
--fast)
SYNC_TYPE=fast
shift
;;
--no-db)
SYNC_DB=false
shift
;;
--no-files)
SYNC_FILES=false
shift
;;
*)
$ECHO "Unknown argument: $1"
print_help
exit $STATE_UNKNOWN
esac
shift
done
$ECHO sync type: $SYNC_TYPE
$ECHO "Start:" `/usr/bin/date`
if [ "$SYNC_DB" = "true" ]; then
LAST_BACKUP=`/usr/bin/borg list --glob-archives=* --last 1 --short --remote-path /usr/local/bin/borg1/borg1 $REPOSITORY`
/usr/bin/mkdir -p $TEMPDIR
/usr/bin/rm -f $TEMPDIR/*sql.gz $TEMPDIR/*sql
$ECHO "restoring from backup: $LAST_BACKUP"
cd $TEMPDIR/.. && /usr/bin/borg extract --progress --strip-components 3 --remote-path /usr/local/bin/borg1/borg1 --pattern '+*sql.gz' $REPOSITORY::$LAST_BACKUP var/backups/mysql/sqldump/
$ECHO "Decompressing backups"
/usr/bin/gunzip --force $TEMPDIR/*.sql.gz
# Remove big tables on a fast restore
if [ "$SYNC_TYPE" = "fast" ]; then
$ECHO "Fast restore - removing tables for faster import"
/usr/bin/sed -i -E '/-- Dumping data for table `(log_civicrm\w+|civicrm_mailing_event\w+|civicrm_(job_)?log|civicrm_mailing_recipients)`/,/UNLOCK TABLES/d' $TEMPDIR/live_civi.sql
fi
# Remove trigger definers
/usr/bin/perl -pi -e 's#\/\*\!5001. DEFINER=`.*`@`.*?\*\/##g; s/CREATE DEFINER.*FUNCTION/CREATE FUNCTION/g' $TEMPDIR/live_civi.sql
$ECHO "Restoring Drupal..."
/usr/bin/pv $TEMPDIR/live_drup.sql | mysql --defaults-file=$MYSQL_CREDS dev_drup
$ECHO "Restoring Civi..."
/usr/bin/pv $TEMPDIR/live_civi.sql | mysql --defaults-file=$MYSQL_CREDS dev_civi
fi
if [ "$SYNC_FILES" = "true" ]; then
$ECHO "Syncing files"
rsync -a --delete-after /var/www/live/web/* /var/www/dev/web
rsync -a --delete-after /var/www/live/private_files/* /var/www/dev/private_files
fi
$ECHO Finish: `/usr/bin/date`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment