rsy 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #!/bin/bash
  2. configError() {
  3. echo "Error: No valid configuration found, exiting"
  4. notify-send "based-sync" "Error: No valid configuration found, exiting"
  5. exit 1
  6. }
  7. # check dependencies
  8. for prog in internal-ip rsync nc notify-send; do
  9. if ! command -v "$prog" > /dev/null 2>&1; then
  10. echo "$prog is not detected" 1>&2
  11. notify-send "$prog is not detected"; exit 1
  12. fi
  13. done
  14. # set dirs
  15. configDir="${XDG_CONFIG_HOME:-$HOME/.config}"
  16. cacheFile="/tmp/norisa/based-sync-cache"
  17. # read config files
  18. [ -f "$HOME/.config/based-sync/dirs" ] || configError
  19. [ -f "$HOME/.config/based-sync/cons" ] || configError
  20. dirs="$(cat "$configDir/based-sync/dirs")"
  21. cons="$(grep -v "$(internal-ip)" < "$configDir/based-sync/cons")"
  22. # reset cachefile safely
  23. [ -d ${cacheFile%/*} ] || mkdir -p ${cacheFile%/*}
  24. if [ -f $cacheFile ]; then
  25. rm $cacheFile; touch $cacheFile
  26. else
  27. touch $cacheFile
  28. fi
  29. # check each connection via netcat
  30. for con in $cons; do
  31. if nc -zv -w 1 "${con/*@}" 22 2>&1 | grep -q "succeeded"; then
  32. echo "$con ON" | tee $cacheFile
  33. else
  34. echo "$con OFF"
  35. fi
  36. done
  37. sed -i "s/ ON//g" $cacheFile
  38. # exit when no working connection found
  39. [ -s $cacheFile ] || configError
  40. # choose remote on which to sync the dirs to
  41. ip=$(dmenu -i -l 30 < $cacheFile)
  42. # sync dirs via rsync
  43. for dir in $dirs; do
  44. remoteUser=${ip/@*}
  45. rsync -uvrP --delete-after -e \
  46. 'ssh -T -c aes128-gcm@openssh.com -o Compression=no' \
  47. "${dir/\~/$HOME}/" "$ip:${dir/\~/\/home\/$remoteUser}"
  48. done
  49. # success message
  50. echo "Success: File syncing is finished"
  51. notify-send "Success: File syncing is finished"