Skip to content

Instantly share code, notes, and snippets.

@ahmadnassri
Last active December 31, 2016 17:48
Show Gist options
  • Save ahmadnassri/4dc9a4dc56cd6d4a2dd742ee21c46f56 to your computer and use it in GitHub Desktop.
Save ahmadnassri/4dc9a4dc56cd6d4a2dd742ee21c46f56 to your computer and use it in GitHub Desktop.

Usage

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[*]}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment