Made an oops and deleted main.py in a Python project not under version control. Thought file recovery is trivial thanks to procfs, except that trick doesn't work with Python. Double oops. Here's what worked:
- Identify the underlying EXT partition with `mount` (it was /dev/mmcblk0p2 due to it being on a RPi)
- Open it with `debugfs /dev/mmcblk0p2`
- Execute the `blocks` command in the debugfs shell on the project directory and other files related to the deleted file to discover the associated blocks
- Given that the file was most likely written to a newer block than that, `dd if=/dev/mmcblk0p2 bs=4096 skip=<blocknumber> count=100000 | grep -a -F <unique line in deleted file> -C <number of lines of deleted file> | less` and go through the matches until the last one containing the last revision of the deleted file.
Going through this felt like an incredibly cursed variation of git, so I copied the last decent looking version of the file contents and backed them up :)