Skip to content

Instantly share code, notes, and snippets.

@EmranAhmed
Created April 16, 2018 11:35
Show Gist options
  • Save EmranAhmed/5f4d0774d958ad9b30a278abd55221fe to your computer and use it in GitHub Desktop.
Save EmranAhmed/5f4d0774d958ad9b30a278abd55221fe to your computer and use it in GitHub Desktop.
RSYNC
  • Make it exeicutable like: chmod u+x rsync.sh later you have to make chmod u+x .rsyncconfig and chmod u+x .rsyncignore
  • ./rsync.sh init
  • Do you have password less alias host? (Y/n): means if you use passwordless login like: ssh example.com were example.com is your alias hostname on ~/.ssh/config file.
  • ~/.ssh/config file example:
Host example.com
    HostName 12.12.12.12 # Your VPS IP Address
    User root
    Port 22
    IdentityFile ~/.ssh/id_rsa # Your generated SSH key which listed on your VPS authorized_keys
    IdentitiesOnly yes
  • SSH Directory Path like: /var/www without backslash (/)

  • If you use want to generate ssh key for your VPS create follow this link: https://blog.g3rt.nl/upgrade-your-ssh-keys.html

  • There willbe a .rsyncignore file to ignore some files. Open it and make change as your need.

  • Please make sure that you added rsync.sh, .rsyncconfig and .rsyncignore file on your .gitignore file

  • use ./rsync.sh test-push to check which file is going to upload.

  • use ./rsync.sh push to upload file

  • use ./rsync.sh test-pull to check which file is going to download from server.

  • use ./rsync.sh pull to download from server.

#!/bin/bash
CONFIGFILE=".rsyncconfig"
IGNOREFILE=".rsyncignore"
ME=`basename "$0"`
create_ignore_config () {
echo "# Rsync Ignore files. remove # for active" > $IGNOREFILE
echo ".DS_Store" >> $IGNOREFILE
echo ".idea" >> $IGNOREFILE
echo ".git" >> $IGNOREFILE
echo ".gitignore" >> $IGNOREFILE
echo $CONFIGFILE >> $IGNOREFILE
echo $IGNOREFILE >> $IGNOREFILE
echo $ME >> $IGNOREFILE
echo "# *.sh" >> $IGNOREFILE
echo "# wp-config.php" >> $IGNOREFILE
echo "# directory" >> $IGNOREFILE
echo ""
echo "Rsync Init Successfully..."
}
create_alias_config () {
echo -en "Alias host name: "
read SSHHOST
echo -en "SSH Directory Path: "
read SSHFILEPATH
echo "# SSH Configuration " > $CONFIGFILE
echo "SSHHOST=\"$SSHHOST\"" >> $CONFIGFILE
echo "SSHFILEPATH=\"$SSHFILEPATH\"" >> $CONFIGFILE
echo "TYPE=\"alias\"" >> $CONFIGFILE
create_ignore_config
exit
}
create_id_rsa_config () {
echo -en "SSH username: "
read SSHUSERNAME
echo -en "SSH IP or domain: "
read SSHHOST
echo -en "SSH Directory Path: "
read SSHFILEPATH
echo -en "SSH Key (id_rsa): "
read SSHIDRSA
echo "# SSH Configuration " > $CONFIGFILE
echo "SSHUSERNAME=\"$SSHUSERNAME\"" >> $CONFIGFILE
echo "SSHHOST=\"$SSHHOST\"" >> $CONFIGFILE
echo "SSHFILEPATH=\"$SSHFILEPATH\"" >> $CONFIGFILE
echo "SSHIDRSA=\"ssh -i $HOME/.ssh/$SSHIDRSA\"" >> $CONFIGFILE
echo "TYPE=\"id_rsa\"" >> $CONFIGFILE
create_ignore_config
exit
}
create_basic_config () {
echo -en "SSH username: "
read SSHUSERNAME
echo -en "SSH IP or domain: "
read SSHHOST
echo -en "SSH Directory Path: "
read SSHFILEPATH
echo "# SSH Configuration " > $CONFIGFILE
echo "SSHUSERNAME=\"$SSHUSERNAME\"" >> $CONFIGFILE
echo "SSHHOST=\"$SSHHOST\"" >> $CONFIGFILE
echo "SSHFILEPATH=\"$SSHFILEPATH\"" >> $CONFIGFILE
echo "TYPE=\"basic\"" >> $CONFIGFILE
create_ignore_config
exit
}
ask_alias_host () {
read -p "Do you have password less alias host? (Y/n): " yn
case $yn in
[Yy]* ) create_alias_config;;
[Nn]* ) ask_id_rsa;;
* ) echo "Please answer yes or no."; exit;;
esac
}
ask_id_rsa () {
read -p "Do you use id_rsa key? (Y/n): " yn
case $yn in
[Yy]* ) create_id_rsa_config;;
[Nn]* ) create_basic_config;;
* ) echo "Please answer yes or no."; exit;;
esac
}
if [[ $1 == "init" && ! -f $CONFIGFILE ]]; then
ask_alias_host
fi
if [ ! -e $CONFIGFILE ]; then
echo "Configuration file is not exists. Please run ./rsync.sh init"
exit 1
fi
if [ ! -e $IGNOREFILE ]; then
echo "Ignore file is not exists. Please run ./rsync.sh init"
exit 1
fi
# source $CONFIGFILE
. $CONFIGFILE
BASECOMMAND="rsync -avz --progress --exclude-from $IGNOREFILE"
DRYCOMMAND="rsync -naic --progress --exclude-from $IGNOREFILE"
help_text () {
echo ""
echo "=== Available Commands ==="
echo ""
echo " ./rsync.sh test-push"
echo " ./rsync.sh push"
echo ""
echo " ./rsync.sh test-pull"
echo " ./rsync.sh pull"
echo ""
echo "==========================="
}
alias_rsync () {
if [[ $1 == "push" ]]
then
$BASECOMMAND -e ssh ./ $SSHHOST:$SSHFILEPATH
elif [[ $1 == "pull" ]]
then
$BASECOMMAND -e ssh $SSHHOST:$SSHFILEPATH ./
elif [[ $1 == "test-push" ]]
then
$DRYCOMMAND -e ssh ./ $SSHHOST:$SSHFILEPATH | egrep '^<'
elif [[ $1 == "test-pull" ]]
then
$DRYCOMMAND -e ssh $SSHHOST:$SSHFILEPATH ./ | egrep '^<'
else
help_text
fi
}
id_rsa_rsync () {
if [[ $1 == "push" ]]
then
$BASECOMMAND -e "$SSHIDRSA" ./ $SSHUSERNAME@$SSHHOST:$SSHFILEPATH
elif [[ $1 == "pull" ]]
then
$BASECOMMAND -e "$SSHIDRSA" $SSHUSERNAME@$SSHHOST:$SSHFILEPATH ./
elif [[ $1 == "test-push" ]]
then
$DRYCOMMAND -e "$SSHIDRSA" ./ $SSHUSERNAME@$SSHHOST:$SSHFILEPATH | egrep '^<'
elif [[ $1 == "test-pull" ]]
then
$DRYCOMMAND -e "$SSHIDRSA" $SSHUSERNAME@$SSHHOST:$SSHFILEPATH ./ | egrep '^<'
else
help_text
fi
}
basic_rsync () {
if [[ $1 == "push" ]]
then
$BASECOMMAND ./ $SSHUSERNAME@$SSHHOST:$SSHFILEPATH
elif [[ $1 == "pull" ]]
then
$BASECOMMAND $SSHUSERNAME@$SSHHOST:$SSHFILEPATH ./
elif [[ $1 == "test-push" ]]
then
$DRYCOMMAND ./ $SSHUSERNAME@$SSHHOST:$SSHFILEPATH | egrep '^<'
elif [[ $1 == "test-pull" ]]
then
$DRYCOMMAND $SSHUSERNAME@$SSHHOST:$SSHFILEPATH ./ | egrep '^<'
else
help_text
fi
}
if [[ $TYPE == "alias" ]]
then
alias_rsync $1
elif [[ $TYPE == "id_rsa" ]]
then
id_rsa_rsync $1
else
basic_rsync $1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment