Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Load the DB utility class
- $this->load->dbutil();
- // Backup your entire database and assign it to a variable
- $backup =& $this->dbutil->backup();
- // Load the file helper and write the file to your server
- $this->load->helper('file');
- write_file('/path/to/mybackup.gz', $backup);
- // Load the download helper and send the file to your desktop
- $this->load->helper('download');
- force_download('mybackup.gz', $backup);
- $this->load->dbutil();
- $prefs = array(
- 'format' => 'zip',
- 'filename' => 'my_db_backup.sql'
- );
- $backup =& $this->dbutil->backup($prefs);
- $db_name = 'backup-on-'. date("Y-m-d-H-i-s") .'.zip';
- $save = 'pathtobkfolder/'.$db_name;
- $this->load->helper('file');
- write_file($save, $backup);
- $this->load->helper('download');
- force_download($db_name, $backup);
- header('Content-type: application/force-download');
- header('Content-Disposition: attachment; filename="dbbackup.sql.gz"');
- passthru("mysqldump --user=xx --host=xx --password=xx dbname | gzip");
- public function db_backup()
- {
- $this->load->dbutil();
- $backup =& $this->dbutil->backup();
- $this->load->helper('file');
- write_file('your_file_path/your_DB.zip', $backup);
- }
- function backup($fileName='db_backup.zip'){
- // Load the DB utility class
- $this->load->dbutil();
- // Backup your entire database and assign it to a variable
- $backup =& $this->dbutil->backup();
- // Load the file helper and write the file to your server
- $this->load->helper('file');
- write_file(FCPATH.'/downloads/'.$fileName, $backup);
- // Load the download helper and send the file to your desktop
- $this->load->helper('download');
- force_download($fileName, $backup);
- }
- public function db_backup()
- {
- $DBUSER=$this->db->username;
- $DBPASSWD=$this->db->password;
- $DATABASE=$this->db->database;
- $filename = $DATABASE . "-" . date("Y-m-d_H-i-s") . ".sql.gz";
- $mime = "application/x-gzip";
- header( "Content-Type: " . $mime );
- header( 'Content-Disposition: attachment; filename="' . $filename . '"' );
- // $cmd = "mysqldump -u $DBUSER --password=$DBPASSWD $DATABASE | gzip --best";
- $cmd = "mysqldump -u $DBUSER --password=$DBPASSWD --no-create-info --complete-insert $DATABASE | gzip --best";
- passthru( $cmd );
- exit(0);
- }
- function dbbackup()
- {
- $this->load->dbutil();
- $backup =& $this->dbutil->backup();
- $this->load->helper('file');
- write_file('<?php echo base_url();?>/downloads', $backup);
- $this->load->helper('download');
- force_download('mybackup.gz', $backup);
- }
- function backup()
- {
- $this->load->helper('download');
- $this->load->library('zip');
- $time = time();
- $this->zip->read_dir('D:xampp/htdocs/wms/');
- $this->zip->download('my_backup.'.$time.'.zip');
- }
- $username = "root";
- $password = "root";
- $hostname = "localhost";
- $dbname = "raas";
- header('Content-Description: File Transfer');
- header('Content-Type: application/octet-stream');
- header('Content-Disposition: attachment; filename='.basename($dbname . "_" .date("Y-m-d_H-i-s").".sql"));
- $command = "C:AppServMySQLbinmysqldump --add-drop-table --host=$hostname --user=$username --password=$password ".$dbname;
- system($command);
- $prefs = array(
- 'ignore'=> array('codes_cdt','codes_cpt','codes_icd10_dx_order','codes_icd10_pcs_order','pharma'),
- 'format'=>'gzip','filename','add_drop'=> TRUE,'add_insert'=>TRUE,'newline'=> "n");
- $filename = APPPATH.'\backups\' .'backup-' . date('d-m-Y') . ' .gz';
- if(!file_exists($filename)){
- get_instance()->load->dbutil();
- file_put_contents( $filename, $this->dbutil->backup($prefs));
- }
- <?
- // Try this one, this works FOR both codeigniter and core PHP
- public function Export_Database()
- {
- date_default_timezone_set('GMT');
- // Load the file helper in codeigniter
- $this->load->helper('file');
- $con = mysqli_connect("localhost","username","password","databasename");
- $tables = array();
- $query = mysqli_query($con, 'SHOW TABLES');
- while($row = mysqli_fetch_row($query)){
- $tables[] = $row[0];
- }
- $result = 'SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";';
- $result .= 'SET time_zone = "+00:00";';
- foreach($tables as $table){
- $query = mysqli_query($con, 'SELECT * FROM `'.$table.'`');
- $num_fields = mysqli_num_fields($query);
- $result .= 'DROP TABLE IF EXISTS '.$table.';';
- $row2 = mysqli_fetch_row(mysqli_query($con, 'SHOW CREATE TABLE `'.$table.'`'));
- $result .= "nn".$row2[1].";nn";
- for ($i = 0; $i < $num_fields; $i++) {
- while($row = mysqli_fetch_row($query)){
- $result .= 'INSERT INTO `'.$table.'` VALUES(';
- for($j=0; $j<$num_fields; $j++){
- $row[$j] = addslashes($row[$j]);
- $row[$j] = str_replace("n","\n",$row[$j]);
- if(isset($row[$j])){
- $result .= '"'.$row[$j].'"' ;
- }else{
- $result .= '""';
- }
- if($j<($num_fields-1)){
- $result .= ',';
- }
- }
- $result .= ");n";
- }
- }
- $result .="nn";
- }
- //Create Folder
- $folder = 'database/';
- if (!is_dir($folder))
- mkdir($folder, 0777, true);
- chmod($folder, 0777);
- $date = date('m-d-Y');
- $filename = $folder."db_filename_".$date;
- $handle = fopen($filename.'.sql','w+');
- fwrite($handle,$result);
- fclose($handle);
- redirect('Dashboard');
- } // end Export_Database function
- ?>
- // to intialize the path split the real path by dot .
- public function init_path($string){
- $array_path = explode('.', $string);
- $realpath = '';
- foreach ($array_path as $p)
- {
- $realpath .= $p;
- $realpath .= '/';
- }
- return $realpath;
- }
- // backup database function
- public function archive_database($host = '',$user ='',$pass ='',$name ='', $path = '' , $download_allow = false , $tables=false, $backup_name=false){
- $CI = &get_instance();
- $CI->load->database();
- if($path != '')
- {
- $path = realpath($this->init_path($path));
- }else{
- if (!is_dir('archives/'))
- mkdir('archives/', 0777);
- $path = realpath($this->init_path('archives'));
- }
- if($host == '')
- {
- $host = $CI->db->hostname;
- }
- if($user == '')
- {
- $user = $CI->db->username;
- }
- if($pass == '')
- {
- $pass = $CI->db->password;
- }
- if($name == '')
- {
- $name = $CI->db->database;
- }
- set_time_limit(3000);
- $mysqli = new mysqli($host,$user,$pass,$name);
- $mysqli->select_db($name);
- $mysqli->query("SET NAMES 'utf8'");
- $queryTables = $mysqli->query('SHOW TABLES');
- while($row = $queryTables->fetch_row()) { $target_tables[] = $row[0]; }
- if($tables !== false)
- { $target_tables = array_intersect( $target_tables, $tables); }
- $content = "SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";rnSET time_zone = "+00:00";rnrnrn/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;rn/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;rn/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;rn/*!40101 SET NAMES utf8 */;rn--rn-- Database: `".$name."`rn--rnrnrn";
- foreach($target_tables as $table){
- if (empty($table)){ continue; }
- $result = $mysqli->query('SELECT * FROM `'.$table.'`');
- $fields_amount=$result->field_count;
- $rows_num=$mysqli->affected_rows;
- $res = $mysqli->query('SHOW CREATE TABLE '.$table);
- $TableMLine=$res->fetch_row();
- $content .= "nn".$TableMLine[1].";nn";
- for ($i = 0, $st_counter = 0; $i < $fields_amount; $i++, $st_counter=0) {
- while($row = $result->fetch_row()) { //when started (and every after 100 command cycle):
- if ($st_counter%100 == 0 || $st_counter == 0 ) {$content .= "nINSERT INTO ".$table." VALUES";}
- $content .= "n("; for($j=0; $j<$fields_amount; $j++){ $row[$j] = str_replace("n","\n", addslashes($row[$j]) ); if (isset($row[$j])){$content .= '"'.$row[$j].'"' ;} else{$content .= '""';} if ($j<($fields_amount-1)){$content.= ',';} } $content .=")";
- //every after 100 command cycle [or at last line] ....p.s. but should be inserted 1 cycle eariler
- if ( (($st_counter+1)%100==0 && $st_counter!=0) || $st_counter+1==$rows_num) {$content .= ";";} else {$content .= ",";} $st_counter=$st_counter+1;
- }
- } $content .="nnn";
- }
- $content .= "rnrn/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;rn/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;rn/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;";
- $backup_name = $backup_name ? $backup_name : $name."___(".date('H-i-s')."_".date('d-m-Y').")__rand".rand(1,11111111).".sql";
- $fileLocation = $path .'\'. $backup_name;
- $file = fopen($fileLocation,"w");
- fwrite($file,$content);
- fclose($file);
- if($download_allow){
- ob_get_clean(); header('Content-Type: application/octet-stream'); header("Content-Transfer-Encoding: Binary"); header("Content-disposition: attachment; filename="".$backup_name.""");
- }
- echo $content; exit;
- }
Add Comment
Please, Sign In to add comment