/dev/blog
Bez Hermoso, Software Engineer @ Square
I wrote a shell script that wraps rsync
with a user prompt in cases where files are going to be added, deleted, or changed, which is a scenario when some work might get lost:
safe-rsync.sh
:
#!/usr/bin/env bash
args=$@
diffs="$(rsync --dry-run --itemize-changes $args | grep '^[><ch.][dfLDS]\|^\*deleting')"
if [ -z "$diffs" ]; then
echo "Nothing to sync."
exit 0
fi
echo "These are the differences detected during dry-run. You might lose work. Please review before proceeding:"
echo "$diffs"
echo ""
read -p "Confirm? (y/N): " choice
case "$choice" in
y|Y ) rsync $args;;
* ) echo "Cancelled.";;
esac
Usage
> ./safe-rsync.sh --exclude='node_modules/' --recursive --progress --verbose ubuntu@aws-server102:/var/www/html ./html
To skip the dry-run and just rsync
regardless of any diffs:
> yes | ./safe-rsync.sh --exclude='node_modules/' --recursive --progress --verbose ubuntu@aws-server102:/var/www/html ./html