Skip to content

Instantly share code, notes, and snippets.

@Dinir
Last active August 28, 2016 17:23
Show Gist options
  • Save Dinir/6a716f0e36fd81cba5d90f6f5344e8d5 to your computer and use it in GitHub Desktop.
Save Dinir/6a716f0e36fd81cba5d90f6f5344e8d5 to your computer and use it in GitHub Desktop.
Sync files to your server without typing all the rsync commands.
#!/bin/bash
# Dinir Nertan, 2016
# http://dinir.works, https://twitter.com/DinirNertan
# I made this script to conveniently rsync files to my server
# by removing the need of typing all the command every time.
# HOW TO USE
# Put this script on the base directory you want to sync files from,
# specify the path you want to sync your files to:
locationInTheServer="user@example.com:~/directory/to/sync"
# , specify files and directories you want to update frequently:
defaultFileListForSync="--del scss css font *.png *.ico *.html"
# (`--del .` can be dangerous as it will overwrite the entire directory)
# , and use the script on a console:
# ./syncToServer.sh => syncs everything
# ./syncToServer.sh filename... => syncs specified files
# ./syncToServer.sh --dry-run => dry-run of syncing everything
# ./syncToServer.sh --dry-run filename... => dry-run of syncing specified files
# DEFAULT FLAGS
# It will always use `--exclude ${0:2}`, `--progress`, `-r`, and `-u`,
# so it will always 'exclude' itself, show 'progress',
# and 'r'ecursively 'u'pdate only older files on the receiver.
# Including other options is okay, but you must specify what to send then
# (except `--dry-run`, I use this often so I made an exception for that now).
# NOTE ABOUT `--del`
# This flag includes 'deleting files you deleted on your side' in the syncing.
# If you're keeping every file in the `locationInTheServer` on your computer,
# you can use `--del .` to sync every file in that directory on the server.
# But if you have some files you're not tracking on your computer,
# specify the files you want to sync like `--del dir1 dir2 dir3 *.html *.png`
# so the untracked files can be kept in the directory on the server.
argumentListForSync=""
if [ -z $1 ]; then
argumentListForSync+=$defaultFileListForSync
else
if [ $1 == "--dry-run" ]; then
argumentListForSync+="$1 "
shift
fi
if [ $# -ne 0 ]; then
argumentListForSync+="$@"
else
argumentListForSync+=$defaultFileListForSync
fi
fi
if [ -n "$argumentListForSync" ]; then
rsync --exclude ${0:2} --progress -ru $argumentListForSync $locationInTheServer
fi
unset argumentListForSync
unset defaultFileListForSync
unset locationInTheServer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment