Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 10, 2025

Description

Restoring compressed media backups fails with tarfile.ReadError: invalid header when using the --uncompress flag. The issue occurs because mediarestore.py relies on tarfile's built-in gzip decompression (mode="r:gz"), which doesn't work reliably with all file-like objects after storage operations.

Changes:

  • Modified mediarestore.py to use utils.uncompress_file() before opening tarfile, matching the pattern in dbrestore.py
  • Added test coverage for compressed and uncompressed media backup restoration
  • Updated changelog

The fix ensures decompression happens explicitly before tarfile processing:

# Before: Unreliable with file-like objects
tar_file = tarfile.open(fileobj=input_file, mode="r:gz" if self.uncompress else "r:")

# After: Explicit decompression first
if self.uncompress:
    uncompressed_file, input_filename = utils.uncompress_file(input_file, input_filename)
    input_file.close()
    input_file = uncompressed_file

tar_file = tarfile.open(fileobj=input_file, mode="r:")

Checklist

Please update this checklist as you complete each item:

  • Tests have been developed for bug fixes or new functionality.
  • The changelog has been updated, if necessary.
  • Documentation has been updated, if necessary.
  • GitHub Issues closed by this PR have been linked.

By submitting this pull request I agree that all contributions comply with this project's open source license(s).

Original prompt

This section details on the original issue you should resolve

<issue_title>restore compresses backup will not work</issue_title>
<issue_description>
Restore a compressed media backup results in:

  File "/home/jens/repos/PyInventory/.venv/lib/python3.12/site-packages/dbbackup/management/commands/mediarestore.py", line 67, in handle
    self._restore_backup()
  File "/home/jens/repos/PyInventory/.venv/lib/python3.12/site-packages/dbbackup/management/commands/mediarestore.py", line 104, in _restore_backup
    else tarfile.open(fileobj=input_file, mode="r:")
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/tarfile.py", line 1875, in open
    return func(name, filemode, fileobj, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/tarfile.py", line 1907, in taropen
    return cls(name, mode, fileobj, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/tarfile.py", line 1765, in __init__
    self.firstmember = self.next()
                       ^^^^^^^^^^^
  File "/usr/lib/python3.12/tarfile.py", line 2753, in next
    raise ReadError(str(e)) from None
tarfile.ReadError: invalid header

I used django-dbbackup v5.0.1 for creating and restore of the backup.
btw.: it worked without compression ;)

</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI changed the title [WIP] Fix restore issue for compressed media backup Fix compressed media backup restoration Dec 10, 2025
Copilot AI requested a review from Archmonger December 10, 2025 08:20
@Archmonger Archmonger marked this pull request as ready for review December 10, 2025 08:51
@Archmonger Archmonger merged commit e4b49d1 into master Dec 17, 2025
19 checks passed
@Archmonger Archmonger deleted the copilot/fix-compressed-backup-restore branch December 17, 2025 10:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

restore compresses backup will not work

2 participants