Skip to content

Instantly share code, notes, and snippets.

@brycehamrick
Created February 12, 2013 18:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brycehamrick/4772168 to your computer and use it in GitHub Desktop.
Save brycehamrick/4772168 to your computer and use it in GitHub Desktop.
Shell script & PHP script to backup a wordpress instance to a Box account
#! /bin/bash
echo Backup Started `date` >> ~/backups/backup.log
mkdir ~/backups/data/`date +%Y%m%d`
tar -czf ~/backups/data/`date +%Y%m%d`/`date +%Y%m%d`_wp3.tar.gz ~/wp3
mysqldump -uwp3 -ppassword wp3 > ./wp3.sql
tar -czf ~/backups/data/`date +%Y%m%d`/`date +%Y%m%d`_sql.tar.gz ./wp3.sql
rm ./wp3.sql
find ~/backups/data -mtime +20 -type f -exec rm -rf {} \;
echo Backup Completed `date` >> ~/backups/backup.log
00 04 * * * ~/backups/backup.sh
00 05 01,15 * * php ~/backups/upload.php
<?php
require_once('lib/Box_Rest_Client.php');
class Backup {
const apikey = 'xxx';
const targetDir = 211925140;
const mysql = 'sql.tar.gz';
const filesystem = 'wp3.tar.gz';
private $token;
private $box;
private $dir;
public function init() {
$this->token = 'xxx';
$this->box = new Box_Rest_Client(self::apikey);
$this->box->auth_token = $this->token;
$this->setDir();
$this->backupStart();
}
private function backupStart() {
$one = $this->upload(self::mysql);
$this->log($one);
$two = $this->upload(self::filesystem);
$this->log($two);
}
private function upload($file_name) {
$file_name = $this->dir . '_' . $file_name;
$file_path = $this->fullPath($file_name);
$file = new Box_Client_File($file_path, $file_name);
$file->attr('folder_id', self::targetDir);
return $this->box->upload($file);
}
private function setDir() {
$contents = scandir(dirname(__FILE__) . '/data');
usort($contents, array($this, 'sortDir'));
$contents = array_slice($contents, 2);
$this->dir = $contents[0];
}
private function sortDir($a, $b) {
$a = strtotime($a);
$b = strtotime($b);
if ($a == $b) {
return 0;
}
return ($a > $b) ? -1 : 1;
}
private function fullPath($file='') {
return dirname(__FILE__) . '/data/' . $this->dir . '/' . $file;
}
private function log($response) {
$log = "upload.log";
$fh = fopen($log, 'a');
$string = date("c") . ": " . $response . "\n";
fwrite($fh, $string);
fclose($fh);
}
}
$backup = new Backup;
$backup->init();cron
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment