File: mongoc-dump.c

package info (click to toggle)
syslog-ng 3.8.1-10
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 47,320 kB
  • ctags: 43,937
  • sloc: ansic: 159,432; yacc: 25,059; sh: 13,574; makefile: 4,669; python: 3,468; java: 3,218; xml: 2,309; perl: 318; lex: 316; awk: 184
file content (258 lines) | stat: -rw-r--r-- 6,371 bytes parent folder | download
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
 * Copyright 2014 MongoDB, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


#include <bson.h>
#include <fcntl.h>
#include <mongoc.h>


static bool
mongoc_dump_mkdir_p (const char *path,
int         mode)
{
#ifdef _WIN32
   if (0 != _access (path, 0)) {
      if (0 != _mkdir (path)) {
         return false;
      }
   }
#else
   if (0 != access (path, F_OK)) {
      if (0 != mkdir (path, mode)) {
         return false;
      }
   }
#endif

   return true;
}


static int
mongoc_dump_collection (mongoc_client_t *client,
                        const char      *database,
                        const char      *collection)
{
   mongoc_collection_t *col;
   mongoc_cursor_t *cursor;
   const bson_t *doc;
   bson_error_t error;
   bson_t query = BSON_INITIALIZER;
   FILE *stream;
   char *path;
   int ret = EXIT_SUCCESS;

   path = bson_strdup_printf ("dump/%s/%s.bson", database, collection);
#ifdef _WIN32
   if (0 == _access (path, 0)) {
      _unlink (path);
   }
#else
   if (0 == access (path, F_OK)) {
      unlink (path);
   }
#endif

   stream = fopen (path, "w");
   if (!stream) {
      fprintf (stderr, "Failed to open \"%s\", aborting.\n", path);
      exit (EXIT_FAILURE);
   }

   col = mongoc_client_get_collection (client, database, collection);
   cursor = mongoc_collection_find (col, MONGOC_QUERY_NONE, 0, 0, 0,
                                    &query, NULL, NULL);

   while (mongoc_cursor_next (cursor, &doc)) {
      if (BSON_UNLIKELY (doc->len != fwrite (bson_get_data (doc), 1, doc->len, stream))) {
         fprintf (stderr, "Failed to write %u bytes to %s\n",
                  doc->len, path);
         ret = EXIT_FAILURE;
         goto cleanup;
      }
   }

   if (mongoc_cursor_error (cursor, &error)) {
      fprintf (stderr, "ERROR: %s\n", error.message);
      ret = EXIT_FAILURE;
   }

cleanup:
   bson_free (path);
   fclose (stream);
   mongoc_cursor_destroy (cursor);
   mongoc_collection_destroy (col);

   return ret;
}


static int
mongoc_dump_database (mongoc_client_t *client,
                      const char      *database,
                      const char      *collection)
{
   mongoc_database_t *db;
   bson_error_t error;
   char *path;
   char **str;
   int ret = EXIT_SUCCESS;
   int i;

   BSON_ASSERT (database);

   path = bson_strdup_printf ("dump/%s", database);
   if (!mongoc_dump_mkdir_p (path, 0750)) {
      fprintf (stderr, "failed to create directory \"%s\"", path);
      bson_free (path);
      return EXIT_FAILURE;
   }

   bson_free (path);

   if (collection) {
      return mongoc_dump_collection (client, database, collection);
   }

   db = mongoc_client_get_database (client, database);
   str = mongoc_database_get_collection_names (db, &error);
   for (i = 0; str [i]; i++) {
      if (EXIT_SUCCESS != mongoc_dump_collection (client, database, str [i])) {
         ret = EXIT_FAILURE;
         goto cleanup;
      }
   }

cleanup:
   mongoc_database_destroy (db);
   bson_strfreev (str);

   return ret;
}


static int
mongoc_dump (mongoc_client_t *client,
             const char      *database,
             const char      *collection)
{
   bson_error_t error;
   char **str;
   int i;

   if (!mongoc_dump_mkdir_p ("dump", 0750)) {
      perror ("Failed to create directory \"dump\"");
      return EXIT_FAILURE;
   }

   if (database) {
      return mongoc_dump_database (client, database, collection);
   }

   if (!(str = mongoc_client_get_database_names (client, &error))) {
      fprintf (stderr, "Failed to fetch database names: %s\n",
               error.message);
      return EXIT_FAILURE;
   }

   for (i = 0; str [i]; i++) {
      if (EXIT_SUCCESS != mongoc_dump_database (client, str [i], NULL)) {
         bson_strfreev (str);
         return EXIT_FAILURE;
      }
   }

   bson_strfreev (str);

   return EXIT_SUCCESS;
}


static void
usage (FILE *stream)
{
   fprintf (stream,
"Usage: mongoc-dump [OPTIONS]\n"
"\n"
"Options:\n"
"\n"
"  -h HOST      Optional hostname to connect to [127.0.0.1].\n"
"  -p PORT      Optional port to connect to [27017].\n"
"  -d DBNAME    Optional database name to dump.\n"
"  -c COLNAME   Optional collection name to dump.\n"
"  --ssl        Use SSL when connecting to server.\n"
"\n");
}


int
main (int argc,
      char *argv[])
{
   mongoc_client_t *client;
   const char *collection = NULL;
   const char *database = NULL;
   const char *host = "127.0.0.1";
   uint16_t port = 27017;
   bool ssl = false;
   char *uri;
   int ret;
   int i;

   mongoc_init ();

   for (i = 1; i < argc; i++) {
      if (0 == strcmp (argv [i], "-c") && ((i + 1) < argc)) {
         collection = argv [++i];
      } else if (0 == strcmp (argv [i], "-d") && ((i + 1) < argc)) {
         database = argv [++i];
      } else if (0 == strcmp (argv [i], "--help")) {
         usage (stdout);
         return EXIT_SUCCESS;
      } else if (0 == strcmp (argv [i], "-h") && ((i + 1) < argc)) {
         host = argv [++i];
      } else if (0 == strcmp (argv [i], "--ssl")) {
         ssl = true;
      } else if (0 == strcmp (argv [i], "-p") && ((i + 1) < argc)) {
         port = atoi (argv [++i]);
         if (!port) {
            fprintf (stderr, "Invalid port \"%s\"", argv [i]);
            return EXIT_FAILURE;
         }
      } else {
         fprintf (stderr, "Unknown argument \"%s\"\n", argv [i]);
         return EXIT_FAILURE;
      }
   }

   uri = bson_strdup_printf ("mongodb://%s:%hu/%s?ssl=%s",
                             host,
                             port,
                             database ? database : "",
                             ssl ? "true" : "false");

   if (!(client = mongoc_client_new (uri))) {
      fprintf (stderr, "Invalid connection URI: %s\n", uri);
      return EXIT_FAILURE;
   }

   ret = mongoc_dump (client, database, collection);

   mongoc_client_destroy (client);

   return ret;
}