Script: Backup

Published 05-06-2018 03:11:46

Raw

#!/bin/bash
# Bound to Mod+Backspace

# Set menu properties
mymenu=$(setmenu)

# # --- User-defined variables

# ServerHost should also be defined in ~/.ssh/config
# for key-authenticated sshfs connections to the
# backup repository
ServerHost="nas"

# NASPath should be the path to a writeable directory
# inside your backup repository
NASPath="/media/home-nas/9c360a84-078b-4cc5-9515-a38f3f84daa5/backup"

# NASMountRoot is the 'root' directory of your
# backup repository; this will be mounted to a local
# directory
NASMountRoot="../../media"

# NASMountLocal is the local directory that is used
# to mount NASMountRoot
NASMountLocal="/media/home-nas"

# USBPath should be the path to a writeable directory
# on a mounted USB drive
USBPath="/run/media/theatomheart/xkey"

# Where should log files be stored
PathToLogs="/home/$USER/Logs/"

# # --- Functions
ClearExit() { clear && exit ;}
Say() { echo "$1" "$2" ; notify-send "$1" "$2" ;}
NoConnection() { Say "⛔ <b>$1</b> is not connected." ; ClearExit ;}
ConnectNAS() { sshfs $ServerHost:$NASMountRoot $NASMountLocal && Say "🔗 Connected to $ServerHost." ;}
BackupMenu() {
	choice=$(echo -e "USB\nNAS\nConfigure" | $mymenu "Backup Menu") || ClearExit
	clear
	case $choice in
		"USB")	path="$USBPath/$HOSTNAME" ;;
		"NAS")	path="$NASPath/$HOSTNAME" ;;
		"Configure")	$TERMINAL -e config backup &disown
				ClearExit ;;
	esac ;}
BackupPath() {
	if [ ! -d $path ]; then
		case $choice in
			"USB")	NoConnection "$(basename $USBPath)" ;;
			"NAS")	ConnectNAS || NoConnection "$ServerHost" ;;
		esac
	fi
	if [ ! -d $path ]; then NoConnection "$ServerHost" ; fi ;}
GetConfirmation() {
	Confirm=$(echo -e "Yes\nNo" | $mymenu "Confirm: backup to $choice?") || ClearExit
	[[ $Confirm = "No" ]] && ClearExit
	clear ;}
RunBackup(){
	clear &&
	Say "⌛ Running backup" &&
	rsync -r -t -p -v --progress -u -s --delete \
		--exclude=$USER/VirtualBox* \
		--exclude=$USER/.cache \
		--exclude=$USER/Podcasts \
		--exclude=$USER/Videos \
		--exclude=$USER/Downloads \
		--exclude=$USER/Music \
		$HOME $path &&
	Say "👍 Backup complete." &&
	mpv $HOME/.scripts/notify.mp3 &>/dev/null ;}

# # --- Simple steps
BackupMenu || ClearExit
BackupPath
GetConfirmation
RunBackup || Say "⛔ There was a problem running the backup."