Showing posts with label database backup. Show all posts
Showing posts with label database backup. Show all posts

Friday, August 3, 2018

dump/export heroku database

You could just make your own pg_dump directly from your Heroku database.
First, get your postgres string using heroku config:get DATABASE_URL.
Look for the Heroku Postgres url (example: HEROKU_POSTGRESQL_RED_URL: postgres://user3123:passkja83kd8@ec2-117-21-174-214.compute-1.amazonaws.com:6212/db982398), which format is postgres://<username>:<password>@<host_name>:<port>/<dbname>.
Next, run this on your command line:
pg_dump --host=<host_name> --port=<port> --username=<username> --password <dbname> > output.sql
OR
pg_dump <heroku postgres string> > output.sql
The terminal will ask for your password then run it and dump it into output.sql.
Then import it: to your localhost db
psql -d my_local_database -f output.sql

Tuesday, May 29, 2018

Download Database Back_up using Codeigniter

//Backup manually
    public function CI_db_backup() {
        // Load the DB utility class
        $this->load->dbutil();
        $this->load->database();
        $dbName = $this->db->database;
        $prefs = array(
            "format" => 'sql', // gzip, zip, txt
            "filename" => $dbName.'.sql', // File name 
        );

// Backup your entire database and assign it to a variable
        $backup = $this->dbutil->backup($prefs);

        $folder = FCPATH . "/storage_db/db_backup/";
        $date = date("m-d-Y-H-i-s", time());
        $filename = $dbName . "_" . $date . ".sql";
// Load the file helper and write the file to your server
        $this->load->helper('file');
        write_file($folder . $filename, $backup);

// Load the download helper and send the file to your desktop
        $this->load->helper("download");
        force_download($filename, $backup);
    }

Friday, May 11, 2018

Database Backup using php Mysql


//Backup Manually
$dbHost = 'localhost';
$dbName = 'billing';
$dbUsername = 'root';
$dbPassword = '';
$tables = '*';
$return = '';
$folder = "db_backup/";

//connect & select the database
$db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);

//get all of the tables
if ($tables == '*') {
    $tables = array();
    $result = $db->query("SHOW TABLES");
    while ($row = $result->fetch_row()) {
        $tables[] = $row[0];
    }
} else {
    $tables = is_array($tables) ? $tables : explode(',', $tables);
}

//loop through the tables
foreach ($tables as $table) {
    $result = $db->query("SELECT * FROM $table");
    $numColumns = $result->field_count;

    $return .= "DROP TABLE IF EXISTS $table;";

    $result2 = $db->query("SHOW CREATE TABLE $table");
    $row2 = $result2->fetch_row();

    $return .= "\n\n" . $row2[1] . ";\n\n";

    for ($i = 0; $i < $numColumns; $i++) {
        while ($row = $result->fetch_row()) {
            $return .= "INSERT INTO $table VALUES(";
            for ($j = 0; $j < $numColumns; $j++) {
                $row[$j] = addslashes($row[$j]);
                $row[$j] = preg_replace("/\n/", "/\\n/", $row[$j]);
                if (isset($row[$j])) {
                    $return .= '"' . $row[$j] . '"';
                } else {
                    $return .= '""';
                }
                if ($j < ($numColumns - 1)) {
                    $return .= ',';
                }
            }
            $return .= ");\n";
        }
    }

    $return .= "\n\n\n";
}

// Create Backup Folder
if (!is_dir($folder))
    mkdir($folder, 0775, true);
//chmod($folder, 0775);

$date = date('m-d-Y-H-i-s', time());
$filename = $dbName . "_" . $date . ".sql";
$path = $folder . $filename;

//save file
$handle = fopen($path, 'w+');
fwrite($handle, $return);
fclose($handle);

if (file_exists($path)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="' . basename($path) . '"');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($path));
    readfile($path);
//            echo $return;
    exit;
}

NOTE: when you import this database into your phpmyadmin at that time uncheck the Enable foreign key checks 

Product Category Demo in laravel

https://drive.google.com/file/d/1kuyeT3LA22IuN3o_kypOyXesEXMLv31e/view?usp=sharing