Skip to content

Instantly share code, notes, and snippets.

@whoabuddy
Created January 16, 2021 16:07
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 whoabuddy/033a47ad502ca30722187ed1baf3cc6e to your computer and use it in GitHub Desktop.
Save whoabuddy/033a47ad502ca30722187ed1baf3cc6e to your computer and use it in GitHub Desktop.
A rotating backup script for working_dir
#!/usr/bin/env bash
# Script created 2021-01-11 by WhoaBuddy
# Hosted on GitHub by AbsorbingChaos
# Link: https://github.com/AbsorbingChaos/bks-setup-miner
# Based on Bash3 Boilerplate. Copyright (c) 2014, kvz.io
# Link: https://kvz.io/blog/2013/11/21/bash-best-practices/
set -o errexit
set -o pipefail
set -o nounset
################################################
# Rotating backups for stacks-blockchain-data #
################################################
# NOTE: working_dir set in stacks-node
# config to $HOME/stacks-blockchain-data
# PLEASE REVIEW YOUR SETUP ACCORDINGLY
###########
# OPTIONS #
###########
# Number of backups to keep
__rotations=3
# Directory to backup FROM (stacks-node working dir)
__targetDir="/home/whoabuddy/stacks-blockchain-data"
# Directory to save backup(s) TO
__backupDir="/home/whoabuddy/stacks-backup"
# Log file name for backups
# Stored in $__backupDir
__backupLog="stacks-backup.log"
##########
# SCRIPT #
##########
if [ -f "$__backupDir/$__backupLog" ]; then
__rotate=$(tail -n 1 "$__backupDir/$__backupLog" | cut -c '1')
if [ "$__rotate" -lt "$__rotations" ]; then
((__rotate++))
else
__rotate=1
fi
else
mkdir -p "$__backupDir"
touch "$__backupDir/$__backupLog"
printf "========== STACKS-BACKUP LOG ==========\n" >> "$__backupDir/$__backupLog"
__rotate=1
fi
{
# new line
printf "=======================================\n"
# log start
__stampStart=$(date +"%Y%m%d-%H%M%S")
printf "%s %s backup started\n" "$__rotate" "$__stampStart"
# log target
printf "target directory: %s\n" "$__targetDir"
printf "backup directory: %s\n" "$__backupDir/$__rotate"
rsync -aq --delete "$__targetDir" "$__backupDir/$__rotate"
# log end
__stampEnd=$(date +"%Y%m%d-%H%M%S")
printf "%s %s backup complete\n" "$__rotate" "$__stampEnd"
} >> "$__backupDir/$__backupLog"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment