36
You’d Better Have Tested Backups... PostgreSQL Conference Europe, 2014 Dimitri Fontaine [email protected] @tapoueh Oct 22,2014 Dimitri Fontaine [email protected] @tapoueh You’d Better Have Tested Backups... Oct 22,2014 1 / 36

You’d Better Have Tested Backups - Dimitri Fontaine, PostgreSQL … · You’d Better Have Tested Backups... PostgreSQL Conference Europe, 2014 Dimitri Fontaine [email protected]

Embed Size (px)

Citation preview

Page 1: You’d Better Have Tested Backups - Dimitri Fontaine, PostgreSQL … · You’d Better Have Tested Backups... PostgreSQL Conference Europe, 2014 Dimitri Fontaine dimitri@2ndQuadrant.fr

You’d Better Have Tested Backups...PostgreSQL Conference Europe, 2014

Dimitri Fontaine [email protected]

@tapoueh

Oct 22,2014

Dimitri Fontaine [email protected] @tapouehYou’d Better Have Tested Backups... Oct 22,2014 1 / 36

Page 2: You’d Better Have Tested Backups - Dimitri Fontaine, PostgreSQL … · You’d Better Have Tested Backups... PostgreSQL Conference Europe, 2014 Dimitri Fontaine dimitri@2ndQuadrant.fr

Dimitri Fontaine

2ndQuadrant FrancePostgreSQL Major Contributor

• pgloader

• prefix, skytools

• apt.postgresql.org

• CREATE EXTENSION

• CREATE EVENT TRIGGER

• Bi-Directional Replication

• pginstall

Dimitri Fontaine [email protected] @tapouehYou’d Better Have Tested Backups... Oct 22,2014 2 / 36

Page 3: You’d Better Have Tested Backups - Dimitri Fontaine, PostgreSQL … · You’d Better Have Tested Backups... PostgreSQL Conference Europe, 2014 Dimitri Fontaine dimitri@2ndQuadrant.fr

You’d Better Have Tested Backups...

Dimitri Fontaine [email protected] @tapouehYou’d Better Have Tested Backups... Oct 22,2014 3 / 36

Page 4: You’d Better Have Tested Backups - Dimitri Fontaine, PostgreSQL … · You’d Better Have Tested Backups... PostgreSQL Conference Europe, 2014 Dimitri Fontaine dimitri@2ndQuadrant.fr

In fact, backups are not interesting

Dimitri Fontaine [email protected] @tapouehYou’d Better Have Tested Backups... Oct 22,2014 4 / 36

Page 5: You’d Better Have Tested Backups - Dimitri Fontaine, PostgreSQL … · You’d Better Have Tested Backups... PostgreSQL Conference Europe, 2014 Dimitri Fontaine dimitri@2ndQuadrant.fr

Actually, automated recovery testing

Dimitri Fontaine [email protected] @tapouehYou’d Better Have Tested Backups... Oct 22,2014 5 / 36

Page 6: You’d Better Have Tested Backups - Dimitri Fontaine, PostgreSQL … · You’d Better Have Tested Backups... PostgreSQL Conference Europe, 2014 Dimitri Fontaine dimitri@2ndQuadrant.fr

Use a battle tested tool

WHY USE BARMAN? Barman: disaster recovery for business criticalPostgreSQL databases

Dimitri Fontaine [email protected] @tapouehYou’d Better Have Tested Backups... Oct 22,2014 6 / 36

Page 7: You’d Better Have Tested Backups - Dimitri Fontaine, PostgreSQL … · You’d Better Have Tested Backups... PostgreSQL Conference Europe, 2014 Dimitri Fontaine dimitri@2ndQuadrant.fr

Today we’re talking about what happens when you don’t

Dimitri Fontaine [email protected] @tapouehYou’d Better Have Tested Backups... Oct 22,2014 7 / 36

Page 8: You’d Better Have Tested Backups - Dimitri Fontaine, PostgreSQL … · You’d Better Have Tested Backups... PostgreSQL Conference Europe, 2014 Dimitri Fontaine dimitri@2ndQuadrant.fr

Backups? we have a shell script!

Dimitri Fontaine [email protected] @tapouehYou’d Better Have Tested Backups... Oct 22,2014 8 / 36

Page 9: You’d Better Have Tested Backups - Dimitri Fontaine, PostgreSQL … · You’d Better Have Tested Backups... PostgreSQL Conference Europe, 2014 Dimitri Fontaine dimitri@2ndQuadrant.fr

Shell Script, ENV, missing setup

find $PGDATA -mtime +5 | xargs rm -f

Dimitri Fontaine [email protected] @tapouehYou’d Better Have Tested Backups... Oct 22,2014 9 / 36

Page 10: You’d Better Have Tested Backups - Dimitri Fontaine, PostgreSQL … · You’d Better Have Tested Backups... PostgreSQL Conference Europe, 2014 Dimitri Fontaine dimitri@2ndQuadrant.fr

And now what?

Dimitri Fontaine [email protected] @tapouehYou’d Better Have Tested Backups... Oct 22,2014 10 / 36

Page 11: You’d Better Have Tested Backups - Dimitri Fontaine, PostgreSQL … · You’d Better Have Tested Backups... PostgreSQL Conference Europe, 2014 Dimitri Fontaine dimitri@2ndQuadrant.fr

Data recovery

Try to recover from what backups we do have:only WAL files, no basebackup

• pg controldata and xlogdump

• initdb then

• hexedit pg control

• 5923145491842547187 to 52 33 3D 71 52 3B 3D F3

• then actually F3 3D 3B 52 71 3D 33 52

• play with the WAL we have and pg resetxlog

• no luck this time, no working around missing WAL files

Dimitri Fontaine [email protected] @tapouehYou’d Better Have Tested Backups... Oct 22,2014 11 / 36

Page 12: You’d Better Have Tested Backups - Dimitri Fontaine, PostgreSQL … · You’d Better Have Tested Backups... PostgreSQL Conference Europe, 2014 Dimitri Fontaine dimitri@2ndQuadrant.fr

Back to having that PostgreSQL running

First, backup the physical filesleft available

Dimitri Fontaine [email protected] @tapouehYou’d Better Have Tested Backups... Oct 22,2014 12 / 36

Page 13: You’d Better Have Tested Backups - Dimitri Fontaine, PostgreSQL … · You’d Better Have Tested Backups... PostgreSQL Conference Europe, 2014 Dimitri Fontaine dimitri@2ndQuadrant.fr

Back to having that PostgreSQL running

logs complain about pg filenode.map

od -j 8 -N $((512-8-8)) -td4

< $PGDATA/global/pg_filenode.map

Dimitri Fontaine [email protected] @tapouehYou’d Better Have Tested Backups... Oct 22,2014 13 / 36

Page 14: You’d Better Have Tested Backups - Dimitri Fontaine, PostgreSQL … · You’d Better Have Tested Backups... PostgreSQL Conference Europe, 2014 Dimitri Fontaine dimitri@2ndQuadrant.fr

Back to having that PostgreSQL running

logs complain about pg clog

> (code-char #b01010101)

#\U

for c in 0000 0001 0002 0003 0004 0005 \

0006 0007 0008 0009 000A 000B 000C

do

dd if=/dev/zero bs=256k count=1 | tr ’\0’ ’U’ > $c

done

Dimitri Fontaine [email protected] @tapouehYou’d Better Have Tested Backups... Oct 22,2014 14 / 36

Page 15: You’d Better Have Tested Backups - Dimitri Fontaine, PostgreSQL … · You’d Better Have Tested Backups... PostgreSQL Conference Europe, 2014 Dimitri Fontaine dimitri@2ndQuadrant.fr

Now PostgreSQL starts.

But is complaining about missing pg database mappings

Dimitri Fontaine [email protected] @tapouehYou’d Better Have Tested Backups... Oct 22,2014 15 / 36

Page 16: You’d Better Have Tested Backups - Dimitri Fontaine, PostgreSQL … · You’d Better Have Tested Backups... PostgreSQL Conference Europe, 2014 Dimitri Fontaine dimitri@2ndQuadrant.fr

Now PostgreSQL starts.

But is complaining about missing pg database mappings

Dimitri Fontaine [email protected] @tapouehYou’d Better Have Tested Backups... Oct 22,2014 16 / 36

Page 17: You’d Better Have Tested Backups - Dimitri Fontaine, PostgreSQL … · You’d Better Have Tested Backups... PostgreSQL Conference Europe, 2014 Dimitri Fontaine dimitri@2ndQuadrant.fr

How to provide for your own mapping?

select oid, relname, pg_relation_filenode(oid)

from pg_class

where relname = ’pg_database’;

oid | relname | pg_relation_filenode

------+-------------+----------------------

1262 | pg_database | 12319

(1 row)

Dimitri Fontaine [email protected] @tapouehYou’d Better Have Tested Backups... Oct 22,2014 17 / 36

Page 18: You’d Better Have Tested Backups - Dimitri Fontaine, PostgreSQL … · You’d Better Have Tested Backups... PostgreSQL Conference Europe, 2014 Dimitri Fontaine dimitri@2ndQuadrant.fr

How to provide for your own mapping?

$ strings $PGDATA/global/12319

postgres

template0

template1

Dimitri Fontaine [email protected] @tapouehYou’d Better Have Tested Backups... Oct 22,2014 18 / 36

Page 19: You’d Better Have Tested Backups - Dimitri Fontaine, PostgreSQL … · You’d Better Have Tested Backups... PostgreSQL Conference Europe, 2014 Dimitri Fontaine dimitri@2ndQuadrant.fr

How to provide for your own mapping?

CREATE DATABASE ... WITH OIDS ...;

Dimitri Fontaine [email protected] @tapouehYou’d Better Have Tested Backups... Oct 22,2014 19 / 36

Page 20: You’d Better Have Tested Backups - Dimitri Fontaine, PostgreSQL … · You’d Better Have Tested Backups... PostgreSQL Conference Europe, 2014 Dimitri Fontaine dimitri@2ndQuadrant.fr

Now we can connect to a database!

Only to hit a never seen before error message:

FATAL: database "dbname" does not exists

DETAIL: Database OID 17838 now seems to belong to "otherdb"

Dimitri Fontaine [email protected] @tapouehYou’d Better Have Tested Backups... Oct 22,2014 20 / 36

Page 21: You’d Better Have Tested Backups - Dimitri Fontaine, PostgreSQL … · You’d Better Have Tested Backups... PostgreSQL Conference Europe, 2014 Dimitri Fontaine dimitri@2ndQuadrant.fr

Start without the system indexes

As one does.

$ pg_ctl start -o "-P"

$ cat > $PGDATA/postgresql.conf <<EOF

enable_indexscan = off

enable_bitmapscan = off

enable_indexonlyscan = off

EOF

$ pg_ctl reload

Dimitri Fontaine [email protected] @tapouehYou’d Better Have Tested Backups... Oct 22,2014 21 / 36

Page 22: You’d Better Have Tested Backups - Dimitri Fontaine, PostgreSQL … · You’d Better Have Tested Backups... PostgreSQL Conference Europe, 2014 Dimitri Fontaine dimitri@2ndQuadrant.fr

Now we can query the catalogs!

psql and list tables, \dtbut

base/16384/12062 is missing

select oid, relname, pg_relation_filenode(oid)

from pg_class

where pg_relation_filenode(oid) = 12062;

oid | relname | pg_relation_filenode

------+---------+----------------------

1255 | pg_proc | 12062

(1 row)

Dimitri Fontaine [email protected] @tapouehYou’d Better Have Tested Backups... Oct 22,2014 22 / 36

Page 23: You’d Better Have Tested Backups - Dimitri Fontaine, PostgreSQL … · You’d Better Have Tested Backups... PostgreSQL Conference Europe, 2014 Dimitri Fontaine dimitri@2ndQuadrant.fr

We lost the system catalogs...

Copy them over from a fresh initdb system.

Unless you did use some extensions...

Dimitri Fontaine [email protected] @tapouehYou’d Better Have Tested Backups... Oct 22,2014 23 / 36

Page 24: You’d Better Have Tested Backups - Dimitri Fontaine, PostgreSQL … · You’d Better Have Tested Backups... PostgreSQL Conference Europe, 2014 Dimitri Fontaine dimitri@2ndQuadrant.fr

Missing pg namespace

But the application is using custom schemas.

Dimitri Fontaine [email protected] @tapouehYou’d Better Have Tested Backups... Oct 22,2014 24 / 36

Page 25: You’d Better Have Tested Backups - Dimitri Fontaine, PostgreSQL … · You’d Better Have Tested Backups... PostgreSQL Conference Europe, 2014 Dimitri Fontaine dimitri@2ndQuadrant.fr

How is pg namespace stored?

select oid, * from pg_namespace;

oid | nspname | nspowner | nspacl

-------+--------------------+----------+----------------------

99 | pg_toast | 10 |

11222 | pg_temp_1 | 10 |

11223 | pg_toast_temp_1 | 10 |

11 | pg_catalog | 10 | {dim=UC/dim,=U/dim}

2200 | public | 10 | {dim=UC/dim,=UC/dim}

11755 | information_schema | 10 | {dim=UC/dim,=U/dim}

(6 rows)

Dimitri Fontaine [email protected] @tapouehYou’d Better Have Tested Backups... Oct 22,2014 25 / 36

Page 26: You’d Better Have Tested Backups - Dimitri Fontaine, PostgreSQL … · You’d Better Have Tested Backups... PostgreSQL Conference Europe, 2014 Dimitri Fontaine dimitri@2ndQuadrant.fr

Figuring out the file content

# copy pg_namespace to stdout with oids;

99 −〉|pg_toast −〉|10 −〉|\N

11222 −〉|pg_temp_1 −〉|10 −〉|\N

11223 −〉|pg_toast_temp_1−〉|10 −〉|\N11 −〉|pg_catalog −〉|10 −〉|{dim=UC/dim,=U/dim}2200 −〉|public −〉|10 −〉|{dim=UC/dim,=UC/dim}11755 −〉|information_schema −〉|10 −〉|{dim=UC/dim,=U/dim}

Dimitri Fontaine [email protected] @tapouehYou’d Better Have Tested Backups... Oct 22,2014 26 / 36

Page 27: You’d Better Have Tested Backups - Dimitri Fontaine, PostgreSQL … · You’d Better Have Tested Backups... PostgreSQL Conference Europe, 2014 Dimitri Fontaine dimitri@2ndQuadrant.fr

Add our namespaces live, with the right OIDs

# copy pg_namespace from stdin with oids;

Enter data to be copied followed by a newline.

End with a backslash and a period on a line by itself.

>> 16443 −〉|my_namespace −〉|10 −〉|\N

>> \.

Dimitri Fontaine [email protected] @tapouehYou’d Better Have Tested Backups... Oct 22,2014 27 / 36

Page 28: You’d Better Have Tested Backups - Dimitri Fontaine, PostgreSQL … · You’d Better Have Tested Backups... PostgreSQL Conference Europe, 2014 Dimitri Fontaine dimitri@2ndQuadrant.fr

Wait, where the OID is coming from?

# select c.oid, relname, relnamespace, nspname

from pg_class c

left join pg_namespace n

on n.oid = c.relnamespace

where relname = ’bar’;

oid | relname | relnamespace | nspname

-------+---------+--------------+---------

16446 | bar | 16443 |

(1 row)

Dimitri Fontaine [email protected] @tapouehYou’d Better Have Tested Backups... Oct 22,2014 28 / 36

Page 29: You’d Better Have Tested Backups - Dimitri Fontaine, PostgreSQL … · You’d Better Have Tested Backups... PostgreSQL Conference Europe, 2014 Dimitri Fontaine dimitri@2ndQuadrant.fr

Now we can query the catalogs

But what we want is the data, not the metadata.

Dimitri Fontaine [email protected] @tapouehYou’d Better Have Tested Backups... Oct 22,2014 29 / 36

Page 30: You’d Better Have Tested Backups - Dimitri Fontaine, PostgreSQL … · You’d Better Have Tested Backups... PostgreSQL Conference Europe, 2014 Dimitri Fontaine dimitri@2ndQuadrant.fr

We are lucky here!

We didn’t lose pg attribute, only pg attrdef

# \d a

Table "public.a"

Column | Type | Modifiers

--------+---------+------------------------------------------------

id | integer | not null default nextval(’a_id_seq’::regclass)

f1 | text |

Indexes:

"a_pkey" PRIMARY KEY, btree (id)

Dimitri Fontaine [email protected] @tapouehYou’d Better Have Tested Backups... Oct 22,2014 30 / 36

Page 31: You’d Better Have Tested Backups - Dimitri Fontaine, PostgreSQL … · You’d Better Have Tested Backups... PostgreSQL Conference Europe, 2014 Dimitri Fontaine dimitri@2ndQuadrant.fr

What’s pg attrdef like already?

# select adrelid, adnum, adsrc

from pg_attrdef

where adrelid = ’public.a’::regclass;

adrelid | adnum | adsrc

---------+-------+-------------------------------

16411 | 1 | nextval(’a_id_seq’::regclass)

(1 row)

Dimitri Fontaine [email protected] @tapouehYou’d Better Have Tested Backups... Oct 22,2014 31 / 36

Page 32: You’d Better Have Tested Backups - Dimitri Fontaine, PostgreSQL … · You’d Better Have Tested Backups... PostgreSQL Conference Europe, 2014 Dimitri Fontaine dimitri@2ndQuadrant.fr

What’s pg attrdef like already?

# select attnum, atthasdef

from pg_attribute

where attrelid = ’public.a’::regclass

and atthasdef;

attnum | atthasdef

--------+-----------

1 | t

(1 row)

Dimitri Fontaine [email protected] @tapouehYou’d Better Have Tested Backups... Oct 22,2014 32 / 36

Page 33: You’d Better Have Tested Backups - Dimitri Fontaine, PostgreSQL … · You’d Better Have Tested Backups... PostgreSQL Conference Europe, 2014 Dimitri Fontaine dimitri@2ndQuadrant.fr

We are not creating new data in that instance, right?

# update pg_attribute

set atthasdef = false

where attrelid = ’my_namespace.bar’;

Dimitri Fontaine [email protected] @tapouehYou’d Better Have Tested Backups... Oct 22,2014 33 / 36

Page 34: You’d Better Have Tested Backups - Dimitri Fontaine, PostgreSQL … · You’d Better Have Tested Backups... PostgreSQL Conference Europe, 2014 Dimitri Fontaine dimitri@2ndQuadrant.fr

PostgreSQL is amazingly resilient

Dimitri Fontaine [email protected] @tapouehYou’d Better Have Tested Backups... Oct 22,2014 34 / 36

Page 35: You’d Better Have Tested Backups - Dimitri Fontaine, PostgreSQL … · You’d Better Have Tested Backups... PostgreSQL Conference Europe, 2014 Dimitri Fontaine dimitri@2ndQuadrant.fr

You should have a proper recovery plan.

WHY USE BARMAN? Barman: disaster recovery for business criticalPostgreSQL databases

Dimitri Fontaine [email protected] @tapouehYou’d Better Have Tested Backups... Oct 22,2014 35 / 36

Page 36: You’d Better Have Tested Backups - Dimitri Fontaine, PostgreSQL … · You’d Better Have Tested Backups... PostgreSQL Conference Europe, 2014 Dimitri Fontaine dimitri@2ndQuadrant.fr

Questions?

Now is the time to ask!

Dimitri Fontaine [email protected] @tapouehYou’d Better Have Tested Backups... Oct 22,2014 36 / 36