Guest User

Untitled

a guest
May 24th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.13 KB | None | 0 0
  1. // Load the DB utility class
  2. $this->load->dbutil();
  3.  
  4. // Backup your entire database and assign it to a variable
  5. $backup =& $this->dbutil->backup();
  6.  
  7. // Load the file helper and write the file to your server
  8. $this->load->helper('file');
  9. write_file('/path/to/mybackup.gz', $backup);
  10.  
  11. // Load the download helper and send the file to your desktop
  12. $this->load->helper('download');
  13. force_download('mybackup.gz', $backup);
  14.  
  15. $this->load->dbutil();
  16.  
  17. $prefs = array(
  18. 'format' => 'zip',
  19. 'filename' => 'my_db_backup.sql'
  20. );
  21.  
  22.  
  23. $backup =& $this->dbutil->backup($prefs);
  24.  
  25. $db_name = 'backup-on-'. date("Y-m-d-H-i-s") .'.zip';
  26. $save = 'pathtobkfolder/'.$db_name;
  27.  
  28. $this->load->helper('file');
  29. write_file($save, $backup);
  30.  
  31.  
  32. $this->load->helper('download');
  33. force_download($db_name, $backup);
  34.  
  35. header('Content-type: application/force-download');
  36. header('Content-Disposition: attachment; filename="dbbackup.sql.gz"');
  37. passthru("mysqldump --user=xx --host=xx --password=xx dbname | gzip");
  38.  
  39. public function db_backup()
  40. {
  41. $this->load->dbutil();
  42. $backup =& $this->dbutil->backup();
  43. $this->load->helper('file');
  44. write_file('your_file_path/your_DB.zip', $backup);
  45. }
  46.  
  47. function backup($fileName='db_backup.zip'){
  48. // Load the DB utility class
  49. $this->load->dbutil();
  50.  
  51. // Backup your entire database and assign it to a variable
  52. $backup =& $this->dbutil->backup();
  53.  
  54. // Load the file helper and write the file to your server
  55. $this->load->helper('file');
  56. write_file(FCPATH.'/downloads/'.$fileName, $backup);
  57.  
  58. // Load the download helper and send the file to your desktop
  59. $this->load->helper('download');
  60. force_download($fileName, $backup);
  61. }
  62.  
  63. public function db_backup()
  64. {
  65. $DBUSER=$this->db->username;
  66. $DBPASSWD=$this->db->password;
  67. $DATABASE=$this->db->database;
  68.  
  69. $filename = $DATABASE . "-" . date("Y-m-d_H-i-s") . ".sql.gz";
  70. $mime = "application/x-gzip";
  71.  
  72. header( "Content-Type: " . $mime );
  73. header( 'Content-Disposition: attachment; filename="' . $filename . '"' );
  74.  
  75. // $cmd = "mysqldump -u $DBUSER --password=$DBPASSWD $DATABASE | gzip --best";
  76. $cmd = "mysqldump -u $DBUSER --password=$DBPASSWD --no-create-info --complete-insert $DATABASE | gzip --best";
  77.  
  78. passthru( $cmd );
  79.  
  80. exit(0);
  81. }
  82.  
  83. function dbbackup()
  84. {
  85. $this->load->dbutil();
  86. $backup =& $this->dbutil->backup();
  87. $this->load->helper('file');
  88. write_file('<?php echo base_url();?>/downloads', $backup);
  89. $this->load->helper('download');
  90. force_download('mybackup.gz', $backup);
  91. }
  92.  
  93. function backup()
  94. {
  95. $this->load->helper('download');
  96. $this->load->library('zip');
  97. $time = time();
  98. $this->zip->read_dir('D:xampp/htdocs/wms/');
  99. $this->zip->download('my_backup.'.$time.'.zip');
  100. }
  101.  
  102. $username = "root";
  103. $password = "root";
  104. $hostname = "localhost";
  105. $dbname = "raas";
  106.  
  107. header('Content-Description: File Transfer');
  108. header('Content-Type: application/octet-stream');
  109. header('Content-Disposition: attachment; filename='.basename($dbname . "_" .date("Y-m-d_H-i-s").".sql"));
  110.  
  111. $command = "C:AppServMySQLbinmysqldump --add-drop-table --host=$hostname --user=$username --password=$password ".$dbname;
  112.  
  113. system($command);
  114.  
  115. $prefs = array(
  116. 'ignore'=> array('codes_cdt','codes_cpt','codes_icd10_dx_order','codes_icd10_pcs_order','pharma'),
  117. 'format'=>'gzip','filename','add_drop'=> TRUE,'add_insert'=>TRUE,'newline'=> "n");
  118.  
  119. $filename = APPPATH.'\backups\' .'backup-' . date('d-m-Y') . ' .gz';
  120. if(!file_exists($filename)){
  121. get_instance()->load->dbutil();
  122. file_put_contents( $filename, $this->dbutil->backup($prefs));
  123. }
  124.  
  125. <?
  126.  
  127. // Try this one, this works FOR both codeigniter and core PHP
  128.  
  129. public function Export_Database()
  130. {
  131. date_default_timezone_set('GMT');
  132. // Load the file helper in codeigniter
  133. $this->load->helper('file');
  134.  
  135.  
  136. $con = mysqli_connect("localhost","username","password","databasename");
  137.  
  138. $tables = array();
  139. $query = mysqli_query($con, 'SHOW TABLES');
  140. while($row = mysqli_fetch_row($query)){
  141. $tables[] = $row[0];
  142. }
  143.  
  144. $result = 'SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";';
  145. $result .= 'SET time_zone = "+00:00";';
  146.  
  147. foreach($tables as $table){
  148. $query = mysqli_query($con, 'SELECT * FROM `'.$table.'`');
  149. $num_fields = mysqli_num_fields($query);
  150.  
  151. $result .= 'DROP TABLE IF EXISTS '.$table.';';
  152. $row2 = mysqli_fetch_row(mysqli_query($con, 'SHOW CREATE TABLE `'.$table.'`'));
  153. $result .= "nn".$row2[1].";nn";
  154.  
  155. for ($i = 0; $i < $num_fields; $i++) {
  156. while($row = mysqli_fetch_row($query)){
  157. $result .= 'INSERT INTO `'.$table.'` VALUES(';
  158. for($j=0; $j<$num_fields; $j++){
  159. $row[$j] = addslashes($row[$j]);
  160. $row[$j] = str_replace("n","\n",$row[$j]);
  161. if(isset($row[$j])){
  162. $result .= '"'.$row[$j].'"' ;
  163. }else{
  164. $result .= '""';
  165. }
  166. if($j<($num_fields-1)){
  167. $result .= ',';
  168. }
  169. }
  170. $result .= ");n";
  171. }
  172. }
  173. $result .="nn";
  174. }
  175.  
  176. //Create Folder
  177. $folder = 'database/';
  178. if (!is_dir($folder))
  179. mkdir($folder, 0777, true);
  180. chmod($folder, 0777);
  181.  
  182. $date = date('m-d-Y');
  183. $filename = $folder."db_filename_".$date;
  184.  
  185. $handle = fopen($filename.'.sql','w+');
  186. fwrite($handle,$result);
  187. fclose($handle);
  188. redirect('Dashboard');
  189.  
  190.  
  191. } // end Export_Database function
  192. ?>
  193.  
  194. // to intialize the path split the real path by dot .
  195. public function init_path($string){
  196. $array_path = explode('.', $string);
  197. $realpath = '';
  198. foreach ($array_path as $p)
  199. {
  200.  
  201. $realpath .= $p;
  202. $realpath .= '/';
  203. }
  204. return $realpath;
  205.  
  206. }
  207.  
  208. // backup database function
  209. public function archive_database($host = '',$user ='',$pass ='',$name ='', $path = '' , $download_allow = false , $tables=false, $backup_name=false){
  210. $CI = &get_instance();
  211. $CI->load->database();
  212. if($path != '')
  213. {
  214. $path = realpath($this->init_path($path));
  215.  
  216. }else{
  217. if (!is_dir('archives/'))
  218. mkdir('archives/', 0777);
  219. $path = realpath($this->init_path('archives'));
  220. }
  221. if($host == '')
  222. {
  223. $host = $CI->db->hostname;
  224.  
  225. }
  226. if($user == '')
  227. {
  228. $user = $CI->db->username;
  229.  
  230. }
  231. if($pass == '')
  232. {
  233. $pass = $CI->db->password;
  234.  
  235. }
  236. if($name == '')
  237. {
  238. $name = $CI->db->database;
  239.  
  240. }
  241.  
  242. set_time_limit(3000);
  243. $mysqli = new mysqli($host,$user,$pass,$name);
  244. $mysqli->select_db($name);
  245. $mysqli->query("SET NAMES 'utf8'");
  246. $queryTables = $mysqli->query('SHOW TABLES');
  247. while($row = $queryTables->fetch_row()) { $target_tables[] = $row[0]; }
  248. if($tables !== false)
  249. { $target_tables = array_intersect( $target_tables, $tables); }
  250. $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";
  251. foreach($target_tables as $table){
  252. if (empty($table)){ continue; }
  253. $result = $mysqli->query('SELECT * FROM `'.$table.'`');
  254. $fields_amount=$result->field_count;
  255. $rows_num=$mysqli->affected_rows;
  256. $res = $mysqli->query('SHOW CREATE TABLE '.$table);
  257. $TableMLine=$res->fetch_row();
  258. $content .= "nn".$TableMLine[1].";nn";
  259. for ($i = 0, $st_counter = 0; $i < $fields_amount; $i++, $st_counter=0) {
  260. while($row = $result->fetch_row()) { //when started (and every after 100 command cycle):
  261. if ($st_counter%100 == 0 || $st_counter == 0 ) {$content .= "nINSERT INTO ".$table." VALUES";}
  262. $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 .=")";
  263. //every after 100 command cycle [or at last line] ....p.s. but should be inserted 1 cycle eariler
  264. if ( (($st_counter+1)%100==0 && $st_counter!=0) || $st_counter+1==$rows_num) {$content .= ";";} else {$content .= ",";} $st_counter=$st_counter+1;
  265. }
  266. } $content .="nnn";
  267. }
  268. $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 */;";
  269. $backup_name = $backup_name ? $backup_name : $name."___(".date('H-i-s')."_".date('d-m-Y').")__rand".rand(1,11111111).".sql";
  270.  
  271. $fileLocation = $path .'\'. $backup_name;
  272. $file = fopen($fileLocation,"w");
  273. fwrite($file,$content);
  274. fclose($file);
  275.  
  276.  
  277. if($download_allow){
  278. ob_get_clean(); header('Content-Type: application/octet-stream'); header("Content-Transfer-Encoding: Binary"); header("Content-disposition: attachment; filename="".$backup_name.""");
  279. }
  280.  
  281. echo $content; exit;
  282. }
Add Comment
Please, Sign In to add comment