Backup given sources via restic to B2 at regularity using SNS to notify of result
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Requires: | |
# - restic (system package) | |
# - AWS account with SNS topic configured and API keys created | |
# - awscli (pip) with API keys configured (aws configure) | |
# - Backblaze B2 account with API keys | |
# - cron implementation (system package) | |
ResticBinaryPath=`which restic` | |
AwsCliBinaryPath=`which aws` | |
ScriptWorkingPath='/path/to/this/script.sh' | |
RepositoryEngine='b2' | |
RepositoryPath='' | |
UploadLimit=0 #KiB/s | |
RepositoryPasswordFile="${ScriptWorkingPath}/b2-encryption-pass" | |
SnsTopic="arn:aws:sns:${AwsSnsRegion}:id:topic_name" | |
AwsRegion='' | |
export AWS_DEFAULT_PROFILE='' | |
export AWS_CONFIG_FILE=/path/to/.aws/config | |
export AWS_SHARED_CREDENTIAL_FILE=/path/to/.aws/credentials | |
export B2_ACCOUNT_ID= | |
export B2_ACCOUNT_KEY= | |
# Path exclusions from objects within SourcePaths | |
ExcludedPaths=( \ | |
'/paths/within/sources/to/exclude' | |
) | |
# Do not leave trailing slash on the end of directory sources | |
SourcePaths=() | |
# Path to a text file containing further source paths to include | |
SourceFileList="${ScriptWorkingPath}/b2-sources.filelist" | |
[ ! -d "${ScriptWorkingPath}/logs" ] && ( mkdir "${ScriptWorkingPath}/logs" ) | |
LogFile="${ScriptWorkingPath}/logs/b2-`date '+%Y%m%d'`.log" | |
function SnsPublication () { | |
local ErrorCode=$1 | |
if [ $ErrorCode -gt 0 ] ; then | |
local ScriptResultCode='Failure' | |
local ScriptPostMessage="Something went wrong executing the S3 restic backup job. See $LogFile for summary details." | |
else | |
local ScriptResultCode='Success' | |
local ScriptPostMessage="Successfully executed S3 restic backup job. See $LogFile for summary details." | |
fi | |
# Publish to SNS topic to notify admin | |
$AwsCliBinaryPath sns publish \ | |
--region $AwsRegion \ | |
--topic-arn $SnsTopic \ | |
--subject "Restic S3 backup job notification ($ScriptResultCode)" \ | |
--message "$ScriptPostMessage" \ | |
2>&1 >> $LogFile | |
} | |
# Assemble the one-liner | |
CommandPrefix="$ResticBinaryPath -r ${RepositoryEngine}:${RepositoryPath} backup -v -v --limit-upload $UploadLimit --password-file $RepositoryPasswordFile" | |
for e in "${ExcludedPaths[@]}" ; do | |
CommandExclusions="${CommandExclusions} --exclude $e" | |
done | |
for i in "${SourcePaths[@]}" ; do | |
CommandSources="${CommandSources} $i" | |
done | |
Command="$CommandPrefix $CommandExclusions $CommandSources --files-from $SourceFileList" | |
# Execute backup job | |
$Command 2>&1 >> $LogFile | |
ReturnCode=$? | |
# Generate email with outcome of job | |
if [ $ReturnCode -ne 0 ] ; then | |
SnsPublication 1 | |
else | |
SnsPublication 0 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment