Search code examples
phpsynchronizationrsyncfallbackmirroring

Synchronizing folders between two servers


I have to realize a fallback solution (a auth system) for an external application. Therefore I have to keep the auth folder of my primary auth server syncronized with my fallback servers. The folder contains several .php files, .bin files and some others. Unfortunately I have no idea how I should realize a (for example hourly) syncronization of those folders to my fallback servers.

All servers use CPanel / WHM, maybe there is a solution for this or how can I keep them synced otherwise? I thought about a .php script which logs in via FTP and syncronizes them. I would put a cronjob then for this .php script. But I don't even know whether this is possible. If the primary server is offline it shouldn't affect my fallback servers in a negative way of course.

How should/can I realize this?


Solution

  • Leonel Atencio's suggestion of rsync is great.

    Here is the rsync shell script that I use. It is placed in a folder named /publish in my project. The gist contains the rs_exclude.txt file the shell script mentions.

    rsync.sh

    # reverse the comments on the next two lines to do a dry run
    #dryrun=--dry-run
    dryrun=
    
    c=--compress
    exclude=--exclude-from=rs_exclude.txt
    pg="--no-p --no-g"
    
    #delete is dangerous - use caution.  I deleted 15 years worth of digital photos using rsync with delete turned on.
    # reverse the comments on the next two lines to enable deleting
    #delete=--delete
    delete=
    
    rsync_options=-Pav
    rsync_local_path=../
    rsync_server_string=user@example.com
    rsync_server_path="/home/www.example.com"
    
    # choose one.
    #rsync $rsync_options $dryrun $delete $exclude $c $pg $rsync_local_path $rsync_server_string:$rsync_server_path
    
    #how to specify an alternate port
    #rsync -e "ssh -p 2220" $dryrun $delete $exclude $c $pg $rsync_local_path $rsync_server_string:$rsync_server_path
    

    https://gist.github.com/treehousetim/2a7871f87fa53007f17e

    running via cron

    Cron
    Source

    Edit your crontab.

    # crontab -e
    

    Crontab entries are one per line. The comment character is the pound (#) symbol. Use the following syntax for your cron entry.

    These examples assume you placed your rsync.sh script in ~/rsync These examples will also create log files of the rsync output.

    Each Minute

    * * * * * ~/rsync/rsync.sh > ~/rsync/rsync.log
    

    Every 5 Minutes

    */5 * * * * ~/rsync/rsync.sh > ~/rsync/rsync.log
    

    Save your crontab and exit the editor. You should see a message confirming your addition to the crontab.