Skip to content

Instantly share code, notes, and snippets.

@hansek hansek/rsync.sh

Created Sep 25, 2014
Embed
What would you like to do?
Script to easily manage Rsync settings to sync project media
#!/bin/bash
# check if config file exists
if [ ! -f $(dirname "$0")/rsync_config.sh ]; then
echo -e "\e[41mERROR:\e[0m Config file not found!"
exit 1
fi
# include config file
source $(dirname "$0")/rsync_config.sh
# help message
usage() {
echo "MEDIA RSYNC"
echo ""
echo "SSH-AGENT must be running"
echo ""
echo "usage: rsync.sh <relative-path-to-local-project-root>"
}
# check if ssh agent is running and has a loaded key
ssh-add -l >/dev/null 2>&1
if [ $? = 2 ]; then
# exit-status 2 = couldn't connect to ssh-agent
echo "\e[41mERROR:\e[0m SSH-AGENT not active, please start SSH-AGENT before continue!"
exit 1
fi
# check if relative path to project root has been specified
if [ $# -ne 1 ]; then
usage
exit 1
fi
# check if relative path to project root exists
if [ ! -d $1 ]; then
echo -e "\e[41mERROR:\e[0m Specified relative path to project root not exists!"
exit 1
fi
# check if server is set in config file
if [ "${server}" == "" ]; then
echo -e "\e[41mERROR:\e[0m Server not set!"
exit 1
fi
# check if server is set in config file
if [ "${server_path}" == "" ]; then
echo -e "\e[41mERROR:\e[0m Server path not set!"
exit 1
fi
# check if ssh user is set in config file
if [ "${user}" == "" ]; then
echo -e "\e[41mERROR:\e[0m User not set!"
exit 1
fi
### MAIN CODE
# iterate over paths from config
for r_path in ${rsync_paths[*]}
do
echo -e "\n\e[37;44m###\e[0m Syncing \e[37;44m${r_path}\e[0m to local \e[37;44m$1${r_path}\e[0m\n"
rsync -rzvt --delete --perms --chmod=a+rwx -e ssh ${user}@${server}:${server_path}${r_path}/ $1${r_path}
echo -e "\n"
done
echo -e "\n\e[37;42m### DONE\e[0m\n"
#!/bin/bash
# server address
server=
# server relative project root path
server_path=
# ssh username
user=
# paths to be synced (bellow examples for MODX Evolution)
rsync_paths=(
media/files
media/galleries
media/images
media/flash
media/media
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.