1 | /* $Id: dump_db.pc 3956 2010-01-05 20:56:56Z zacheiss $ |
---|
2 | * |
---|
3 | * This program dumps the Moira database to a series of output files |
---|
4 | * which can be later read back into Moira in the event of a crash. |
---|
5 | * |
---|
6 | * Copyright (C) 1988-1998 by the Massachusetts Institute of Technology. |
---|
7 | * For copying and distribution information, please see the file |
---|
8 | * <mit-copyright.h>. |
---|
9 | */ |
---|
10 | |
---|
11 | #include <mit-copyright.h> |
---|
12 | #include <moira.h> |
---|
13 | #include "dump_db.h" |
---|
14 | |
---|
15 | #include <fcntl.h> |
---|
16 | #include <stdio.h> |
---|
17 | #include <string.h> |
---|
18 | #include <unistd.h> |
---|
19 | |
---|
20 | EXEC SQL INCLUDE sqlca; |
---|
21 | |
---|
22 | RCSID("$HeadURL: svn+ssh://svn.mit.edu/moira/trunk/moira/backup/dump_db.pc $ $Id: dump_db.pc 3956 2010-01-05 20:56:56Z zacheiss $"); |
---|
23 | |
---|
24 | EXEC SQL BEGIN DECLARE SECTION; |
---|
25 | char *db = "moira"; |
---|
26 | EXEC SQL END DECLARE SECTION; |
---|
27 | |
---|
28 | int main(int argc, char **argv) |
---|
29 | { |
---|
30 | char *prefix; |
---|
31 | |
---|
32 | if (argc != 2) |
---|
33 | { |
---|
34 | fprintf(stderr, "Usage: %s prefix\n", argv[0]); |
---|
35 | exit(1); |
---|
36 | } |
---|
37 | prefix = argv[1]; |
---|
38 | |
---|
39 | EXEC SQL CONNECT :db IDENTIFIED BY :db; |
---|
40 | |
---|
41 | do_backups(prefix); |
---|
42 | |
---|
43 | EXEC SQL COMMIT; |
---|
44 | exit(0); |
---|
45 | } |
---|
46 | |
---|
47 | void dump_int(FILE *f, int n) |
---|
48 | { |
---|
49 | char buf[1024]; |
---|
50 | sprintf(buf, "%d", n); |
---|
51 | dump_str(f, buf); |
---|
52 | } |
---|
53 | |
---|
54 | void wpunt(void) |
---|
55 | { |
---|
56 | punt("can't write backup file"); |
---|
57 | } |
---|
58 | |
---|
59 | void dump_str(FILE *f, char *str) |
---|
60 | { |
---|
61 | unsigned char *ibp, c; |
---|
62 | int t; |
---|
63 | |
---|
64 | for (ibp = str; (c = *ibp); ibp++) |
---|
65 | { |
---|
66 | if (c < 32 || c > 126 || c == SEP_CHAR || c == '\\') |
---|
67 | { |
---|
68 | if (putc('\\', f) < 0) |
---|
69 | wpunt(); |
---|
70 | t = ((c >> 6) & 7) + '0'; |
---|
71 | if (putc(t, f) < 0) |
---|
72 | wpunt(); |
---|
73 | t = ((c >> 3) & 7) + '0'; |
---|
74 | if (putc(t, f) < 0) |
---|
75 | wpunt(); |
---|
76 | t = (c & 7) + '0'; |
---|
77 | if (putc(t, f) < 0) |
---|
78 | wpunt(); |
---|
79 | } |
---|
80 | else |
---|
81 | { |
---|
82 | if (putc(c, f) < 0) |
---|
83 | wpunt(); |
---|
84 | } |
---|
85 | } |
---|
86 | } |
---|
87 | |
---|
88 | void safe_close(FILE *stream) |
---|
89 | { |
---|
90 | if (fflush(stream) == EOF) |
---|
91 | punt("Unable to fflush"); |
---|
92 | if (fsync(fileno(stream)) != 0) |
---|
93 | punt("Unable to fsync"); |
---|
94 | fclose(stream); |
---|
95 | } |
---|
96 | |
---|
97 | FILE *open_file(char *prefix, char *suffix) |
---|
98 | { |
---|
99 | char name[BUFSIZ]; |
---|
100 | int fd; |
---|
101 | FILE *f; |
---|
102 | |
---|
103 | strcpy(name, prefix); |
---|
104 | strcat(name, suffix); |
---|
105 | |
---|
106 | fd = open(name, O_CREAT|O_WRONLY|O_EXCL, 0644); |
---|
107 | if (fd < 0) |
---|
108 | punt(name); |
---|
109 | f = fdopen(fd, "w"); |
---|
110 | if (!f) |
---|
111 | { |
---|
112 | fprintf(stderr, "fdopen of "); |
---|
113 | punt(name); |
---|
114 | } |
---|
115 | fprintf(stderr, "Working on %s\n", name); |
---|
116 | return f; |
---|
117 | } |
---|
118 | |
---|
119 | /* |
---|
120 | * Trim whitespace off the tail end of a string |
---|
121 | */ |
---|
122 | char *endtrim(char *save) |
---|
123 | { |
---|
124 | char *t, *s; |
---|
125 | |
---|
126 | s = save; |
---|
127 | for (t = s; *t; t++) |
---|
128 | continue; |
---|
129 | while (t > s) |
---|
130 | { |
---|
131 | --t; |
---|
132 | if (!isspace(*t)) |
---|
133 | { |
---|
134 | t++; |
---|
135 | break; |
---|
136 | } |
---|
137 | } |
---|
138 | if (*t) |
---|
139 | *t = '\0'; |
---|
140 | return s; |
---|
141 | } |
---|