Dries Buytaert: Automatically exporting my Drupal content to GitHub

The two-day delay (--end-date="2 days ago") gives me time to catch typos before posts are archived to GitHub. I usually find them right after hitting publish.
ssh-keyscan github.com >> ~/.ssh/known_hosts

This note is mostly for my future self, in case I need to set this up again. I’m sharing it publicly because parts of it might be useful to others, though it’s not a complete tutorial since it relies on a custom Drupal module I haven’t released.

Giving your server access to GitHub

Create a new GitHub repository. I called mine website-content.
And yes, this note about automatically backing up my content will itself be automatically backed up.
On a traditional server, you’d add this script to Cron to run daily. My site runs on Acquia Cloud, which is Kubernetes-based and automatically scales pods up and down based on traffic. This means there is no single server to put a crontab on. Instead, Acquia Cloud provides a scheduler that runs jobs reliably across the infrastructure.
#!/bin/bash
set -e

TEMP=/tmp/dries-export

# Clone the existing repository
git clone git@github.com:dbuytaert/website-content.git $TEMP
cd $TEMP

# Clean all directories so moved/deleted content is tracked
rm -rf */

# Export all content older than 2 days
drush node:export --end-date="2 days ago" --destination=$TEMP

# Commit and push if there are changes
git config user.email "dries+bot@buytaert.net"
git config user.name "Dries Bot"
git add -A
git diff --staged --quiet || {
git commit -m "Automatic updates for $(date +%Y-%m-%d)"
git push
}

rm -rf $TEMP

The -N "" creates the key without a passphrase. For automated scripts on secured servers, passwordless keys are standard practice. The security comes from restricting what the key can do (a deploy key with write access to one repository) rather than from a passphrase.
Next, tell SSH to use this key when connecting to GitHub:
I created the following export script:
For context: I switched to Markdown and then open-sourced my blog content by exporting it to GitHub. Every day, my Drupal site exports its content as Markdown files and commits any changes to github.com/dbuytaert/website-content. New posts appear automatically, and so do edits and deletions.
ssh-keygen -t ed25519 -f ~/.ssh/github -N ""

For your server to push changes to GitHub automatically, you need SSH key authentication.

The export script

ssh -T git@github.com

Test that everything works:
The git add -A stages everything including deletions, so if I remove a post from my site, it disappears from GitHub too (though Git’s history preserves it).
cat ~/.ssh/github.pub

SSH into your server and generate a new SSH key pair:
cat >> ~/.ssh/config << 'EOF'
Host github.com
IdentityFile ~/.ssh/github
IdentitiesOnly yes
EOF

Add GitHub’s server fingerprint to your known hosts file. This prevents SSH from asking “Are you sure you want to connect?” when the script runs:

Similar Posts