Saturday, September 27, 2014

Psql Snapshot

Ever want to make some temporary changes to a (test) database, that you can't do with just commits? I can't remember why I needed this, but it looks handy.

# This was to create a quick snapshot of the database, make the changes I
# wanted, and then revert the database to exactly the way it was before I
# snapshotted it.

/etc/init.d/postgresql stop
umount /var/lib/postgresql/
lvcreate --snapshot --size 1G --name postgres-snap /dev/vg0/postgres
mount /dev/vg0/postgres-snap /var/lib/postgresql/
/etc/init.d/postgresql start

read -p "Press enter to start watching. And then ^C to drop the snapshot and switch to the old database" zzz
watch lvs

/etc/init.d/postgresql stop
umount /dev/vg0/postgres-snap && /sbin/lvremove --force /dev/vg0/postgres-snap
mount /var/lib/postgresql/
/etc/init.d/postgresql start

No comments:

Post a Comment