Skip to content

Instantly share code, notes, and snippets.

@misteio
Last active August 29, 2015 14:01
Show Gist options
  • Save misteio/a77a7d246a2f4a5df92b to your computer and use it in GitHub Desktop.
Save misteio/a77a7d246a2f4a5df92b to your computer and use it in GitHub Desktop.
Sync File + Sync DataBase from Local To Another Server
#!/bin/bash
#
# run as a cron job to synchronize Files + SQL Right Tables from the local server to live amazon server.
#
# set environment
dt=`date +%d%m%Y`
timenow=`date +%T`
logfile="sync.log"
errfile="sync.err"
machine=`hostname`
# set some variables
user1=""
user2=""
pw1=""
pw2=""
db1="" #whatever your database name is to sync TO
db2="" #whatever your database name is to sync FROM
server1=""
server2=""
sqldump="$dt-$timenow-dump.sql"
echo "**************************************" >> $logfile
echo "Date: $dt - $timenow " >> $logfile
echo "Sync log " >> $logfile
echo "**************************************" >> $logfile
echo "" >> $logfile
echo "" >> $logfile
echo "Syncing FILES" >> $logfile
sudo rsync -r -a -v -e "ssh -i /home/rsynckey" --exclude _tmp --exclude log --exclude logs --delete /folder hostname:/folder >> $logfile 2>> $errfile
echo "**************************************" >> $logfile
echo "Dump Database" >> $logfile
mysqldump -u $user1 --password=$pw1 $db1 --ignore-table=database.table > $sqldump >> $logfile 2>> $errfile
echo "**************************************" >> $logfile
echo "Insert Database" >> $logfile
mysql -u $user2 $db2 -h $server2 --port=3306 --password=$pw2 < $sqldump >> $logfile 2>> $errfile
echo "**************************************" >> $logfile
echo "**************DONE****************" >> $logfile
echo "" >> $logfile
echo "" >> $logfile
echo "" >> $logfile
#
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment