Jumat, 30 Oktober 2015

Download Backup Database Mysql Menggunakan PHP

Langsung aja gak pake basa basi, script di bawah ini di gunakan untuk backup database menggunakan PHP, filanya bisa langsung di download atau di save di hosting juga bisa.
<?php
error_reporting(E_ALL);

/* Define database parameters here */
define("DB_USER", 'root');
define("DB_PASSWORD", '');
define("DB_NAME", 'kwitansi');
define("DB_HOST", 'localhost');
define("OUTPUT_DIR", 'dbBackup'); // Folder / Directory Name
define("TABLES", '*');

/* Instantiate Backup_Database and perform backup */
$backupDatabase = new Backup_Database(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$status = $backupDatabase->backupTables(TABLES, OUTPUT_DIR) ? 'OK' : 'KO';
//echo "Backup result: " . $status . " - By Asefur Mukti";
//echo '<a href="'.$backupDatabase->filename.'">Download</a>';
/* The Backup_Database class */

class Backup_Database {
/* Host where database is located  */

  var $host = '';
  var $username = '';
  var $passwd = '';
  var $dbName = '';
  var $charset = '';

  /* Constructor initializes database */

  function Backup_Database($host, $username, $passwd, $dbName, $charset = 'utf8') {
    $this->host = $host;
    $this->username = $username;
    $this->passwd = $passwd;
    $this->dbName = $dbName;
    $this->charset = $charset;
    $this->initializeDatabase();
  }

  protected function initializeDatabase() {
    $conn = @mysql_connect($this->host, $this->username, $this->passwd); // Ik Added @ to Hide PDO Error Message
    mysql_select_db($this->dbName, $conn);
    if (!mysql_set_charset($this->charset, $conn)) {
      mysql_query('SET NAMES ' . $this->charset);
    }
  }

  /* Backup the whole database or just some tables Use '*' for whole database or 'table1 table2 table3...' @param string $tables  */

  public function backupTables($tables = '*', $outputDir = '.') {
    try {
      /* Tables to export  */
      if ($tables == '*') {
        $tables = array();
        $result = mysql_query('SHOW TABLES');
        while ($row = mysql_fetch_row($result)) {
          $tables[] = $row[0];
        }
      } else {
        $tables = is_array($tables) ? $tables : explode(',', $tables);
      }

      $sql = 'CREATE DATABASE IF NOT EXISTS ' . $this->dbName . ";\n\n";
      $sql .= 'USE ' . $this->dbName . ";\n\n";

  /* Iterate tables */
  foreach ($tables as $table) {
    //echo "Backing up " . $table . " table...";

    $result = mysql_query('SELECT * FROM ' . $table);
 
    $numFields = mysql_num_fields($result);

    $sql .= 'DROP TABLE IF EXISTS ' . $table . ';';
    $row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE ' . $table));
    $sql.= "\n\n" . $row2[1] . ";\n\n";

    for ($i = 0; $i < $numFields; $i++) {
      while ($row = mysql_fetch_row($result)) {
        $sql .= 'INSERT INTO ' . $table . ' VALUES(';
        for ($j = 0; $j < $numFields; $j++) {
          $row[$j] = addslashes($row[$j]);
          // $row[$j] = ereg_replace("\n", "\\n", $row[$j]);
          if (isset($row[$j])) {
            $sql .= '"' . $row[$j] . '"';
          } else {
            $sql.= '""';
          }
          if ($j < ($numFields - 1)) {
            $sql .= ',';
          }
        }
        $sql.= ");\n";
      }
    }
    $sql.="\n\n\n";
    //echo " OK <br/><br/>" . "";
  }
} catch (Exception $e) {
  //var_dump($e->getMessage());
  return false;
 }

    return $this->saveFile($sql, $outputDir);
  }

  /* Save SQL to file @param string $sql */

  protected function saveFile(&$sql, $outputDir = '.') {
    if (!$sql)
      return false;

    try {
   $this->filename = 'db-backup-' . $this->dbName . '-' . date("Ymd-His", time()) . '.sql'; 
      //$handle  = fopen($this->filename, 'w+'); //UNTUK MEMBUAT FILE
      //fwrite($handle, $sql);     //SETELAH DI BUAT LALU DI TULIS KE DALAM FILE
   header("Content-type: application/octet-stream");
   header('Content-Disposition: attachment; filename='.$this->filename);
   echo $sql;
      //fclose($handle);       //LALU FILE DI TUTUP
    } catch (Exception $e) {
      //var_dump($e->getMessage());    //TAMPILKAN PESAN
      return false;
    }
    return true;
  }

}
?>

Kamis, 22 Oktober 2015

Cara zip folder di linux dengan command

Cek apakah zip sudah terinstal di linux menggunakan perintah di bawah ini:

# which zip   (It will show the absolute path of zip)

In CentOS

# rpm -q zip

In Debian or Ubuntu

$ sudo dpkg -l zip

Jika zip belum terinstall di system gunakan perintah di bawah ini:
In CentOS or Red hat
# yum install zip

In Debian or Ubuntu
$ sudo apt-get install zip
Setelah instalasi selesai gunakan command di bawah ini untuk mengkompresi folder :
zip -r GiveAnyName.zip  /path/of/Directory

for eg.
 We have a backup directory in /root/ (i.e /root/backup)

zip -r backup.zip /root/backup

Gunakan perintah ini untuk unzip file zip:
unzip backup.zip

Senin, 19 Oktober 2015

Perintah Dasar Linux

Melihat Space Hardisk
df -h

Copy Folder
cp -avr /var/www/html /var/www/html2
/var/www/html => ini adalah folder yg akan di copy
/var/www/html2 => ini adalah tujuan paste

Menghapus Folder yang ada isi nya
rm -rf namafolder

Caranya masuk ke folder sebelum folder yg akan di hapus misalkan kita punya folder /var/www/html/manual dan folder yg akan di hapus adalah folder manual maka masuk ke folder dengan perintah cd /var/www/html lalu hapus dengan perintah rm -rf manual

Menghapus History di Command
history -cw 
Mengetahui Unkuran Besar kecilnya Folder
du -sh *

membuka koneksi database ke server lain
Membuka koneksi mysq dari cenots ke server lain (EC2)
setsebool httpd_can_network_connect_db on
Cara mencari text di dalam file (Centos)
grep -rwl "cari-text-disini" /path/folder
Agar file web bisa di akses
chown www-data:www-data -R /var/www/dev.ivendor.co.id/public_html/*

Senin, 05 Oktober 2015

Disable Pembayaran Saat Check Out Woocommerce Berdasarkan Produk Tertentu

Sometimes, you don't want to offer a particular payment gateway in your WooCommerce checkout if a customer has a certain product in their cart.
Here's a code snippet that you can add to your theme's functions.php file to help!

/*
* Disable PayPal payment method in the checkout if certain
* products are present in the cart.
* Add this to your theme's functions.php file
*/
add_filter( 'woocommerce_available_payment_gateways', 'filter_gateways', 1);
function filter_gateways( $gateways ){
global $woocommerce;
foreach ($woocommerce->cart->cart_contents as $key => $values ) {
// store product IDs in array PayPal method is disabled at checkout
$nonPPproducts = array(1111,2222,3333); // LIST YOUR PRODUCTS HERE
if ( in_array( $values['product_id'], $nonPPproducts ) ) {
unset($gateways['paypal']);
// You can unset other gateways using the gateway ID e.g. "cod", "bacs", "stripe"
break;
}
}
return $gateways;
}

You simply have to add the product(s) by their ID to the array list. If you want to disable other payment methods, you can find their Gateway ID under WooCommerce > Settings > Checkout: