Skip to content

Instantly share code, notes, and snippets.

@alfredopalhares
Last active January 29, 2018 15:30
Show Gist options
  • Save alfredopalhares/3a8b6a00927b1447d4c1cf9b394eb0f6 to your computer and use it in GitHub Desktop.
Save alfredopalhares/3a8b6a00927b1447d4c1cf9b394eb0f6 to your computer and use it in GitHub Desktop.
Simple duplicity backup script
#!/usr/bin/env bash
# Backup script to an offline external drive
#
# Assumptions:
# * The drive is already mounted and it matches the destination
# * The encryption is taken care at the drive itself and its decypted before
# the script starts
#
# Is its recomemended to run this script without perfoming any workload
#
# Sends notifications to Pushover once steps are done
readonly logger_tag="backup-offline"
source "${HOME}/bin/source-me.sh"
readonly backup_dir="/home/masterkorp"
readonly destination="file:///path/to/backup"
readonly full_backups=2
logMessage "Started backup script" 0
duplicity collection-status --no-encryption \
"${destination}"
getConfirmation "A backup is about start. Do you want to continue?"
if [[ $? == "1" ]]; then
echo "Backup aborted by user"
exit 1
fi
duplicity --no-encryption \
--name offline \
--full-if-older-than 30D \
--exclude "${backup_dir}/.atom" \
--exclude "${backup_dir}/.bundle" \
--exclude "${backup_dir}/.cache" \
--exclude "${backup_dir}/Downloads" \
--exclude "${backup_dir}/Public" \
--exclude "${backup_dir}/tmp" \
--exclude "${backup_dir}/VirtualBox VMs" \
--exclude "**.swp" \
--exclude "**.tmp" \
--exclude "**~" \
--use-agent \
"${backup_dir}" "${destination}"
logMessage "Offline backup finished" 3
sendPush "Offline Backup Finished"
getConfirmation "Do you want to remove old backups?"
if [[ $? == "1" ]]; then
echo "Backup aborted by user"
exit 1
fi
duplicity remove-all-but-n-full $full_backups \
--name offline \
--no-encryption --force \
"${destination}"
sendPush "Finishing prunning offline backups"
exit 0
#!/bin/sh
# Logs given string to diferent outputs
#
# Usage: logMessage "message" <value>
# Values can be:
# 0 = logs to journal/syslog
# 1 = displays to stdout
# 2 = logs to journal and stdout
# 3 = logs to journal, displays to stdout and shows a popup notification
function logMessage {
if [[ -z $logger_tag ]]; then
exit 1;
fi
case $2 in
0)
logger -t "${logger_tag}" $1 ;;
1)
echo ${1} ;;
2)
logger -t "${logger_tag}" $1
echo ${1} ;;
3)
logger -t "${logger_tag}" $1
echo ${1}
notify-send "${logger_tag}" "'${1}'" ;;
*)
logger -t "${logger_tag}" $1 ;;
esac
}
# Prompts for confirmation, returns 1 if Yes
#
# Usage: getConfirmation "Question message"
# You can get anwser value on retcode
# if [[ $? == "1"]]; then
# echo "You choose no"
# fi
function getConfirmation {
value=0
while true; do
read -p "${1} [Y/n]" yn
case $yn in
[Yy]* ) return 0 ;;
[Nn]* ) return 1 ;;
* ) return 0 ;;
esac
done
}
# Send a push notification to Push Over
# Uses laptop-scripts application
# Sends the tittle as the hostname of the device
#
# Usage: sendPush "<message string>"
function sendPush {
hostname=$(hostname)
curl -s \
--form-string "token=<token>" \
--form-string "user=<user>" \
--form-string "title=${hostname}" \
--form-string "priority=0" \
--form-string "message=${1}" \
https://api.pushover.net/1/messages.json
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment