0

I have a database created in mysql iwant to create a backup utility for backing up the data on a routine base

3
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by simplypixie
0

Use the command line tool mysqldump to dump the whole database to a file which you can then store at a safe place.

1

This is the code I use and run a cron job:

backup_tables('user','password','database');


/* backup the db OR just a table */
function backup_tables($user,$pass,$db)
{

    $suffix = date("mdY-Hi");
  #Execute the command to create backup sql file
  exec("mysqldump --user={$user} --password={$pass} --quick --add-drop-table --add-locks --extended-insert --lock-tables --all {$db} > ../db_backups/backup.sql");

  #Now zip that file
  $zip = new ZipArchive();
  $filename = "../db_backups/backup-$suffix.zip";
  if ($zip->open($filename, ZIPARCHIVE::CREATE) !== TRUE) {
   exit("cannot open <$filename>\n");
  }
  $zip->addFile("../db_backups/backup.sql" , "backup.sql");
  $zip->close();
  #Now delete the .sql file without any warning
  @unlink("../db_backups/backup.sql");
  #Return the path to the zip backup file
  return "../db_backups/backup-$suffix.zip";
}
Votes + Comments
agree
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.