[轉帖]PG的簡單備份恢復 找時間進行測試
轉帖PG的簡單使用
https://blog.csdn.net/lk_db/article/details/77971634
一: 純檔案格式的指令碼:
示例:
1. 只匯出postgres資料庫的資料,不包括模式 -s
pg_dump -U postgres -f /postgres.sql -s postgres(資料庫名)
2. 匯出postgres資料庫(包括資料)
pg_dump -U postgres -f /postgres.sql postgres(資料庫名)
3. 匯出postgres資料庫中表test01的資料
create database "test01" with owner="postgres" encoding='utf-8';(單引號,雙引號不能錯)
pg_dump -U postgres -f /postgres.sql -t test01 postgres(資料庫名)
4. 匯出postgres資料庫中表test01的資料,以insert語句的形式
pg_dump -U postgres -f /postgres.sql -t test01 --column-inserts postgres(資料庫名)
5. 恢復資料到bk01資料庫
psql -U postgres -f /postgres.sql bk01
二、 使用歸檔檔案格式:
pg_restore
使用pg_restore純文字恢復純文字格式的指令碼,無法恢復
[
pg_restore: [archiver] input file appears to be a text format dump. Please use psql.
pg_restore和歸檔檔案格式一起使用重建資料庫。
1. 先備份:
pg_dump -U postgres -F t -f /vendemo.tar vendemo 備份下來有800多k
. 恢復:
pg_restore -U postgres -d bk01 /vendemo.tar
2. 先備份:
pg_dump -U postgres -F c -f /vendemo.tar vendemo 備份下來有300多k
. 恢復:
pg_restore -U postgres -d bk01 /vendemo.tar
三、 壓縮備份與恢復:
處理大資料庫:
1. 使用壓縮的轉儲. 使用你熟悉的壓縮程式,比如說 gzip。
. 先備份:
pg_dump -U postgres vendemo | gzip > /vendemo.gz 備份下來只有30多k
. 恢復:
gunzip -c /vendemo.gz | psql -U postgres bk02
或者
cat /vendemo.gz | gunzip | psql -U postgres bk02
2. 使用 split。. split 命令允許你 你用下面的方法把輸出分解成作業系統可以接受的大小。 比如,讓每個塊大小為 1 兆位元組:
. 先備份:
pg_dump -U postgres -d vendemo | split -b 100k - /vend/vend
匯出來的樣子是 vendaa 100k
vendab 100k
vendac 100k
vendad 16k
. 恢復:
cat /vend/vend* | psql -U postgres bk02