Mar 2, 2014

For backup: Auto commit Git script

I'm a novice in Bash scripting. But after trying to write this by myself, I found Bash is really useful and interesting as well.
#!/bin/bash
# ------------------------------------------------------
# Git: Auto-commit on scheduled
# ------------------------------------------------------
# Path to list of sites
# Sites should be under git versioning
# Example: /srv/site-a should have:
#
#          /srv/site-a/public <-- HTTP server point here
#          /srv/site-a/.git
SRV_DIR="/srv"
# ------------------------------------------------------
SRV_LIST=$(ls $SRV_DIR)
for s in $SRV_LIST; do
    # Is a dir with git inside?
    if [[ -d $s && -d $s/.git ]]
    then
        echo "Found: $s"
        GIT_BASE=" git --git-dir=$SRV_DIR/$s/.git --work-tree=$SRV_DIR/$s "
        IS_CHANGE=$($GIT_BASE status | grep 'Changes not staged' | wc -l)
        if [ $IS_CHANGE -eq '1' ]
        then
            CURRENT_TIME=$(date)
            $GIT_BASE add $SRV_DIR/$s
            $GIT_BASE commit -m"Auto-commit $CURRENT_TIME"
            $GIT_BASE push backup
        fi
    fi
done

No comments:

Post a Comment

New comment