<?php
// definisikan koneksi ke database
$server = "localhost";
$username = "root";
$password = "12345";
$database = "tutorial";

// Koneksi dan memilih database di server
mysql_connect($server,$username,$password) or die("Koneksi gagal");
mysql_select_db($database) or die("Database tidak bisa dibuka");
?>

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Kumpulan Contoh Aplikasi Andeznet</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="">

    <!-- Le styles -->
	    <link href="assets/css/bootstrap.css" rel="stylesheet" media="screen">
        <link href="assets/css/bootstrap-responsive.min.css" rel="stylesheet" media="screen">
		 <link href="assets/css/bootstrap.min.css" rel="stylesheet" media="screen">
		<link href="assets/css/easypiechart/jquery.easy-pie-chart.css" rel="stylesheet" media="screen">
		
    <style>
      body {
        padding-top: 60px; /* 60px to make the container go all the way to the bottom of the topbar */
      }
    </style>
    <link href="assets/css/bootstrap-responsive.css" rel="stylesheet">

    <!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
    <!--[if lt IE 9]>
      <script src="../assets/js/html5shiv.js"></script>
    <![endif]-->

    <!-- Fav and touch icons -->
    <link rel="apple-touch-icon-precomposed" sizes="144x144" href="assets/ico/apple-touch-icon-144-precomposed.png">
    <link rel="apple-touch-icon-precomposed" sizes="114x114" href="assets/ico/apple-touch-icon-114-precomposed.png">
      <link rel="apple-touch-icon-precomposed" sizes="72x72" href="assets/ico/apple-touch-icon-72-precomposed.png">
                    <link rel="apple-touch-icon-precomposed" href="assets/ico/apple-touch-icon-57-precomposed.png">
                                   <link rel="shortcut icon" href="assets/ico/logoandeznet.png">
  </head>

  <body>
  
   <div class="navbar navbar-inverse navbar-fixed-top">
      <div class="navbar-inner">
        <div class="container">
          <button type="button" class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="brand" href="http://andeznet.com">AndezNet</a>
          <div class="nav-collapse collapse">
            <ul class="nav">
              <li class="active"><a href="media.php">Home</a></li>
			    
				<li class="dropdown">
					<a class="dropdown-toggle" data-toggle="dropdown" href="#">Contoh Aplikasi<b class="caret"></b></a>
				<ul class="dropdown-menu">
				 <li>
                 <a tabindex="-1" href="statistik.php">Statistik Pendaftar</a>
                </li>
				<li>
                 <a tabindex="-1" href="backuprestore.php">Backup Restore</a>
                </li>
				<li>
				<a tabindex="-1" href="carialamat.php">Pencarian Alamat</a>
				</li>
				<li>
				<a tabindex="-1" href="playerbootsrap/index.html">MP3 Player</a>
				</li>
				</ul>
				</li>
				
			  <li><a href="#">About</a></li>
			  
              <li>
				<a href="logout.php" id="logout" onclick="return confirm('Apakah Anda yakin?')">
				<i class="ace-icon fa fa-power-off"></i>
				Logout
			</a>
			</li>
            </ul>
          </div><!--/.nav-collapse -->
        </div>
      </div>
    </div>
<div class="container">
            <?php
			
			
			
            $file	  =	$database.'_'.date("DdMY").'_'.time().'.sql';
            //membuat nama file

            ?>


            <script>
                function pastikan(text){
                    confirm('Apakah Anda yakin akan '+text+'?')
                }
            </script>



            <div>
                <div class="tabbable">
                    <ul class="nav nav-tabs" id="myTab">
                        <li class="active">
                            <a data-toggle="tab" href="#backup">
                                <i class="green ace-icon fa  fa-cloud-download bigger-120"></i>
                                Backup
                            </a>
                        </li>

                        <li>
                            <a data-toggle="tab" href="#restore">
                                <i class="blue ace-icon fa  fa-cloud-upload bigger-120"></i>
                                Restore
                            </a>
                        </li>

                    </ul>

                    <div class="tab-content">
                        <div id="backup" class="tab-pane fade in active">
                            <form action="" method="post" name="postform" enctype="multipart/form-data" >
                                <p>
                                    <strong>Backup</strong> semua data yang ada didalam database &quot;<strong><?= $database ?></strong>&quot;.</em>
                                </p>
                                <div class="asd">
                                    <label for="backup">Backup database</label>
                                    <button id="loading-btn" type="submit" class="btn btn-success" name="backup" onClick="return pastikan('Backup database')">Proses Backup</button>
                                </div>

                            </form>
                        </div>

                        <div id="restore" class="tab-pane fade">
                            <form action="" method="post" name="postform" enctype="multipart/form-data" >
                                <p>
                                    <strong>Restore</strong> semua data yang ada didalam database &quot;<strong><?= $database ?></strong>&quot;.</em>
                                </p>
                                <div class="asd">
                                    <label for="backup">File Backup Database (*.sql)</label>
                                    <input type="file" name="datafile" size="20" class="filestyle" data-buttonName="btn-primary"/>
                                    <button type="submit" class="btn btn-primary" name="restore" onClick="return pastikan('Backup database')" data-loading-text="Loading...">Proses Restore</button>
                                </div>
                            </form>
                        </div>

                    </div>
                </div>
            </div><!-- /.col -->




                <?php

                //Download file backup ============================================
                if(isset($_GET['nama_file']))
                {
                    $file = $back_dir.$_GET['nama_file'];

                    if (file_exists($file))
                    {
                        header('Content-Description: File Transfer');
                        header('Content-Type: application/octet-stream');
                        header('Content-Disposition: attachment; filename='.basename($file));
                        header('Content-Transfer-Encoding: binary');
                        header('Expires: 0');
                        header('Cache-Control: private');
                        header('Pragma: private');
                        header('Content-Length: ' . filesize($file));
                        ob_clean();
                        flush();
                        readfile($file);
                        exit;

                    }
                    else
                    {
                        echo "file {$_GET['nama_file']} sudah tidak ada.";
                    }

                }

                //Backup database =================================================
                if(isset($_POST['backup']))
                {
                    backup($file);
                   
					echo 'Backup database telah selesai <a style="cursor:pointer" href="'.$file.'" title="Download">Download file database</a>';

                    echo "<pre>";
                    print_r($return);
                    echo "</pre>";
                }
                else
                {
                    unset($_POST['backup']);
                }

                //Restore database ================================================
                if(isset($_POST['restore']))
                {
                    restore($_FILES['datafile']);

                    echo "<pre>";
                    print_r($lines);
                    echo "</pre>";
                }
                else
                {
                    unset($_POST['restore']);
                }

                ?>


            <?php

            function restore($file) {
                global $rest_dir;

                $nama_file	= $file['name'];
                $ukrn_file	= $file['size'];
                $tmp_file	= $file['tmp_name'];

                if ($nama_file == "")
                {
                    echo "Fatal Error";
                }
                else
                {
                    $alamatfile	= $rest_dir.$nama_file;
                    $templine	= array();

                    if (move_uploaded_file($tmp_file , $alamatfile))
                    {

                        $templine	= '';
                        $lines		= file($alamatfile);

                        foreach ($lines as $line)
                        {
                            if (substr($line, 0, 2) == '--' || $line == '')
                                continue;

                            $templine .= $line;

                            if (substr(trim($line), -1, 1) == ';')
                            {
                                mysql_query($templine) or print('Query gagal \'<strong>' . $templine . '\': ' . mysql_error() . '<br /><br />');

                                $templine = '';
                            }
                        }
                        echo "<center>Berhasil Restore Database, silahkan di cek.</center>";

                    }else{
                        echo "Proses upload gagal, kode error = " . $file['error'];
                    }
                }

            }

            function backup($nama_file,$tables = '')
            {
                global $return, $tables, $back_dir, $database ;

                if($tables == '')
                {
                    $tables = array();
                    $result = @mysql_list_tables($database);
                    while($row = @mysql_fetch_row($result))
                    {
                        $tables[] = $row[0];
                    }
                }else{
                    $tables = is_array($tables) ? $tables : explode(',',$tables);
                }

                $return	= '';

                foreach($tables as $table)
                {
                    $result	 = @mysql_query('SELECT * FROM '.$table);
                    $num_fields = @mysql_num_fields($result);

                    //menyisipkan query drop table untuk nanti hapus table yang lama
                    $return	.= "DROP TABLE IF EXISTS ".$table.";";
                    $row2	 = @mysql_fetch_row(mysql_query('SHOW CREATE TABLE  '.$table));
                    $return	.= "\n\n".$row2[1].";\n\n";

                    for ($i = 0; $i < $num_fields; $i++)
                    {
                        while($row = @mysql_fetch_row($result))
                        {
                            $return.= 'INSERT INTO '.$table.' VALUES(';

                            for($j=0; $j<$num_fields; $j++)
                            {
                                $row[$j] = @addslashes($row[$j]);
                                $row[$j] = @ereg_replace("\n","\\n",$row[$j]);
                                if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
                                if ($j<($num_fields-1)) { $return.= ','; }
                            }
                            $return.= ");\n";
                        }
                    }
                    $return.="\n\n\n";
                }

                $nama_file;

                $handle = fopen($back_dir.$nama_file,'w+');
                fwrite($handle, $return);
                fclose($handle);
            }
            ?>
</div>

	<div class="row-fluid">
			<div class="span12">
			  <div class="row-fluid">
				<div class="alert alert-info">
					<a name="contact"></a>
				  <h2>www.andeznet.com</h2>
				  <p class="text-info">Gudang Teknologi & Informasi</p>
				  <p>&copy; <a href="http://andeznet.com">www.andeznet.com</a>&nbsp<?php echo date("Y");?></p>
				</div><!--/span-->
			  </div><!--/row-->
			</div><!--/span-->
	</div><!--/row-->	

<!-- Le javascript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
    <script src="assets/js/jquery.js"></script>
    <script src="assets/js/bootstrap-transition.js"></script>
    <script src="assets/js/bootstrap-alert.js"></script>
    <script src="assets/js/bootstrap-modal.js"></script>
    <script src="assets/js/bootstrap-dropdown.js"></script>
    <script src="assets/js/bootstrap-scrollspy.js"></script>
    <script src="assets/js/bootstrap-tab.js"></script>
    <script src="assets/js/bootstrap-tooltip.js"></script>
    <script src="assets/js/bootstrap-popover.js"></script>
    <script src="assets/js/bootstrap-button.js"></script>
    <script src="assets/js/bootstrap-collapse.js"></script>
    <script src="assets/js/bootstrap-carousel.js"></script>
    <script src="assets/js/bootstrap-typeahead.js"></script>
	<script src="assets/js/bootstrap-filestyle.js"></script>
	
	
	<script src="assets/js/bootstrap.min.js"></script>
    <script src="assets/css/easypiechart/jquery.easy-pie-chart.js"></script>
	
	<script>
        $(function() {
            // Easy pie charts
            $('.chart').easyPieChart({animate: 1000});
			$('.dropdown-toggle').dropdown();
			$(":file").filestyle({buttonName: "btn-primary"});
        });
     </script>

  </body>
</html>

    