Skip to content

Instantly share code, notes, and snippets.

@mdestafadilah
Created May 30, 2022 09:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mdestafadilah/e346c7995e469dd8bcdf7a99b5d60f07 to your computer and use it in GitHub Desktop.
Save mdestafadilah/e346c7995e469dd8bcdf7a99b5d60f07 to your computer and use it in GitHub Desktop.
BACKUP DB MYSQL DUMP
$this->load->helper(array('url','file','download'));
$this->load->library('zip');
$website = $this->load->database('website', TRUE); // the TRUE paramater tells CI that you'd like to return the database object.
$DBUSER=$website->username;
$DBPASSWD=$website->password;
$DATABASE=$website->database;
$HOSTNAME=$website->hostname;
$db_name = 'backup-on-' . date("Y-m-d-H-i-s") . '.sql';
$backup_path = APPPATH.'/assets/backup/'; // this is the destination directory name
if (!file_exists($backup_path)) {
mkdir($backup_path, 0755, true);
}
$save = $backup_path.$db_name;
// Windows
$command = 'C:\laragon\bin\mysql\mysql-5.7.24-winx64\bin\mysqldump --opt --host='.$HOSTNAME.' -u '.$DBUSER.' -p'.$DBPASSWD.' '.$DATABASE.' > '.$save.' 2>&1';
// Linux
// $command = 'mysqldump --opt --host='.$HOSTNAME.' -u '.$DBUSER.' -p'.$DBPASSWD.' '.$DATABASE.' > '.$save.' 2>&1';
passthru( $command );
exit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment