Skip to content

Instantly share code, notes, and snippets.

@vfaronov
Created January 16, 2017 10:00
Show Gist options
  • Select an option

  • Save vfaronov/883563dc620e118cee27b8444548f46d to your computer and use it in GitHub Desktop.

Select an option

Save vfaronov/883563dc620e118cee27b8444548f46d to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
die()
{
echo "notdot: $1" >&2
exit 1
}
do_add()
{
machine=$1
[[ $machine ]] || die 'no machine specified'
[[ ! -e "machines/$machine" ]] || die "$machine already registered"
shift
profile=${1:-default}
[[ -d "profiles/$profile" ]] || die "profile $profile does not exist"
ln -s "../profiles/$profile" "machines/$machine" || die 'ln failed'
ssh "$machine" true || die 'ssh failed'
do_sync "$machine"
}
do_sync()
{
tmpdir=$( mktemp --tmpdir -d "notdot-$USER-XXX" )
for machine; do
echo "-------- sync $machine --------"
rsync --recursive --links \
"machines/$machine/files/" "$machine:~/" \
|| die 'rsync failed'
echo 'rsync OK, checking for extraneous files...'
pushd "machines/$machine/files" >/dev/null
find . | cat - ../ignore >"$tmpdir/expected"
popd >/dev/null
ssh "$machine" 'find .' >"$tmpdir/found" || die 'ssh failed'
comm -13 <( sort "$tmpdir/expected" ) <( sort "$tmpdir/found" )
echo
done
rm -rf "$tmpdir"
}
do_syncall()
{
# shellcheck disable=SC2046
do_sync $( ls machines/ )
}
[[ -d machines ]] || die 'no machines/ directory here'
action=$1
shift
"do_$action" "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment