Script to sync CMS between remote locations

Discussion in 'Server Operation' started by rafael-ec, Jul 13, 2012.

  1. rafael-ec

    rafael-ec New Member

    Hi,

    I wrote this simple script to sync sites between remote locations. I have tested with joomla and it works good, but I think it needs to be improved.

    The script works good with the root user using login with authentication keys.

    My concerns are related to the database. I am using rsync for mysql sync, it has work find, but something tells me it is not the best idea. Specially if the origin is writing at the moment of sync. Ideas to improve this script?

    Code:
    
    #!/bin/bash
    
    #### Configuration web servers###
    
    #Remote Web server
    servidorWebDest=10.20.20.20
    
    #local directorio. Be sure to include "/" at the ende
    dirWebLocal='/var/www/html/'
    
    #remote directory to sync
    dirWebRemoto='/var/www/html/'
    
    #exclude files from sync
    exluidos='configuration.php'
    
    #remote user
    usuarioSshWeb='root'
    
    #### Data Base###
    #Remote database server
    servidorRemotoBd=10.20.20.50
    
    #Ssh user for the remote database
    usuarioSshBd='root'
    
    #local database directory. Put "/" at the end
    dirBdLocal='/var/lib/mysql/dataName'
    
    #remote database directory
    dirBdRemota='/var/lib/mysql/dataName'
    
    #Sync web server
    rsync -azpP --exclude $exluidos $dirWebLocal $usuarioSshWeb@$servidorWebDest:$dirWebRemoto
    
    echo "Stop remote database"
    ssh $usuarioSshBd@$servidorRemotoBd service mysqld stop
    
    
    #Sync Database
    rsync -azpP  $dirBdLocal $usuarioSshBd@$servidorRemotoBd:$dirBdRemota
    
    
    #Start database
    echo "Start remote database"
    ssh $usuarioSshBd@$servidorRemotoBd service mysqld start                                               
    
     
  2. bozeak

    bozeak New Member

    I didn't try this script, but I think i'll have to!)
    Big thanks!!!
     
  3. amergrgic

    amergrgic New Member

    hi rafael,

    instead of using rsync to export and import the database you should use mysqldump, below is a quick example of a bash script i've made. What you could do is use rsync to copy the tar file to the new server, extract it there and then use mysql -p <database name> < <name of sql file>.sql.

     

Share This Page