Skip to content

Instantly share code, notes, and snippets.

@alexisrobert
Last active April 8, 2019 10:23
Show Gist options
  • Save alexisrobert/b8addeba619dfba3d840 to your computer and use it in GitHub Desktop.
Save alexisrobert/b8addeba619dfba3d840 to your computer and use it in GitHub Desktop.
Backup script for Dokku (postgres & storage & let's encrypt & glacier)
#!/bin/bash
BACKUP_ROOT="/root/backup/"
BACKUP_DATE="`date +'%Y%m%d-%H%M%S'`"
BACKUP_FTP_ROOT="/tmp/glacier-${BACKUP_DATE}"
mkdir -p $BACKUP_FTP_ROOT
DAYS_RETENTION=30
DUPLICITY_PATH=""
PASSPHRASE=""
echo "Backup root : ${BACKUP_ROOT}"
echo "Backup FTP root : ${BACKUP_FTP_ROOT}"
echo "=> Let's encrypt renewal..."
dokku letsencrypt:auto-renew
echo "=> Storage..."
for app in `dokku apps:list|tail -n '+2'` ; do
while read -r line ; do
vol_info=$(echo $line | awk '{print $1}' | cut -d: -f1)
vol_dest=$(echo $line | awk '{print $1}' | cut -d: -f2 | sed 's/\/$//' | sed 's/\//_/g')
echo "- $app ($vol_info) -> $BACKUP_DATE$vol_dest.tar"
mkdir -p "$BACKUP_ROOT/volume/$app/"
mkdir -p "$BACKUP_FTP_ROOT/volume/$app/"
tar cf $BACKUP_ROOT/volume/$app/$BACKUP_DATE$vol_dest.tar $vol_info
cp $BACKUP_ROOT/volume/$app/$BACKUP_DATE$vol_dest.tar "$BACKUP_FTP_ROOT/volume/$app/volume.tar"
done < <(dokku storage:list $app |tail -n +2)
done
echo "=> Postgresql..."
for db in `dokku postgres:list |tail -n '+2' | awk '{print $1}'`; do
echo "- $db -> $BACKUP_DATE.db"
mkdir -p "$BACKUP_ROOT/postgres/$db/"
mkdir -p "$BACKUP_FTP_ROOT/postgres/$db/"
dokku postgres:export $db > "$BACKUP_ROOT/postgres/$db/$BACKUP_DATE.db"
cp "$BACKUP_ROOT/postgres/$db/$BACKUP_DATE.db" "$BACKUP_FTP_ROOT/postgres/$db/database.db"
done
echo "=> Removing stale files ($DAYS_RETENTION days retention)..."
OLD=$(find $BACKUP_ROOT -type f -mtime +$DAYS_RETENTION)
if [[ -n "$OLD" ]] ; then
echo "- Deleting `echo $OLD | wc -l` files ..."
echo $OLD | xargs rm -fv
fi
echo "=> Sending to Duplicity..."
export PASSPHRASE
duplicity remove-older-than 6M --force $DUPLICITY_PATH
duplicity incr --asynchronous-upload --full-if-older-than 1M --allow-source-mismatch $BACKUP_FTP_ROOT $DUPLICITY_PATH
unset PASSPHRASE
rm -rf $BACKUP_FTP_ROOT
echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment