Login
Cara Backup File & Database di Codeigniter

Cara Backup File & Database di Codeigniter

Hallo 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. kita buat folder dengan nama backup lalu buat lagi folder db & files di dalam folder backup. Folder www/backup/db tempat untuk menyimpan backupan database, sedangkan folder www/backup/files untuk menyimpal hasil backupan file dalam bentuk zip.


2. kita buat file Backup.php di dalam folder controllers www/application/controllers/Backup.php. Brikut kode.y:

<?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
     );
     
     $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. kita buat sebuah librari dengan nama recurseZip_lib.php di dalam folder libraries www/application/libraries/recurseZip_lib.php untuk mengkonversi file menjadi zip. Brikut kode.y:

<?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 selelsai, 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. ^-^