sync-projects [direction] [please] [delete]
direction: values: up
or down
please: confirm action or attempt a dry run
delete delete files on destination
#!/bin/bash | |
PROJECTS_PATH="Projects" | |
EXCLUDE_FILE=".sync-projects.exclude" | |
SOURCE=$HOME/$PROJECTS_PATH | |
DEST=$HOME/Dropbox/ | |
if [ "$1" = "down" ]; then | |
SOURCE=$HOME/Dropbox/$PROJECTS_PATH | |
DEST=$HOME/ | |
fi | |
rm -rf $HOME/$EXCLUDE_FILE | |
# create temp exclude file | |
cat <<EOT>> $HOME/$EXCLUDE_FILE | |
.git | |
*.log | |
bower_components | |
coverage | |
node_modules | |
.nyc_output | |
sandbox | |
EOT | |
# append .gitignore content for each project to exclude from rsync | |
find $HOME/$PROJECTS_PATH -type f -name ".gitignore" | \ | |
egrep -v "node_modules|bower_components|coverage" | \ | |
xargs -I % sh -c "nl -s %/ % | cut -c7-" | \ | |
sed "s/\/.gitignore\//\//" | | |
sed "s@$HOME/$PROJECTS_PATH/@@" >> $HOME/$EXCLUDE_FILE | |
# construct rsync command | |
CMD=('rsync') | |
if [ "$2" != 'please' ]; then | |
CMD+=('--dry-run') | |
CMD+=('--itemize-changes') | |
fi | |
if [ "$3" = 'delete' ]; then | |
CMD+=('--delete') | |
fi | |
CMD+=('--checksum') | |
CMD+=('--hard-links') | |
CMD+=('--ignore-times') | |
CMD+=('--links') | |
CMD+=('--one-file-system') | |
CMD+=('--perms') | |
CMD+=('--progress') | |
CMD+=('--recursive') | |
CMD+=('--verbose') | |
CMD+=("--exclude-from=$HOME/$EXCLUDE_FILE") | |
CMD+=("$SOURCE $DEST") | |
$(IFS=' ' ; echo "${CMD[*]}") |