Bash scripts for syncing local and remote dev servers

## drop all tables in the local db
mysql -u[local user] -p[local pass]
DROP DATABASE [local db name];
CREATE DATABASE [local db name];
exit
## In one command, dump the remote into local ##
ssh -C [remote host] mysqldump -u[remote user] -p[remote pass] [remote db name] | mysql -u[local user] -p[local pass] [local db name]
 
## clear caches
cd /[local site docroot]
drush cc all
 
## git push
cd /[local site docroot]
git push [remote] master
 
## git pull
cd /[local site docroot]
git pull [remote] master
 
## to make a backup of remote db
cd ~/[projects metadata directory]/[local site docroot]/dbdumps/
DATEVAR=`date +%Y-%m-%d--%H-%M-%S`
ssh -C [remote host] mysqldump -u[remote user] -p[remote pass] [remote db name] > [local site docroot]_$DATEVAR.sql
 
## to make a backup of local db
cd /[local site docroot]
drush cc all
cd ~/[projects metadata directory]/[local site docroot]/dbdumps/
DATEVAR=`date +%Y-%m-%d--%H-%M-%S`
mysqldump -u[local user] -p[local pass] [local db name] > [local site docroot]_local_$DATEVAR.sql
 
**to restore from backup**
cd ~/[projects metadata directory]/[local site docroot]/dbdumps/
ls -al
mysql -u[local user] -p[local pass] [local db name] < [db name].sql
 
 
######################################################
###CAUTION! NEXT SECTION WRITES TO THE PROD SERVER!!!
######################################################
 
## backup local db and copy it to remote
cd /[local site docroot]
drush cc all
cd ~/[projects metadata directory]/[local site docroot]/dbdumps/
DATEVAR=`date +%Y-%m-%d--%H-%M-%S`
mysqldump -u[local user] -p[local pass] [local db name] > [local site docroot]_local_$DATEVAR.sql
scp [local site docroot]_local_$DATEVAR.sql [remote host]:~/dbdumps
 
## empty remote db, restore from backup, clear caches
ssh [remote host]
mysql -u[remote user] -p[remote pass]
DROP DATABASE [remote db name];
CREATE DATABASE [remote db name];
exit
 
cd ~/dbdumps/
ls -al
mysql -u[remote user] -p[remote pass] [remote db name] < [db name].sql
 
cd ~/public_html/[local site docroot]
drush cc all

Internal References

Article Type

General