Skip to content

Instantly share code, notes, and snippets.

@ericjsilva
Created August 16, 2013 14:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericjsilva/6250250 to your computer and use it in GitHub Desktop.
Save ericjsilva/6250250 to your computer and use it in GitHub Desktop.
Quickly and easily backup your MySQL database and have the .tgz copied to your Dropbox account. Requires the DropboxUploader.php utility here: https://github.com/jakajancar/DropboxUploader/blob/master/DropboxUploader.php Original post about this script can be found at my website: http://ericsilva.org/2012/07/05/backup-mysql-database-to-dropbox
<?php
/**
* Quickly and easily backup your MySQL database and have the .tgz copied to
* your Dropbox account.
*
* Copyright (c) 2012 Eric Silva
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author Eric Silva [eric@ericsilva.org] [http://ericsilva.org/]
* @version 1.0.0
*/
require('DropboxUploader.php');
// location of your temp directory
$tmpDir = "/tmp/";
// username for MySQL
$user = "username";
// password for MySQL
$password = "password";
// database name to backup
$dbName = "my_database";
// hostname or IP where database resides
$dbHost = "localhost";
// the zip file emailed to you will have this prefixed
$prefix = "db_";
// username for Dropbox
$dropbox_user = "dropbox_account";
// password for Dropbox
$dropbox_password = "dropbox_password";
// Destination folder in Dropbox (folder will be created if doesn't yet exist)
$dropbox_dest = "db_backups";
// Create the database backup file
$sqlFile = $tmpDir.$prefix.date('Y_m_d').".sql";
$backupFilename = $prefix.date('Y_m_d').".tgz";
$backupFile = $tmpDir.$backupFilename;
$createBackup = "mysqldump -h ".$dbHost." -u ".$user." --password='".$password."' ".$dbName." > ".$sqlFile;
//echo $createBackup;
$createZip = "tar cvzf $backupFile $sqlFile";
//echo $createZip;
exec($createBackup);
exec($createZip);
try {
// Upload database backup to Dropbox
$uploader = new DropboxUploader($dropbox_user, $dropbox_password);
$uploader->upload($backupFile, $dropbox_dest, $backupFilename);
} catch(Exception $e) {
die($e->getMessage());
}
// Delete the temporary files
unlink($sqlFile);
unlink($backupFile);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment