1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Unattended offsite backup of your site (blogs, forum, etc...)

Discussion in 'Site & Server Administration' started by geeklingo, Dec 12, 2016.

  1. #1
    Probably teaching you all to suck eggs but here is how I do my unattended server backups including offsite. Hopefully some will find it useful. I use this method with WordPress and IPS/VB forums.

    The first step is to work out what you would like as your backup regime. I take a daily, weekly and monthly backup so have 3 php files (one for each type). This is what my daily_backup.php looks like:
    SEMrush
    <?php
    date_default_timezone_set("Australia/Brisbane");
    
    $command = "find /user/backup/daily/ -mtime +2 -exec mv {} /nonssddisk/backup/daily/ \;";
    system($command);
    
    $backupFile = '/user/backup/daily/sql_daily-' . date("Y-m-d-H-i") . '.gz';
    $command = "mysqldump --defaults-extra-file=/user/.my.cnf -u user -h localhost DatabaseName | gzip > $backupFile";
    system($command);
    
    $backupFile = '/user/backup/daily/site_daily-' . date("Y-m-d-H-i") . '.tgz';
    $command = "tar -czf $backupFile /path/to/html/files/";
    system($command);
    
    $command = "find /nonssddisk/backup/daily/ -mtime +3 -exec rm -r {} \;";
    system($command);
    
    ?>
    PHP:
    Now there is a lot going on there so let's break it down....

    I use a SSD based host with a larger nonSSD drive attached. Once my latest backups have aged a couple of days, I move them to the nonSSD drive.
    
    $command = "find /user/backup/daily/ -mtime +2 -exec mv {} /nonssddisk/backup/daily/ \;";
    system($command);
    
    PHP:
    Next up is the dumping and zipping of the site database. I always attached time and date to the filename.
    
    $backupFile = '/user/backup/daily/sql_daily-' . date("Y-m-d-H-i") . '.gz';
    $command = "mysqldump --defaults-extra-file=/user/.my.cnf -u user -h localhost DatabaseName | gzip > $backupFile";
    system($command);
    
    PHP:
    Now we archive the site files, basically anything not stored on the database.
    
    $backupFile = '/user/backup/daily/site_daily-' . date("Y-m-d-H-i") . '.tgz';
    $command = "tar -czf $backupFile /path/to/html/files/";
    system($command);
    
    PHP:
    Finally, we remove any old backups.
    
    $command = "find /nonssddisk/backup/daily/ -mtime +3 -exec rm -r {} \;";
    system($command);
    
    PHP:
    That's the backup php script done, now we need to ensure this runs every day. To do this, create a cron job:
    # Example of job definition:
    # .---------------- minute (0 - 59)
    # |  .------------- hour (0 - 23)
    # |  |  .---------- day of month (1 - 31)
    # |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
    # |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
    # |  |  |  |  |
    # *  *  *  *  * user-name  command to be executed
    #Daily Backup
    0 1 * * * user /usr/bin/php /user/backup/daily_backup.php
    Code (markup):
    This now gives you an automated daily backup of your site which is stored locally to that site. Do the same for the weekly and monthly scripts. The final piece of the puzzle is to create an offsite copy. There are many ways in which this can be achieved like using Amazon S3, FTP or in this example Rsync to another server.
    #Sync to Dev
    0 2 * * * localuser /usr/bin/rsync -avz -e "ssh -p 4001 -i /localuser/.ssh/id_rsa" /user/backup/ remoteuser@remotesite.com:/home/remoteuser/backup
    0 2 * * * localuser /usr/bin/rsync -avz -e "ssh -p 4001 -i /localuser/.ssh/id_rsa" /nonssddisk/backup/ remoteuser@remotesite.com:/home/remoteuser/backup
    Code (markup):
     
    geeklingo, Dec 12, 2016 IP
    SEMrush
  2. geeklingo

    geeklingo Member

    Messages:
    27
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    31
    #2
    And if you want to sync to amazon you can use something like s3cmd
     
    geeklingo, Dec 15, 2016 IP