syncdir (1503B)
1 #!/bin/sh 2 set -e 3 4 version=0.1 5 user=$USER 6 host=iddqd 7 8 show_help() { 9 echo "usage: ${0##*/} DIR1 [DIR2 ...]" 10 echo "performs a two-way sync of the contents of the specified DIRs to $host" 11 echo 12 echo "OPTIONS are one or more of the following:" 13 echo " -h, --help show this message" 14 echo " -v, --version show version and license information" 15 echo " -H, --host VALUE use specified host instead of default ($host)" 16 echo " -U, --user VALUE use specified user instead of default ($USER)" 17 echo " -- do not consider any following args as options" 18 } 19 20 show_version() { 21 echo "${0##*/} version $version" 22 echo "Licensed under the GNU Public License, v3+" 23 echo "written by Anders Damsgaard, anders@adamsgaard.dk" 24 echo "https://gitlab.com/admesg/dotfiles" 25 } 26 27 die() { 28 printf '%s\n' "$1" >&2 29 exit 1 30 } 31 32 sync_dir() { 33 echo "syncing $1 to $host" 34 rsync --update -rav --progress -e "ssh" "$1/" "$user@$host":"$1/" 35 36 echo "syncing $host to $1" 37 rsync --update -rav --progress -e "ssh" "$user@$host":"$1/" "$1/" 38 } 39 40 while :; do 41 case "$1" in 42 -h|-\?|--help) 43 show_help 44 exit 0 45 ;; 46 -v|--version) 47 show_version 48 exit 0 49 ;; 50 -H|--host) 51 host="$2" 52 shift 53 ;; 54 -U|--user) 55 user="$2" 56 shift 57 ;; 58 --) # end all options 59 shift 60 break 61 ;; 62 -?*) 63 die 'Error: Unknown option specified' 64 ;; 65 *) # No more options 66 break 67 esac 68 shift 69 done 70 71 if [ $# -lt 1 ]; then 72 show_help 73 exit 1 74 fi 75 76 for d in "$@"; do 77 mkdir -p "$d" 78 sync_dir "$d" 79 done