-
Notifications
You must be signed in to change notification settings - Fork 1
/
copy_maker
executable file
·89 lines (79 loc) · 2.25 KB
/
copy_maker
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
#! /usr/bin/env php
<?php
// copy_maker args
// start copy tasks for files that
// - currently have no copy task
// - satisfy the criteria given by args
//
// copy to the given remote site and store
//
// args:
// --filename_like expr
// filename matches MySQL "like" expr (% is wildcard)
// --type t
// file type is t
// --remote_site site-name
// --remote_store store-name
//
// This must be run on the Librarian host; it accesses the DB
require_once("hl_db.inc");
require_once("hl_rpc_client.php");
// enumerate files using the given query,
// then arrange to copy them to the given site/store
//
function copy_maker($query, $remote_site, $remote_store) {
$files = enum_general($query);
$n = 0;
foreach ($files as $file) {
$req = new StdClass;
$req->task_type = TASK_TYPE_PUSH;
$req->file_name = $file->name;
$req->local_store_id = $file->store_id;
$req->remote_site_name = $remote_site;
$req->remote_store_name = $remote_store;
$req->delete_when_done = false;
if (!task_insert($req)) {
echo "DB error: ".db_error()."\n";
} else {
$n++;
}
}
echo "started $n copies\n";
}
if (!init_db(get_server_config())) {
die("can't open DB\n");
}
$filename_pattern = null;
$type = null;
$remote_site = null;
$remote_store = null;
for ($i=1; $i<$argc; $i++) {
if ($argv[$i] == "--filename_like") {
$filename_pattern = $argv[++$i];
} else if ($argv[$i] == "--type") {
$type = $argv[++$i];
} else if ($argv[$i] == "--remote_site") {
$remote_site = $argv[++$i];
} else if ($argv[$i] == "--remote_store") {
$remote_store = $argv[++$i];
} else {
die ("bad arg ".$argv[$i]."\n");
}
}
if (!$remote_site || !$remote_store) {
die ("Usage\n");
}
// don't include files that already have active copy tasks
//
$query = "select * from file left join task on file.name = task.file_name where task.id is null";
if ($filename_pattern) {
$filename_pattern = $link->escape_string($filename_pattern);
$query .= " and file.name like '$filename_pattern'";
}
if ($type) {
$type = $link->escape_string($type);
$query .= " and type='$type'";
}
//echo $query;
copy_maker($query, $remote_site, $remote_store);
?>