This repository has been archived by the owner on Aug 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport.php
56 lines (48 loc) · 1.46 KB
/
export.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
ini_set('memory_limit', '512M');
set_time_limit(0);
error_reporting(E_ALL);
ini_set("display_errors", 1);
chdir(__DIR__);
$baseName = "redis-dump";
$start = time();
if (file_exists("$baseName.tar.gz")) {
die("File $baseName.tar.gz already exists !\n");
}
$dir = __DIR__ . "/$baseName";
if (file_exists($dir)) {
die("Error: directory already exists: " . $dir . PHP_EOL);
}
mkdir($dir);
$redis = new Redis();
$redis->connect('172.17.0.2', 6379);
$keys = $redis->keys('PHPREDIS_SESSION:*');
$count = count($keys);
echo "$count keys\n";
$j = 0;
$fileCount = 0;
$result = [];
foreach ($keys as $i => $key) {
$result[$key] = [
'data' => $redis->get($key),
'type' => $redis->type($key),
'ttl' => $redis->ttl($key),
];
$j++;
if ($j >= 10000) {
$j = 0;
$dump = json_encode($result, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
file_put_contents($dir . '/part_' . $fileCount++ . '.json', $dump);
$result = [];
echo "Read " . ($i + 1) . " from $count\n";
}
}
if (!empty($result)) {
$dump = json_encode($result, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
file_put_contents($dir . '/part_' . $fileCount++ . '.json', $dump);
}
exec("tar -czf $baseName.tar.gz $baseName");
exec("rm -r '$dir'");
echo "Complete. Result file: " . __DIR__ ."/" . $baseName . ".tar.gz\n";
$duration = time() - $start;
echo "Elapsed " . (int)($duration / 60) . " minutes " . ($duration % 60) . " seconds\n";