ACC SHELL

Path : /srv/www/vhosts/centrumlb/cron/
File Upload :
Current File : /srv/www/vhosts/centrumlb/cron/backup_database.php

<?php

header("Content-Type: text/html; charset=utf8");

include_once('../config/connect.php');

$datum = date("d_m_y_H_i_s");

/* backup the db OR just a table */
$nazev_filu = 'archive/db-backup-' . $datum . '.sql';

//get all of the tables
$tables = array();
$return = 'DROP DATABASE IF EXISTS ' . DB_NAME . ';';

$result = mysql_query('SHOW TABLES');
while ($row = mysql_fetch_row($result)) {
    $test = (integer) ereg("piwik", $row[0]);
    if ($test == 0) {
        $tables[] = $row[0];
    }
}


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

    $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";
}

//save file
$handle = fopen("$nazev_filu", 'w+');
fwrite($handle, $return);
fclose($handle);


$zip = new ZipArchive;
$res = $zip->open($nazev_filu . '.zip', ZipArchive::CREATE);
if ($res === TRUE) {
    $zip->addFile($nazev_filu);
    $zip->close();
    unlink($nazev_filu);
} else {
    echo 'failed';
}


$expiretime = 60 * 60 * 24 * 7; //expire time DAYS (60s * 60min * 24hrs * 7 days)

$tmp_older = "archive/";
$fileTypes = ".zip";

//glob($pattern, $flags)
//$files = glob($tmp_older . $fileTypes);
$files = glob("archive/*.zip");

if ($files) {
    foreach ($files as $Filename) {

        // Read file creation time
        $FileCreationTime = filectime($Filename);

        // Calculate file age in seconds
        $FileAge = time() - $FileCreationTime;

        // Is the file older than the given time span?
        if ($FileAge > ($expiretime)) {

            // Now do something with the olders files...
            //print "The file $Filename is older than $expire_time minutes\n";
            //deleting files:
            unlink($Filename);
        }
    }
}

ACC SHELL 2018