Skip to content

Instantly share code, notes, and snippets.

@guyromm
Created March 20, 2020 05:05
Show Gist options
  • Save guyromm/f953c0321be935158009e667ef1b990c to your computer and use it in GitHub Desktop.
Save guyromm/f953c0321be935158009e667ef1b990c to your computer and use it in GitHub Desktop.
incrementally periodically dump a postgresql database to google drive using borg, zstandard & rclone
export BORG_PASSCOMMAND='/bin/echo CHANGEME'
export DBNAME='PGDATABASETOBACKUP'
export DRIVENAME='RCLONEDRIVENAME' # must be in ~/.config/rclone/rclone.conf
export TDIR='TARGETDIRWITHINRCLONEDRIVE'
export MNTFOLDER=~/mnt/$DRIVENAME # where to locally temporarily mount the rclone drive
export DFN="pg_dump/data-"$(hostname)"-"$(date +'%Y-%m-%d').sql
function mnt() {
mkdir -p $MNTFOLDER && rclone mount --allow-non-empty --daemon --vfs-cache-mode full $DRIVENAME:/ $MNTFOLDER && \
#echo 'sleeping 5' && \
sleep 5
}
function umnt() {
#echo 'unmounting' && \
fusermount -u $MNTFOLDER
}
function restore_gdrive() {
mnt && \
restore $1 $2
umnt
}
function restore() {
borg extract $MNTFOLDER/$TDIR::$1 --stdout $2
}
function list_gdrive() {
mnt && \
borg list $MNTFOLDER/$TDIR
umnt
}
function borg_init() {
borg init -e repokey $MNTFOLDER/$TDIR
}
function do_dump() {
pg_dump $DBNAME | borg create --compression zstd -v --stats $MNTFOLDER/$TDIR/::$(hostname)"-"$(date +'%Y-%m-%d')"$1" -
}
function rclone_sync() {
rclone sync -v $MNTFOLDER/$TDIR $DRIVENAME:/$TDIR
}
#RCLONEPID=$!
function run() {
echo 'mounting remote backup folder' && \
echo 'initializing backup repo' && \
borg_init ||:
echo 'dumping & backuping' && \
do_dump "$1" && \
# echo 'cleaning up' && \
# rm $DFN && \
echo 'rclone sync' && \
rclone_sync && \
echo 'all done!'
}
if [[ "$(basename -- "$0")" == "pg_backup.sh" ]]; then
echo "RUNNING ; $0";
run $1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment