1
0
Fork 0
mirror of https://github.com/DanielnetoDotCom/YouPHPTube synced 2025-10-03 09:49:28 +02:00
Oinktube/install/mysqlRestore.php

67 lines
2.1 KiB
PHP

<?php
//streamer config
$global['createDatabase'] = 1;
$doNotIncludeConfig = 1;
require_once '../videos/configuration.php';
$global['mysqli'] = new mysqli($mysqlHost, $mysqlUser, $mysqlPass, '', @$mysqlPort);
$createSQL = "CREATE DATABASE IF NOT EXISTS {$mysqlDatabase};";
$global['mysqli']->query($createSQL);
$global['mysqli']->select_db($mysqlDatabase);
if (php_sapi_name() !== 'cli') {
return die('Command Line only');
}
ob_end_flush();
$globPattern = "{$global['systemRootPath']}videos/mysqldump-*.sql";
echo "Searching [{$globPattern}]" . PHP_EOL;
$glob = glob($globPattern);
foreach ($glob as $key => $file) {
echo "($key) {$file}" . PHP_EOL;
}
echo "Type the number of what file you want to restore or just press enter to get the latest" . PHP_EOL;
$option = trim(readline(""));
if ($option === '') {
$filename = end($glob);
} else {
$option = intval($option);
$filename = $glob[$option];
}
echo 'We will make a backup first ...' . PHP_EOL;
$file = "{$global['systemRootPath']}videos/" . 'mysqlBackupBeforeRestore-' . date('YmdHis') . '.sql';
passthru("mysqldump --opt -u '{$mysqlUser}' -p'{$mysqlPass}' -h {$mysqlHost} {$mysqlDatabase} > {$file}");
echo PHP_EOL . "Backup file created at {$file}" . PHP_EOL;
executeFile($filename);
function executeFile($filename) {
global $global;
$templine = '';
// Read in entire file
$lines = file($filename);
// Loop through each line
foreach ($lines as $line) {
// Skip it if it's a comment
if (substr($line, 0, 2) == '--' || $line == '')
continue;
// Add this line to the current segment
$templine .= $line;
// If it has a semicolon at the end, it's the end of the query
if (substr(trim($line), -1, 1) == ';') {
// Perform the query
if (!$global['mysqli']->query($templine)) {
echo ('sqlDAL::executeFile ' . $filename . ' Error performing query \'<strong>' . $templine . '\': ' . $global['mysqli']->error . '<br /><br />');
}
// Reset temp variable to empty
$templine = '';
}
}
}