THEAARBS tools - syncworkflow

Published 07-07-2018 03:11:46

Raw

#!/bin/bash
# Bound to Mod+UpArrow and Mod+DownArrow
MenuChoices=\
"all
browsers
calendar
documents
dotfiles
hosts
hugo
pictures
projects
scripts
ssh
vpn"
choices=${MenuChoices//'\n'/ }
choices=($choices)
# Set dmenu properties
mymenu=$(setmenu -l)
# # --- 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/6eece2a2-d4d4-47d6-bb12-ae9db150e8e6/rsync_computers"

# 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"

# I like to be able to type 'push web' to make
# changes to theatomheart.net
WebPublishScript="/home/theatomheart/Projects/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/" ;;
        hugo|sites)     $Folder "Hugo/" ;;
        config|dotfiles)    $File   ".bashrc"
                    $File   ".config/aliasrc"
                    $File   ".profile"
                    $File   ".gtkrc-2.0"
                    $File   ".vimrc"
                    $File   ".xinitrc"
                    $File   ".config/hugosites.conf"
                    $Folder ".config/i3/"
                    $Folder ".config/i3blocks/"
                    $File   ".config/mutt/muttrc"
                    $Folder ".config/mpv/"
                    $Folder ".config/ncmpcpp/"
                    $Folder ".config/ranger/"
                    $Folder ".config/sxiv/"
                    $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 ".scripts/" ;;
        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 --delete "$HOME/$1" "$SyncPath/$1" | oneline && Say "⏫ Pushed $1" ;}
PullFolder() { rsync -rtpvus --links --progress --delete "$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 ~/.scripts/tools/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 ~/.scripts/tools/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/.scripts/notify.mp3 &>/dev/null ;}

[[ $2 = "-h" ]] && PrintHelp | lolcat && exit 0

mkdir -p $PathToLogs/sync/$(date +%b%d)/
echo "Arguments: $*" >> $LogFile

[[ ! $1 = "up" ]] && [[ ! $1 = "down" ]] && exit 0

[[ $1 = "up" ]] && [[ $2 = "web" ]] && $WebPublishScript && exit 0

[[ -z $2 ]] && chosen=$(echo -e "$MenuChoices" | $mymenu "Sync selection:") || chosen=${@:2}

[[ $chosen = "all" ]] && chosen=${choices[@]}

[[ $chosen ]] && SyncSteps $1 $chosen | lolcat && exit 0

clear