<s> """<STR_LIT>""" <EOL> from __future__ import ( absolute_import , division , <EOL> print_function , unicode_literals ) <EOL> import os <EOL> import re <EOL> from datetime import datetime <EOL> import tempfile <EOL> from optparse import make_option <EOL> from shutil import copyfileobj <EOL> from django . conf import settings <EOL> from django . core . management . base import CommandError <EOL> from . _base import BaseDbBackupCommand <EOL> from ... dbcommands import DBCommands , MongoDBCommands <EOL> from ... storage . base import BaseStorage , StorageError <EOL> from ... import utils , settings as dbbackup_settings <EOL> class Command ( BaseDbBackupCommand ) : <EOL> help = """<STR_LIT>""" <EOL> option_list = BaseDbBackupCommand . option_list + ( <EOL> make_option ( "<STR_LIT:-c>" , "<STR_LIT>" , help = "<STR_LIT>" , action = "<STR_LIT:store_true>" , default = False ) , <EOL> make_option ( "<STR_LIT>" , "<STR_LIT>" , help = "<STR_LIT>" ) , <EOL> make_option ( "<STR_LIT>" , "<STR_LIT>" , help = "<STR_LIT>" ) , <EOL> make_option ( "<STR_LIT>" , "<STR_LIT>" , help = "<STR_LIT>" , action = "<STR_LIT:store_true>" , default = False ) , <EOL> make_option ( "<STR_LIT>" , "<STR_LIT>" , help = "<STR_LIT>" , action = "<STR_LIT:store_true>" , default = False ) , <EOL> make_option ( "<STR_LIT>" , "<STR_LIT>" , help = "<STR_LIT>" , default = None ) , <EOL> make_option ( "<STR_LIT>" , "<STR_LIT>" , help = "<STR_LIT>" , default = None ) , <EOL> ) <EOL> @ utils . email_uncaught_exception <EOL> def handle ( self , ** options ) : <EOL> """<STR_LIT>""" <EOL> self . verbosity = int ( options . get ( '<STR_LIT>' ) ) <EOL> self . quiet = options . get ( '<STR_LIT>' ) <EOL> self . clean = options . get ( '<STR_LIT>' ) <EOL> self . clean_keep = dbbackup_settings . CLEANUP_KEEP <EOL> self . database = options . get ( '<STR_LIT>' ) <EOL> self . servername = options . get ( '<STR_LIT>' ) <EOL> self . compress = options . get ( '<STR_LIT>' ) <EOL> self . encrypt = options . get ( '<STR_LIT>' ) <EOL> self . filename = options . get ( '<STR_LIT>' ) <EOL> self . path = options . get ( '<STR_LIT>' ) <EOL> self . storage = BaseStorage . storage_factory ( ) <EOL> database_keys = ( self . database , ) if self . database else dbbackup_settings . DATABASES <EOL> for database_key in database_keys : <EOL> database = settings . DATABASES [ database_key ] <EOL> if '<STR_LIT>' in database [ '<STR_LIT>' ] : <EOL> self . dbcommands = MongoDBCommands ( database ) <EOL> else : <EOL> self . dbcommands = DBCommands ( database ) <EOL> try : <EOL> self . _save_new_backup ( database ) <EOL> if self . clean : <EOL> self . _cleanup_old_backups ( database ) <EOL> except StorageError as err : <EOL> raise CommandError ( err ) <EOL> def _save_new_backup ( self , database ) : <EOL> """<STR_LIT>""" <EOL> if not self . quiet : <EOL> self . logger . info ( "<STR_LIT>" , database [ '<STR_LIT>' ] ) <EOL> filename = self . dbcommands . filename ( self . servername ) <EOL> outputfile = tempfile . SpooledTemporaryFile ( <EOL> max_size = dbbackup_settings . TMP_FILE_MAX_SIZE , <EOL> dir = dbbackup_settings . TMP_DIR ) <EOL> self . dbcommands . run_backup_commands ( outputfile ) <EOL> if self . compress : <EOL> compressed_file , filename = utils . compress_file ( outputfile , filename ) <EOL> outputfile = compressed_file <EOL> if self . encrypt : <EOL> encrypted_file , filename = utils . encrypt_file ( outputfile , filename ) <EOL> outputfile = encrypted_file <EOL> filename = self . filename if self . filename else filename <EOL> if not self . quiet : <EOL> self . logger . info ( "<STR_LIT>" , utils . handle_size ( outputfile ) ) <EOL> if self . path is None : <EOL> self . logger . info ( "<STR_LIT>" , <EOL> self . storage . name , self . storage . backup_dir , <EOL> filename ) <EOL> self . storage . write_file ( outputfile , filename ) <EOL> else : <EOL> self . logger . info ( "<STR_LIT>" , self . path ) <EOL> self . write_local_file ( outputfile , self . path ) <EOL> def _cleanup_old_backups ( self , database ) : <EOL> """<STR_LIT>""" <EOL> self . logger . info ( "<STR_LIT>" , database [ '<STR_LIT>' ] ) <EOL> filepaths = self . storage . list_directory ( ) <EOL> filepaths = self . dbcommands . filter_filepaths ( filepaths ) <EOL> for filepath in sorted ( filepaths [ <NUM_LIT:0> : - self . clean_keep ] ) : <EOL> regex = r'<STR_LIT>' % self . dbcommands . filename_match ( self . servername , '<STR_LIT>' ) <EOL> datestr = re . findall ( regex , os . path . basename ( filepath ) ) [ <NUM_LIT:0> ] <EOL> dateTime = datetime . strptime ( datestr , dbbackup_settings . DATE_FORMAT ) <EOL> if int ( dateTime . strftime ( "<STR_LIT>" ) ) != <NUM_LIT:1> : <EOL> if not self . quiet : <EOL> self . logger . info ( "<STR_LIT>" , filepath ) <EOL> self . storage . delete_file ( filepath ) <EOL> def write_local_file ( self , outputfile , path ) : <EOL> """<STR_LIT>""" <EOL> outputfile . seek ( <NUM_LIT:0> ) <EOL> with open ( path , '<STR_LIT:wb>' ) as fd : <EOL> copyfileobj ( outputfile , fd ) </s>
