Kitaab

Recovering data from a qcow2 image

ops nix postgresql qemu

published 2023-05-24 14:30

updated 2023-05-27 01:02

https://gist.github.com/shamil/62935d9b456a6f9877b5

{{{bash sudo modprobe nbd qemu-nbd --connect=/dev/nbd0 .qcow2 sudo fdisk -l /dev/nbd0 sudo mount /dev/nbd0p2 }}}

You can now look around and explore the filesystem for any files you wanted

In particular I wanted my postgres databases, as a backup was taken a week ago, and I could still get the fresher data.

Getting postgres going

⚠ This is shady ⚠

I needed to edit the postgresql.conf to make it run in the /tmp directory, instead of granting permission of /run/postgresql (I don't run psql on my laptop)

postgresql.conf was already a broken symlink. The /nix/store was different.

{{{bash useradd postgres nix-shell -p postgresql_11 pg_ctl start --pgdata="/var/lib/postgresql/" sudo -u postgres psql -h /tmp

Look around for the databases and users you want to restore

sudo useradd freshrss sudo -u freshrss pg_dump -h /tmp freshrss > freshrss.sql

sudo useradd gitea sudo -u gitea pg_dump -h /tmp gitea > gitea.sql }}}

= Don't forget to clean up! =

{{{bash sudo umount qemu-nbd --disconnect /dev/ndb0 pg_ctl stop --pgdata="/var/lib/postgresql/" sudo userdel postgres sudo userdel gitea sudo userdel freshrss }}}