diff --git a/sapl/legacy/management/commands/migracao_25_31.py b/sapl/legacy/management/commands/migracao_25_31.py index 1298feeb3..27591e058 100644 --- a/sapl/legacy/management/commands/migracao_25_31.py +++ b/sapl/legacy/management/commands/migracao_25_31.py @@ -1,7 +1,7 @@ from django.core import management from django.core.management.base import BaseCommand -from sapl.legacy.migracao import migrar +from sapl.legacy.migracao import migrar, migrar_dados class Command(BaseCommand): @@ -10,13 +10,24 @@ class Command(BaseCommand): def add_arguments(self, parser): parser.add_argument( - '-f', + '--force', action='store_true', default=False, dest='force', help='Não interativa: pula confirmação de exclusão dos dados', ) + parser.add_argument( + '--dados', + action='store_true', + default=False, + dest='dados', + help='migra somente dados', + ) def handle(self, *args, **options): management.call_command('migrate') - migrar(interativo=not options['force']) + somente_dados, interativo = options['dados'], not options['force'] + if somente_dados: + migrar_dados(interativo=interativo) + else: + migrar(interativo=interativo) diff --git a/sapl/legacy/migracao.py b/sapl/legacy/migracao.py index a8fed932f..2690e9a53 100644 --- a/sapl/legacy/migracao.py +++ b/sapl/legacy/migracao.py @@ -1,9 +1,42 @@ +import subprocess +import tarfile + +from django.conf import settings + from sapl.legacy.migracao_dados import migrar_dados from sapl.legacy.migracao_documentos import migrar_documentos from sapl.legacy.migracao_usuarios import migrar_usuarios def migrar(interativo=False): - migrar_dados(interativo) + migrar_dados(interativo=interativo) migrar_usuarios() migrar_documentos() + + +# fonte: https://stackoverflow.com/a/17081026/1877490 +def make_tarfile(output_filename, source_dir): + with tarfile.open(output_filename, "w:gz") as tar: + tar.add(source_dir, arcname=os.path.basename(source_dir)) + + +def gerar_pacote(): + banco = settings.DATABASES['legacy']['NAME'] + + # backup do banco + print('Gerando backup do banco... ', end='', flush=True) + arq_backup = settings.MEDIA_ROOT.child('{}.backup'.format(banco)) + backup_cmd = ''' + pg_dump --host localhost --port 5432 --username postgres --no-password + --format custom --blobs --verbose --file {} {}'''.format( + arq_backup, banco) + subprocess.check_output(backup_cmd.split(), stderr=subprocess.DEVNULL) + print('SUCESSO') + + # tar de media/sapl + print('Criando tar de media... ', end='', flush=True) + tar_media = settings.MEDIA_ROOT.child('{}.media.tgz'.format(banco)) + dir_media = settings.MEDIA_ROOT.child('sapl') + with tarfile.open(tar_media, "w:gz") as tar: + tar.add(dir_media, arcname=dir_media.name) + print('SUCESSO') diff --git a/sapl/legacy/scripts/migra_um_db.sh b/sapl/legacy/scripts/migra_um_db.sh index 1a4e29920..aab47c20e 100755 --- a/sapl/legacy/scripts/migra_um_db.sh +++ b/sapl/legacy/scripts/migra_um_db.sh @@ -4,7 +4,7 @@ if [ $# -ge 2 ]; then # proteje pasta com dumps de alterações acidentais - chmod -R -w ~/migracao_sapl/sapl_dumps + # chmod -R -w ~/migracao_sapl/sapl_dumps DATE=$(date +%Y-%m-%d) DIR=~/${DATE}_logs_migracao @@ -37,7 +37,7 @@ if [ $# -ge 2 ]; then echo "--- MIGRACAO DE DADOS ---" | tee -a $LOG echo >> $LOG - DATABASE_NAME=$1 ./manage.py migracao_25_31 -f --settings sapl.legacy_migration_settings |& tee -a $LOG + DATABASE_NAME=$1 ./manage.py migracao_25_31 --force --dados --settings sapl.legacy_migration_settings 2>&1 | tee -a $LOG echo >> $LOG else echo "USO:"