Skip to content

Instantly share code, notes, and snippets.

@dt
Created July 24, 2018 19:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dt/a2707b9f05e0f07bcc4c875c52d1c6db to your computer and use it in GitHub Desktop.
Save dt/a2707b9f05e0f07bcc4c875c52d1c6db to your computer and use it in GitHub Desktop.
daily cockroachdb backup script
#!/bin/bash
set -euxo pipefail
# this script runs backups, creating full backups when run on the configured
# day of the week and incremental backups when run on other days, tracking the
# backups it has created recently to correctly construct the list of path for
# the INCREMENTAL option.
full_day="Sunday" # Must match (including case) the output of `date +%A`.
what="DATABASE mydb" # what to backup.
base="s3://my-bucket/db-backups" # base dir in which to create backups.
recent="backups.txt" # file in which recent backups are recorded.
options="" # e.g. "WITH revision_history"
cmd="cockroach sql --certs mycerts" # customize as needed with security/network settings. `-e "stmt"` will be appended.
destination="${base}/$(date +"%Y%m%d-%H%M")"
if [[ "$(date +%A)" == "${full_day}" ]] ; then
$cmd -e "BACKUP ${what} TO '${destination}' AS OF SYSTEM TIME '-1m'${options};"
echo ${destination} > ${recent}
else
sep=""
prev=""
for i in $(cat ${recent}); do prev="${prev}${sep}'${i}'"; sep=", "; done;
if [ -z "${prev}" ]; then
echo "Missing prior backups, a full backup is required."
$cmd -e "BACKUP ${what} TO '${destination}' AS OF SYSTEM TIME '-1m'${options};"
echo ${destination} > ${recent}
else
destination="${destination}-inc"
$cmd -e "BACKUP ${what} TO '${destination}' AS OF SYSTEM TIME '-1m' INCREMENTAL FROM ${prev}${options};"
echo ${destination} >> ${recent}
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment