#!/bin/bash
# Bound to Mod+UpArrow and Mod+DownArrow
MenuChoices="all\nbrowsers\ncalendar\ndocuments\ndotfiles\nhosts\npictures\nprojects\nscripts\nssh\nvpn"
choices=${MenuChoices//'\n'/ }
choices=($choices)
# # --- User-defined variables
# ServerHost should also be defined in ~/.ssh/config
# for key-authenticated sshfs connections to the
# backup repository
ServerHost="nas"
# SyncPath should be the path to a writeable directory
# inside your backup repository
#SyncPath="/media/home-nas/syncworkflow"
SyncPath="/media/nas/sync/X220"
# NASMountRoot is the 'root' directory of your
# backup repository; this will be mounted to a local
# directory
NASMountRoot="../../sharedfolders"
# NASMountLocal is the local directory that is used
# to mount NASMountRoot
NASMountLocal="/media/nas"
# I like to be able to type 'push web' to make
# changes to theatomheart.net
WebPublishScript="/home/theatomheart/Projects/sh/theatomheart.sh"
# Where should log files be stored
PathToLogs="/home/$USER/Logs/"
# A new log file is created for each run
[[ $1 = "down" ]] && logfile="pull" || logfile="push"
LogFile="$PathToLogs/sync/$(date +%b%d)/$(date +%H%M).$logfile.log"
# # --- Functions
ProcessArguments() {
case $1 in
browser|browsers) $Folder ".mozilla/"
$Folder ".surf/" ;;
calendar|cal) $Folder ".config/calcurse/" ;;
documents|docs) $Folder "Documents/" ;;
config|dotfiles) $File ".bashrc"
$File ".profile"
$File ".xprofile"
$File ".gtkrc-2.0"
$File ".vimrc"
$File ".xinitrc"
$File ".config/aliasrc"
$File ".config/bookmarks"
$File ".config/contacts"
$File ".config/todo"
$File ".config/hugosites.conf"
$Folder ".config/mpv/"
$Folder ".config/ncmpcpp/"
$Folder ".config/ranger/"
$Folder ".config/sxiv/"
$Folder ".config/theaarbs/"
$Folder ".config/wal/"
$File ".config/tmux/tmux.conf"
$File ".config/mpd/mpd.conf"
$File ".config/mimeapps.list"
$Folder ".config/zathura/"
$Folder ".local/share/applications/"
$Folder ".config/newsboat/"
$Folder ".vim/" ;;
pictures|pics) $Folder "Pictures/" ;;
projects) $Folder "Projects/" ;;
scripts) $Folder ".local/bin/" ;;
ssh|keys) $File ".ssh/config"
$File ".ssh/known_hosts" ;;
hosts) HandleHosts ;;
vpn) HandleVPNs ;;
all) ;;
*) Say "⛔ '$chosen' is an invalid option." && PrintHelp ;;
esac ;}
PrintHelp() {
cat << EOF
Usage:
push [argument(s)] - syncs files to a server
pull [argument(s)] - syncs files from a server
push web - runs web publishing script
Available arguments:
EOF
for i in ${choices[@]} ; do
echo " $i"
done ;}
oneline() {
cr=`tput cr;tput el`
if [ -z "$COLUMNS" ] ; then COLUMNS=80 ; fi
while read line ; do
echo -n "$cr${line:0:$COLUMNS}"
echo "$line" >> $LogFile
done
echo ;}
Say() {
echo "$1" "$2"
notify-send "$1" "$2"
echo "$1" "$2" >> $LogFile ;}
NoConnection() { Say "⛔ I can't find $ServerHost." && exit 1 ;}
PushFolder() { rsync -rtpvus --links --progress "$HOME/$1" "$SyncPath/$1" | oneline && Say "⏫ Pushed $1" ;}
PullFolder() { rsync -rtpvus --links --progress "$SyncPath/$1" "$HOME/$1" | oneline && Say "⏬ Pulled $1" ;}
PushFile() { rsync -rtpvus --links --progress "$HOME/$1" "$SyncPath/$1" | oneline && Say "⏫ Pushed $1" ;}
PullFile() { rsync -rtpvus --links --progress "$SyncPath/$1" "$HOME/$1" | oneline && Say "⏬ Pulled $1" ;}
RunArgs() { for arg in $* ; do ProcessArguments $arg ; done ;}
HandleHosts() {
[[ $File = "PushFile" ]] && cat /etc/hosts >~/.config/hosts
$File ".config/hosts"
[[ $File = "PullFile" ]] && sudo cat ~/.config/hosts >/etc/hosts ;}
HandleVPNs() {
$Folder ".cert/"
if [ $File = "PushFile" ] ; then
sudo rsync -rtpvus --links --progress --delete /etc/ppp/ ~/.config/vpns/ppp/
sudo rsync -rtpvus --links --progress --delete /etc/vpnc/ ~/.config/vpns/vpnc/
sudo rsync -rtpvus --links --progress --delete /etc/openvpn/ ~/.config/vpns/openvpn/
sudo ~/.local/bin/getnmprofiles UP
sudo chown -R $USER ~/.config/vpns/
fi
$Folder ".config/vpns/"
if [ $File = "PullFile" ] ; then
sudo rsync -rtpvus --links --progress --delete ~/.config/vpns/ppp/ /etc/ppp/
sudo rsync -rtpvus --links --progress --delete ~/.config/vpns/vpnc/ /etc/vpnc/
sudo rsync -rtpvus --links --progress --delete ~/.config/vpns/openvpn/ /etc/openvpn/
sudo ~/.local/bin/getnmprofiles DOWN
fi ;}
SyncSteps() {
Say "🔗 Checking connection to $ServerHost."
if [ ! -d "$SyncPath" ]; then
sshfs $ServerHost:$NASMountRoot $NASMountLocal &&
Say "🔗 Connected to $ServerHost." ||
NoConnection ; fi
[[ ! -d "$SyncPath" ]] && NoConnection
case $1 in
up) File="PushFile" ; Folder="PushFolder" ;;
down) File="PullFile" ; Folder="PullFolder" ;;
esac
RunArgs $chosen
mpv --quiet $HOME/.local/bin/notify.mp3 &>/dev/null ;}
[[ $2 = "-h" ]] && PrintHelp && exit 0
mkdir -p $PathToLogs/sync/$(date +%b%d)/
echo "Arguments: $*" >> $LogFile
[[ ! $1 = "up" ]] && [[ ! $1 = "down" ]] && exit 0
case $(tty) in
*tty*) mymenu="dwmenu -l Sync $1" ;;
*) mymenu="fzf --prompt="Sync_$1"" ;;
esac
[[ $1 = "up" ]] && [[ $2 = "web" ]] && $WebPublishScript && exit 0
[[ -z $2 ]] && chosen=$(echo -e "$MenuChoices" | $mymenu) || chosen=${@:2}
[[ $chosen = "all" ]] && chosen=${choices[@]}
[[ $chosen ]] && SyncSteps $1 $chosen && exit 0