| 1 |
""" |
| 2 |
Save media files. |
| 3 |
""" |
| 4 |
|
| 5 |
import os |
| 6 |
import tarfile |
| 7 |
|
| 8 |
from django.core.management.base import CommandError |
| 9 |
|
| 10 |
from dbbackup import utils |
| 11 |
from dbbackup.management.commands._base import BaseDbBackupCommand, make_option |
| 12 |
from dbbackup.signals import post_media_backup, pre_media_backup |
| 13 |
from dbbackup.storage import StorageError, get_storage, get_storage_class |
| 14 |
|
| 15 |
|
| 16 |
class Command(BaseDbBackupCommand): |
| 17 |
help = """Backup media files, gather all in a tarball and encrypt or |
| 18 |
compress.""" |
| 19 |
content_type = "media" |
| 20 |
|
| 21 |
option_list = ( |
| 22 |
*BaseDbBackupCommand.option_list, |
| 23 |
make_option("-c", "--clean", help="Clean up old backup files", action="store_true", default=False), |
| 24 |
make_option("-s", "--servername", help="Specify server name to include in backup filename"), |
| 25 |
make_option("-z", "--compress", help="Compress the archive", action="store_true", default=False), |
| 26 |
make_option("-e", "--encrypt", help="Encrypt the backup files", action="store_true", default=False), |
| 27 |
make_option("-o", "--output-filename", default=None, help="Specify filename on storage"), |
| 28 |
make_option( |
| 29 |
"-O", |
| 30 |
"--output-path", |
| 31 |
default=None, |
| 32 |
help="Specify where to store backup (local filesystem path or S3 URI like s3://bucket/path/)", |
| 33 |
), |
| 34 |
) |
| 35 |
|
| 36 |
@utils.email_uncaught_exception |
| 37 |
def handle(self, **options): |
| 38 |
self.verbosity = options.get("verbosity") |
| 39 |
self.quiet = options.get("quiet") |
| 40 |
self._set_logger_level() |
| 41 |
|
| 42 |
self.encrypt = options.get("encrypt", False) |
| 43 |
self.compress = options.get("compress", False) |
| 44 |
self.servername = options.get("servername") |
| 45 |
|
| 46 |
self.filename = options.get("output_filename") |
| 47 |
self.path = options.get("output_path") |
| 48 |
try: |
| 49 |
self.media_storage = get_storage_class()() |
| 50 |
self.storage = get_storage() |
| 51 |
self.backup_mediafiles() |
| 52 |
if options.get("clean"): |
| 53 |
self._cleanup_old_backups(servername=self.servername) |
| 54 |
|
| 55 |
except StorageError as err: |
| 56 |
raise CommandError(err) from err |
| 57 |
|
| 58 |
def _explore_storage(self): |
| 59 |
"""Generator of all files contained in media storage.""" |
| 60 |
path = "" |
| 61 |
dirs = [path] |
| 62 |
while dirs: |
| 63 |
path = dirs.pop() |
| 64 |
subdirs, files = self.media_storage.listdir(path) |
| 65 |
for media_filename in files: |
| 66 |
yield os.path.join(path, media_filename) |
| 67 |
dirs.extend([os.path.join(path, subdir) for subdir in subdirs]) |
| 68 |
|
| 69 |
def _create_tar(self, name): |
| 70 |
"""Create TAR file.""" |
| 71 |
fileobj = utils.create_spooled_temporary_file() |
| 72 |
mode = "w:gz" if self.compress else "w" |
| 73 |
tar_file = tarfile.open(name=name, fileobj=fileobj, mode=mode) |
| 74 |
for media_filename in self._explore_storage(): |
| 75 |
tarinfo = tarfile.TarInfo(media_filename) |
| 76 |
media_file = self.media_storage.open(media_filename) |
| 77 |
tarinfo.size = len(media_file) |
| 78 |
tar_file.addfile(tarinfo, media_file) |
| 79 |
# Close the TAR for writing |
| 80 |
tar_file.close() |
| 81 |
return fileobj |
| 82 |
|
| 83 |
def backup_mediafiles(self): |
| 84 |
""" |
| 85 |
Create backup file and write it to storage. |
| 86 |
""" |
| 87 |
# Send pre_media_backup signal |
| 88 |
pre_media_backup.send( |
| 89 |
sender=self.__class__, |
| 90 |
servername=self.servername, |
| 91 |
storage=self.storage, |
| 92 |
) |
| 93 |
|
| 94 |
# Check for filename option |
| 95 |
if self.filename: |
| 96 |
filename = self.filename |
| 97 |
else: |
| 98 |
extension = f"tar{'.gz' if self.compress else ''}" |
| 99 |
filename = utils.filename_generate(extension, servername=self.servername, content_type=self.content_type) |
| 100 |
|
| 101 |
tarball = self._create_tar(filename) |
| 102 |
# Apply trans |
| 103 |
if self.encrypt: |
| 104 |
encrypted_file = utils.encrypt_file(tarball, filename) |
| 105 |
tarball, filename = encrypted_file |
| 106 |
|
| 107 |
self.logger.debug("Backup size: %s", utils.handle_size(tarball)) |
| 108 |
# Store backup |
| 109 |
tarball.seek(0) |
| 110 |
if self.path is None: |
| 111 |
self.write_to_storage(tarball, filename) |
| 112 |
elif self.path.startswith("s3://"): |
| 113 |
# Handle S3 URIs through storage backend |
| 114 |
self.write_to_storage(tarball, self.path) |
| 115 |
else: |
| 116 |
self.write_local_file(tarball, self.path) |
| 117 |
|
| 118 |
# Send post_media_backup signal |
| 119 |
post_media_backup.send( |
| 120 |
sender=self.__class__, |
| 121 |
filename=filename, |
| 122 |
servername=self.servername, |
| 123 |
storage=self.storage, |
| 124 |
) |