1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011 Free Software Foundation, Inc
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Affero General Public License for more details.
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 from mediagoblin
.gmg_commands
import util
as commands_util
18 from mediagoblin
import mg_globals
19 from mediagoblin
.db
import util
as db_util
20 from mediagoblin
.db
.open import setup_connection_and_db_from_config
21 from mediagoblin
.init
.config
import read_mediagoblin_config
28 def import_export_parse_setup(subparser
):
30 subparser
.add_argument(
32 subparser
.add_argument(
33 '-cf', '--conf_file', default
='mediagoblin.ini',
34 help='Config file used to set up environment')
35 subparser
.add_argument(
36 '--mongodump_cache', default
='mongodump',
37 help='mongodump cache directory')
38 subparser
.add_argument(
39 '--mongodump_path', default
='mongodump',
40 help='mongodump binary')
41 subparser
.add_argument(
42 '--mongorestore_path', default
='mongorestore',
43 help='mongorestore binary')
44 subparser
.add_argument(
45 '--extract_path', default
='/tmp/mediagoblin-import',
46 help='the directory to which the tarball should be extracted temporarily')
48 def _export_database(db
, args
):
49 print "\n== Exporting database ==\n"
51 command
= '{mongodump_path} -d {database} -o {mongodump_cache}'.format(
52 mongodump_path
=args
.mongodump_path
,
54 mongodump_cache
=args
.mongodump_cache
)
61 print "\n== Database exported ==\n"
63 def _import_database(db
, args
):
64 command
= '{mongorestore_path} -d {database} -o {mongodump_cache}'.format(
65 mongorestore_path
=args
.mongorestore_path
,
67 mongodump_cache
=args
.mongodump_cache
)
70 config
, validation_result
= read_mediagoblin_config(args
.conf_file
)
71 connection
, db
= setup_connection_and_db_from_config(
72 config
['mediagoblin'], use_pymongo
=True)
78 tf
.extractall(args
.extract_path
)
81 config
, validation_result
= read_mediagoblin_config(args
.conf_file
)
82 connection
, db
= setup_connection_and_db_from_config(
83 config
['mediagoblin'], use_pymongo
=True)
85 if os
.path
.exists(args
.tar_file
):
86 overwrite
= raw_input(
87 'The output file already exists. '
88 'Are you **SURE** you want to overwrite it? '
90 if not overwrite
== 'yes':
99 _export_database(db
, args
)
101 tf
.add(args
.mongodump_cache
)