Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: PHP
Posted by: Amit Sarker
Added: Jan 2, 2019 1:57 PM
Modified: Jan 2, 2019 1:58 PM
Views: 18
Tags: no tags
  1.  public function database_backup() {
  2.         $fileName = 'backup-on-' . date("Y-m-d-H-i-s") . '.sql';
  3.         $backupDbName = 'backup-on-' . date("Y-m-d-H-i-s") . '.zip';
  4.         $savePath = $this->get_db_backup_directory() . '/' . $backupDbName;
  5.         $tableArray = $this->db->list_tables();
  6.         $prefs = array(
  7.             'tables' => $tableArray, // Array of tables to backup.
  8.             'ignore' => array(), // List of tables to omit from the backup
  9.             'format' => 'zip', // gzip, zip, txt
  10.             'filename' => $fileName, // File name - NEEDED ONLY WITH ZIP FILES
  11.             'add_drop' => TRUE, // Whether to add DROP TABLE statements to backup file
  12.             'add_insert' => TRUE, // Whether to add INSERT data to backup file
  13.             'newline' => "\n"                         // Newline character used in backup file
  14.         );
  15.         // Load the DB utility class
  16.         $this->load->dbutil();
  17. // Backup your entire database and assign it to a variable
  18.         $backup = $this->dbutil->backup($prefs);
  19. // Load the file helper and write the file to your server
  20.         $this->load->helper('file');
  21.         write_file($savePath, $backup);
  22.  
  23. // Load the download helper and send the file to your desktop
  24. //        $this->load->helper('download');
  25. //        force_download('mybackup.gz', $backup);
  26.     }
  27.  
  28.     public function get_db_backup_directory() {
  29.         $directoryName = 'db_backup';
  30.         if (!is_dir('./' . $directoryName)) {
  31.             mkdir('./' . $directoryName, 0755, TRUE);
  32.         }
  33.         return $directoryName;
  34.     }