-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup-db.php
More file actions
executable file
·164 lines (144 loc) · 4.63 KB
/
backup-db.php
File metadata and controls
executable file
·164 lines (144 loc) · 4.63 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/php -n -d error_reporting=-1 -d display_errors=1 -d memory_limit=1G
<?php
ini_set('error_reporting', -1);
ini_set('display_errors', 1);
/**
* Backup Tool for CouchDB
*
* This tool uses the CouchDB changes API to identify changes to a
* database and replicate it locally to the filesystem. This is
* intended to be used with a secondary backup system that will
* enable a primitive revisioning to occur (e.g. by using Time
* Machine or by the provided GIT scripts). This can then be used
* to restore previous versions or recover accidentally deleted
* entries.
*/
// Get all of the configs and process updates for them.
foreach(glob(realpath(dirname(__FILE__)) . '/configs/*.ini') as $configFile)
{
processUpdate(parse_ini_file($configFile));
}
printf("%s\n", date('c'));
die("All done\n");
/**
* Delete an entire tree recursively.
*
* @param string $dir The directory path to delete.
*
* @return bool The result of the final rmdir operation on this directory.
*/
function delTree($dir) {
$files = array_diff(scandir($dir), array('.','..'));
foreach ($files as $file) {
(is_dir("$dir/$file")) ? delTree("$dir/$file") : unlink("$dir/$file");
}
return rmdir($dir);
}
/**
* Returns a hashed destination path for a record ID.
* This is to have hashed directories to avoid megadirectories.
*
* @param string $backupPath Destination path for backup.
* @param string $id Unique Identifier of the record.
* @param int $depth The depth of the hashing.
*
* @return string The desintation path to use for this record.
*/
function getPathForRecord($backupPath, $id, $depth = 2)
{
$hash = md5($id);
if ($depth > 15)
{
$depth = 15;
}
$path = $backupPath;
for ($i = 0; $i < $depth; $i++)
{
$path .= '/' . substr($hash, $i * 2, 2);
}
return $path;
}
/**
* Process updates for a given configuration.
*
* This script will work through changes from a given CouchDB instance
* and apply them to a local directory extracting out the documents to
* JSON files and storing exporting attachments.
*
* @param array $config Soure configs.
*/
function processUpdate($config)
{
extract($config);
if (!file_exists($backupPath))
{
mkdir($backupPath, 0777, true);
}
$sequenceFile = sprintf('%s/%s_to_backup_%s', $sequencePath, $dbName, md5($dbHost.$backupPath));
$since = "0";
$pending = 1;
if (file_exists($sequenceFile))
{
printf("Using sequence file: %s\n", $sequenceFile);
$lastSince = file_get_contents($sequenceFile);
$since = !empty($lastSince) ? $lastSince : $since;
}
while ($pending > 0)
{
$url = sprintf('%s/%s/_changes?include_docs=true&limit=10&since=%s&attachments=true', $dbHost, $dbName, $since);
printf("Using URL %s\n\n", $url);
$data = file_get_contents($url) or die("Unable to connect to CouchDB\n");
$response = json_decode($data);
$pending = $response->pending;
$changes = $response->results;
$lastSeq = $response->last_seq;
$since = $lastSeq;
foreach ($changes as $change)
{
$documentPath = getPathForRecord($backupPath, $change->id);
if (!file_exists($documentPath))
{
mkdir($documentPath, 0777, true);
}
if (isset($change->deleted))
{
printf("Change %s is deleted, deleting doc.\n", $change->id);
$recPath = sprintf('%s/%s.json', $documentPath, $change->id);
$recPathCompressed = sprintf('%s/%s.json.gz', $documentPath, $change->id);
$attachments = sprintf('%s/%s', $documentPath, $change->id);
file_exists($recPath) ? var_dump(unlink($recPath)) : null;
file_exists($recPathCompressed) ? var_dump(unlink($recPathCompressed)) : null;
file_exists($attachments) ? var_dump(delTree($attachments)) : null;
continue;
}
printf("Found Record %s\n", $change->id);
// Drop attachments.
if (isset($change->doc->_attachments))
{
$attachmentRoot = sprintf("%s/%s", $documentPath, $change->id);
mkdir ($attachmentRoot);
foreach ($change->doc->_attachments as $filename=>$attachment)
{
file_put_contents($attachmentRoot . '/' . $filename, base64_decode($attachment->data));
unset($change->doc->_attachments->$filename->data);
}
}
$data_json = json_encode($change->doc);
$url = sprintf('%s/%s.json', $documentPath, $change->id);
if (isset($compressBackup) && $compressBackup)
{
$url .= '.gz';
$data_json = gzencode($data_json);
}
// If the directory doesn't exist, create it!
if (!file_exists(dirname($url)))
{
mkdir(dirname($url), 0777, true);
}
$result = file_put_contents($url, $data_json);
var_dump($result);
}
printf("Pending: %d; Last Seq: %s\n", $pending, $lastSeq);
file_put_contents($sequenceFile, $lastSeq);
}
}