-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbbackup.php
More file actions
executable file
·116 lines (94 loc) · 3.55 KB
/
dbbackup.php
File metadata and controls
executable file
·116 lines (94 loc) · 3.55 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/php
<?php
error_reporting(E_ERROR | E_WARNING | E_PARSE);
/* WORKING DIR constant */
define('DIR', dirname(__FILE__));
$configs = parse_ini_file(DIR .'/config.ini');
$sqlitedb = new SQLite3(DIR .'/var/hbrain.db');
$mysqlidb = new mysqli($configs["DB_REPLIC_HOST"], $configs["DB_REPLIC_USER"], $configs["DB_REPLIC_PASS"], $configs["DB_REPLIC_DBNAME"]);
$sqliteres = $sqlitedb->query('SELECT c.timestamp, c.statebefore, c.changedto, s.name state, s.auto FROM changelog c JOIN states s ON c.state = s.name;');
while ($entry = $sqliteres->fetchArray(SQLITE3_ASSOC)) {
$mysqlidb->query("REPLACE INTO changeLog (timestamp, statebefore, state, auto, changedto)
VALUES (
'".$entry['timestamp']."',
'".$entry['statebefore']."',
'".$entry['state']."',
'".$entry['auto']."',
".$entry['changedto']."
)");
}
$sqlitedb->close();
$mysqlidb->close();
// HBRAIN //////////////////////////////////////////////////////////////////////////////////
$sql = "
BEGIN TRANSACTION;
CREATE TABLE states (
name varchar(50) PRIMARY KEY,
auto int(1) NOT NULL DEFAULT 1,
active int(1) NOT NULL DEFAULT 0
);
";
$output = '';
exec('sqlite3 '. DIR .'/var/hbrain.db \'.dump "states"\' | grep \'^INSERT\'', $output);
foreach ( $output as $line )
$sql .= " ".$line . "\n";
$sql .= "
CREATE TABLE fcm (
timestamp DATETIME,
email varchar(99) NOT NULL,
approved BOOLEAN DEFAULT false,
token varchar(99) NOT NULL,
PRIMARY KEY(token)
);
";
$output = '';
exec('sqlite3 '. DIR .'/var/hbrain.db \'.dump "fcm"\' | grep \'^INSERT\'', $output);
foreach ( $output as $line )
$sql .= " ".$line . "\n";
$sql .= "
CREATE TABLE changelog (
timestamp DATETIME,
statebefore varchar(30) NOT NULL,
state varchar(50) NOT NULL,
changedto int(1) NOT NULL,
PRIMARY KEY(statebefore, state, changedto),
FOREIGN KEY (state) REFERENCES states(name)
);
";
$sql .= "
CREATE TRIGGER changelog_trigg
BEFORE UPDATE ON states
FOR EACH ROW
WHEN OLD.active <> NEW.active
BEGIN
INSERT OR REPLACE INTO changelog (timestamp, statebefore, state, changedto)
VALUES (
datetime('now','localtime'),
(SELECT group_concat(active, '') FROM states),
NEW.name,
NEW.active
);
DELETE FROM changelog WHERE timestamp <= date('now', '-60 day');
END;
";
$sql .= "
CREATE VIEW logic AS
SELECT
COUNT(*) AS weight,
STRFTIME('%H', timestamp)*1 AS hour,
STRFTIME('%w', timestamp)*1 AS dow,
c.statebefore,
c.changedto,
s.name,
s.auto
FROM changelog c join states s ON c.state=s.name
GROUP BY c.statebefore, c.state, c.changedto
ORDER BY weight desc, c.timestamp desc;
";
$sql .= "
COMMIT;
";
file_put_contents(DIR .'/hbrain.sql', $sql);
/////////////////////////////////////////////////////////////////////////////////////////////
exec ("cp ". DIR ."/var/hbrain.db ". DIR ."/saved_var/hbrain.db");
?>