Uncategorized

backup – How to read and restore the last megabyte of a drive using Python


A block device, just like a regular file, has a size. You can query that, calculate the start of the last megabyte and seek there:

# assume you've already `open()`ed the file as f
# seek to end of file
f.seek(0, os.SEEK_END)
# figure out what byte that is
size = f.tell()
start_of_last_MB = size - 2**20
f.seek(start_of_last_MB)
last_MB_of_data = f.read()
backupfile = open("./end_backup", "wb")
backupfile.write(last_MB_of_data)
backupfile.close()

or similar.

However, of course, this, just as the start of the block device, of course are only the things that Linux sees; if the hardware hides the first or last MB from the operating system, this can’t circumvent that. Nothing could.

It’s pretty unusual to backup the first and last Megabyte separately – they’re useless without the stuff in between, and the stuff in between is useless without them. So, you’d basically always do a full-disk backup where you get these regions, anyways; or you’d do a backup of only the data-relevant parts (i.e., simply the relevant partitions, or really actually just the files as presented by the file system), and then these parts are irrelevant.



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *