PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sat Dec 07, 2019 1:47 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Fri Nov 11, 2011 7:03 am 
Offline
Site Administrator
User avatar

Joined: Sun May 19, 2002 10:24 pm
Posts: 6887
This is just a quick and dirty script I wrote which will dump all MySQL databases to individual compressed files in a folder. It's designed to run from the command line. I am sharing this because I wasn't able to find anything similar.

Syntax: [ Download ] [ Hide ]
<?php
# configuration
define('USER', 'root');
define('PASS', '');
define('HOST', 'localhost');
define('PATH', '/var/www/databases/');

# databases to skip
$skip = array(
    'information_schema',
    'mysql',
);
# end config

try {
    if (!class_exists('mysqli')) {
        throw new Exception("The MySQLi extension is not installed.");
    }

    $db = new mysqli(HOST, USER, PASS);

    if ($db->connect_error) {
        throw new Exception($db->connect_error);
    }

    if (false == $result = $db->query('SHOW DATABASES;')) {
        throw new Exception($db->error);
    }

    while ($row = $result->fetch_object()) {
        $database = $row->Database;

        if (in_array($database, $skip)) {
            continue;
        }

        echo "Dumping database: $database";
        exec("mysqldump -u" . USER . " -p" . PASS . " $database > " . PATH . "$database.sql");
        echo "\r\033[K";
        echo "Compressing database: $database";
        exec("bzip2 " . PATH . "$database.sql");
        echo "\r\033[K";
    }

    echo "All databases exported.\n";
} catch (Exception $e) {
    echo $e->getMessage();
}


 

_________________
Image


Top
 Profile  
 
PostPosted: Fri Nov 11, 2011 3:35 pm 
Offline
Moderator
User avatar

Joined: Mon Nov 03, 2003 7:13 pm
Posts: 5978
Location: Odessa, Ukraine
I'd imagine it's just a bash one-liner:
Syntax: [ Download ] [ Hide ]
mysql -N -uroot -ptoor -e 'show databases'| grep -vi information_schema |grep -vi mysql| while read db; do mysqldump -uroot -ptoor "$db" | bzip2 > "$db.sql.bz2"; done
 


Top
 Profile  
 
PostPosted: Sun Nov 13, 2011 6:03 pm 
Offline
Site Administrator
User avatar

Joined: Sun May 19, 2002 10:24 pm
Posts: 6887
Thanks, Weirdan. Trying to write that would make my head hurt. It may come in handy though :)

_________________
Image


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group