Cara Backup File & Database di Codeigniter

Cara Backup File & Database di Codeigniter

Halo sobat kali ini saya akan share tutorial cara membackup data file & database di website yang menggunakan framework codeigniter.

Caranya sangat mudah berikut adalah contoh daftar file dan folder yang telah saya praktekan di website saya ini:

  • www
    • application
      • controllers
        • Backup.php
      • libraries
        • recurseZip_lib.php
    • backup
      • db
        • backup-on-2016-01-04.zip
      • files
        • www.zip

1. Buat folder dengan nama backup lalu buat lagi folder db & files di dalam folder backup. www/backup/db tempat untuk menyimpan hasil backup database, sedangkan folder www/backup/files untuk menyimpan hasil backup file dalam bentuk zip.

2. Buat file Backup.php didalam folder controllers www/application/controllers/Backup.php. Berikut kodenya:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Backup extends CI_Controller
{
   
  // backup files in directory
  function files()
  {
     $opt = array(
       'src' => '../www', // dir name to backup
       'dst' => 'backup/files' // dir name backup output destination
     );
     
     // Codeigniter v3x
     $this->load->library('recurseZip_lib', $opt);
     $download = $this->recursezip_lib->compress();
    
     /* Codeigniter v2x
     $zip    = $this->load->library('recurseZip_lib', $opt);     
     $download = $zip->compress();
     */
     
     redirect(base_url($download));
  }
   
  // backup database.sql
  public function db()
  {
     $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'; // file name
     $save  = 'backup/db/' . $db_name; // dir name backup output destination
   
     $this->load->helper('file');
     write_file($save, $backup);
   
     $this->load->helper('download');
     force_download($db_name, $backup);
  }
   
}

3. Buat sebuah library dengan nama recurseZip_lib.php didalam folder libraries www/application/libraries/recurseZip_lib.php untuk mengkonversi file menjadi zip. Berikut kodenya:

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/*
Copyright (c) 2011 http://ramui.com. All right reserved.
I have modified it for use in CodeIgniter.
*/

class recurseZip_lib
{
   
  function __construct($opt)
  {
     $this->src = $opt['src'];
     $this->dst = $opt['dst'];
  }
   
  private function recurse_zip($src, &$zip, $path)
  {
     $dir = opendir($src);
     while (false !== ($file = readdir($dir))) {
        if (($file != '.') && ($file != '..')) {
           if (is_dir($src . '/' . $file)) {
              $this->recurse_zip($src . '/' . $file, $zip, $path);
           } else {
              $zip->addFile($src . '/' . $file, substr($src . '/' . $file, $path));
           }
        }
     }
     closedir($dir);
  }
   
  private function run($src, $dst = '')
  {
     if (substr($src, -1) === '/') {
        $src = substr($src, 0, -1);
     }
     if (substr($dst, -1) === '/') {
        $dst = substr($dst, 0, -1);
     }
     $path  = strlen(dirname($src) . '/');
     $filename = substr($src, strrpos($src, '/') + 1) . '.zip';
     $dst  = empty($dst) ? $filename : $dst . '/' . $filename;
     @unlink($dst);
     $zip = new ZipArchive;
     $res = $zip->open($dst, ZipArchive::CREATE);
     if ($res !== TRUE) {
        echo 'Error: Unable to create zip file';
        exit;
     }
     if (is_file($src)) {
        $zip->addFile($src, substr($src, $path));
     } else {
        if (!is_dir($src)) {
           $zip->close();
           @unlink($dst);
           echo 'Error: File not found';
           exit;
        }
     $this->recurse_zip($src, $zip, $path);
     }
     $zip->close();
     return $dst;
  }
   
  public function compress()
  {
     return $this->run($this->src, $this->dst);
  }
   
}

Nah sekarang sudah selesai, untuk membackup database tinggal buka url http://localhost/www/index.php/backup/db

dan untuk membackup file buka url http://localhost/www/index.php/backup/files

Sekian semoga bermanfaat.

keyboard_arrow_up