Skip to content

Instantly share code, notes, and snippets.

@dhsathiya
Created September 23, 2020 09:59
Show Gist options
  • Save dhsathiya/240ce8fcccd975dec55d90d590b0295b to your computer and use it in GitHub Desktop.
Save dhsathiya/240ce8fcccd975dec55d90d590b0295b to your computer and use it in GitHub Desktop.
# bash update_and_sync.sh /tmp/test/ [email protected] /tmp/test/
# syntax bash update_and_sync.sh <source> <user@ip> <destination>
DIRECTORY_TO_WATCH="$1"
REMOTE_SERVER_USER_IP="$2"
REMOTE_SERVER_LOCATION="$3"
inotifywait -m $DIRECTORY_TO_WATCH -e close_write -e delete |
while read path action file; do
rsync -avzhP $path $REMOTE_SERVER_USER_IP:$REMOTE_SERVER_LOCATION --delete
done
@dhsathiya
Copy link
Author

dhsathiya commented Jul 21, 2021

For local

# bash update_and_sync.sh /tmp/test/ /tmp/test2/
# Change in /tmp/test/ will be synced in /tmp/test2/
DIRECTORY_TO_WATCH="$1"
REMOTE_SERVER_LOCATION="$2"
inotifywait -m $DIRECTORY_TO_WATCH -e close_write -e delete |
    while read path action file; do
        rsync -avzh $path $REMOTE_SERVER_LOCATION --delete
    done

Local with changing file permissions to www-data

# bash update_and_sync.sh /tmp/test/ /tmp/test2/ user_name
# Change in /tmp/test/ will be synced in /tmp/test2/
DIRECTORY_TO_WATCH="$1"
REMOTE_SERVER_LOCATION="$2"
SYNC_USER="$3"
inotifywait -r -m $DIRECTORY_TO_WATCH -e close_write -e delete |
    while read path action file; do
        mkdir -p $path
        rsync -avzh -og $path $REMOTE_SERVER_LOCATION --usermap=$SYNC_USER:www-data --groupmap=$SYNC_USER:www-data --delete
    done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment