MySQL/MariaDB backups with php

I’ve probably written about this before, but it’s still something that I don’t follow through often enough, or take care of things properly.

Here’s a simple script that you can either run manually, or set up a cron job to dump your full database if you are using php. You’ll have to configure it, but it calls the filename the DB name, along with the date and time of the execution, and a random number are the end.

I hope this helps you.


ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);

$dbhost = 'localhost';
$dbuser = ';
$dbpass = '';
$dbname = '';
$exportpath = '/home/db/backups';

$time = time();
$date = date('Ymd_His_', $time);

$backupFile = $exportpath . '/' . $dbname . '_' . $date . '_' . (rand(10,10000)) . '.gz';

if (file_exists($backupFile)) {

$cmd = "/usr/bin/mysqldump --single-transaction -u $dbuser -p$dbpass $dbname | gzip > {$backupFile}";
// See the command that was called.
echo $cmd;

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a free website or blog at

Up ↑

%d bloggers like this: