forked from WWBN/AVideo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysqlDump.php
56 lines (43 loc) · 1.47 KB
/
mysqlDump.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
//streamer config
require_once '../videos/configuration.php';
if (php_sapi_name() !== 'cli') {
return die('Command Line only');
}
ob_end_flush();
$prefix = 'mysqldump';
if(!empty($restore)){
$prefix = 'mysqldumpBackup';
}
$file = Video::getStoragePath().$prefix.'-'.date('YmdHis').'.sql';
$excludeTables = ['CachesInDB', 'audit']; // tables to exclude from the dump
// Create a connection to the database to retrieve all table names
$connection = new mysqli($mysqlHost, $mysqlUser, $mysqlPass, $mysqlDatabase, $mysqlPort);
if ($connection->connect_error) {
die("Connection failed: " . $connection->connect_error);
}
$res = sqlDAL::readSql("SHOW TABLES");
$row = sqlDAL::fetchAllAssoc($res);
foreach ($row as $value) {
$firstElement = reset($value);
if (!in_array($firstElement, $excludeTables)) {
$tables[] = $firstElement;
}else{
echo "Exclude from dump $firstElement".PHP_EOL;
}
}
if(empty($mysqlPort)){
$mysqlPort = 3306;
}
// Use the mysqldump command to get the database dump
$dumpCommand = "mysqldump --host=$mysqlHost --port=$mysqlPort --user='$mysqlUser' --password='$mysqlPass' "
. "--default-character-set=utf8mb4 $mysqlDatabase $tableList > {$file}";
// Execute the command
system($dumpCommand, $output);
// Check the result
if ($output !== 0) {
echo $dumpCommand.PHP_EOL;
die("Error occurred while taking the database dump.");
}
echo "Database dumped successfully to {$file}".PHP_EOL;
?>