1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011 MediaGoblin contributors. See AUTHORS.
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
import mg_globals
18 from mediagoblin
.db
.open import setup_connection_and_db_from_config
19 from mediagoblin
.storage
.filestorage
import BasicFileStorage
20 from mediagoblin
.init
import setup_storage
, setup_global_and_app_config
30 from contextlib
import closing
32 _log
= logging
.getLogger('gmg.import_export')
34 _log
.setLevel(logging
.INFO
)
37 def import_export_parse_setup(subparser
):
39 subparser
.add_argument(
41 subparser
.add_argument(
42 '--mongodump_path', default
='mongodump',
43 help='mongodump binary')
44 subparser
.add_argument(
45 '--mongorestore_path', default
='mongorestore',
46 help='mongorestore binary')
47 subparser
.add_argument(
49 help='Temporary directory where files will be temporarily dumped')
52 def _import_media(db
, args
):
56 Must be called after _import_database()
58 _log
.info('-> Importing media...')
60 media_cache
= BasicFileStorage(
61 args
._cache
_path
['media'])
63 # TODO: Add import of queue files
64 queue_cache
= BasicFileStorage(
65 args
._cache
_path
['queue'])
67 for entry
in db
.MediaEntry
.find():
68 for name
, path
in entry
.media_files
.items():
69 _log
.info('Importing: {0} - {1}'.format(
73 media_file
= mg_globals
.public_store
.get_file(path
, mode
='wb')
75 media_cache
.get_file(path
, mode
='rb').read())
77 _log
.info('...Media imported')
80 def _import_database(db
, args
):
82 Restore mongo database from ___.bson files
84 _log
.info('-> Importing database...')
86 p
= subprocess
.Popen([
87 args
.mongorestore_path
,
89 os
.path
.join(args
._cache
_path
['database'], db
.name
)])
93 _log
.info('...Database imported')
98 Restore mongo database and media files from a tar archive
100 if not args
.cache_path
:
101 args
.cache_path
= tempfile
.mkdtemp()
103 setup_global_and_app_config(args
.conf_file
)
105 # Creates mg_globals.public_store and mg_globals.queue_store
108 global_config
, app_config
= setup_global_and_app_config(args
.conf_file
)
109 connection
, db
= setup_connection_and_db_from_config(
116 tf
.extractall(args
.cache_path
)
118 args
.cache_path
= os
.path
.join(
119 args
.cache_path
, 'mediagoblin-data')
120 args
= _setup_paths(args
)
122 # Import database from extracted data
123 _import_database(db
, args
)
125 _import_media(db
, args
)
130 def _setup_paths(args
):
132 Populate ``args`` variable with cache subpaths
134 args
._cache
_path
= dict()
138 'database': 'database'}
140 for key
, val
in PATH_MAP
.items():
141 args
._cache
_path
[key
] = os
.path
.join(args
.cache_path
, val
)
146 def _create_archive(args
):
148 Create the tar archive
150 _log
.info('-> Compressing to archive')
157 tf
.add(args
.cache_path
, 'mediagoblin-data/')
159 _log
.info('...Archiving done')
164 Remove cache directory
166 shutil
.rmtree(args
.cache_path
)
169 def _export_check(args
):
171 Run security checks for export command
173 if os
.path
.exists(args
.tar_file
):
174 overwrite
= raw_input(
175 'The output file already exists. '
176 'Are you **SURE** you want to overwrite it? '
178 if not overwrite
== 'yes':
186 def _export_database(db
, args
):
187 _log
.info('-> Exporting database...')
189 p
= subprocess
.Popen([
192 '-o', args
._cache
_path
['database']])
196 _log
.info('...Database exported')
199 def _export_media(db
, args
):
200 _log
.info('-> Exporting media...')
202 media_cache
= BasicFileStorage(
203 args
._cache
_path
['media'])
205 # TODO: Add export of queue files
206 queue_cache
= BasicFileStorage(
207 args
._cache
_path
['queue'])
209 for entry
in db
.MediaEntry
.find():
210 for name
, path
in entry
.media_files
.items():
211 _log
.info(u
'Exporting {0} - {1}'.format(
215 mc_file
= media_cache
.get_file(path
, mode
='wb')
217 mg_globals
.public_store
.get_file(path
, mode
='rb').read())
218 except Exception as e
:
219 _log
.error('Failed: {0}'.format(e
))
221 _log
.info('...Media exported')
224 def env_export(args
):
226 Export database and media files to a tar archive
229 if os
.path
.exists(args
.cache_path
):
230 _log
.error('The cache directory must not exist '
231 'before you run this script')
232 _log
.error('Cache directory: {0}'.format(args
.cache_path
))
236 args
.cache_path
= tempfile
.mkdtemp()
238 args
= _setup_paths(args
)
240 if not _export_check(args
):
241 _log
.error('Checks did not pass, exiting')
244 globa_config
, app_config
= setup_global_and_app_config(args
.conf_file
)
248 connection
, db
= setup_connection_and_db_from_config(
251 _export_database(db
, args
)
253 _export_media(db
, args
)
255 _create_archive(args
)