Skip to content

Instantly share code, notes, and snippets.

@whoabuddy

whoabuddy/stacks-backup.sh Secret

Created Jan 16, 2021
Embed
What would you like to do?
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