PostgreSQL Tools နဲ့ Backup၊ Restore လုပ်ပါ

PostgreSQL 19.0

Database တစ်ခုကို logical backup ယူပြီး သီးသန့် database ထဲ ပြန်သွင်းလို့ရတာ စစ်ပါ။

ဒီစာမျက်နှာမှာ

Custom Format Backup ဖန်တီးပါ

pg_dump ကို -Fc နဲ့ သုံးပါ။ Setup ပြီးရင် OS terminal မှာ run ပါ။ Backup ယူမယ့် objects နဲ့ data ကို ဖတ်ခွင့်ရှိတဲ့ account သုံးပါ။ pg_dump client ရဲ့ major version က server နဲ့ တူရမယ် ဒါမှမဟုတ် ပိုအသစ်ဖြစ်ရပါမယ်။ Backup ဖိုင်ထဲမှာ database အချက်အလက် ပါလို့ သီးသန့် သိမ်းပါ။

pg_dump -h localhost -p 5432 -U postgres -Fc -f learning_db.dump learning_db
pg_restore --list learning_db.dump
pg_dump -h localhost -p 5432 -U postgres -Fc -f learning_db.dump learning_db
localhost မှာ postgres role နဲ့ ချိတ်ပြီး learning_db ကို ဖတ်တယ်။ -Fc နဲ့ custom-format archive ထုတ်ပြီး -f က သတ်မှတ်တဲ့ learning_db.dump ဖိုင်ထဲ သိမ်းတယ်။
pg_restore --list learning_db.dump
Archive ထဲက database objects စာရင်းကို ပြတယ်။ Database ထဲကို restore မလုပ်သေးပါဘူး။

Database အသစ်ထဲ Restore လုပ်ပါ

Database ဆောက်ခွင့်ရှိတဲ့ account နဲ့ test database အလွတ် ဆောက်ပါ။ Database owner က တခြား account ဖြစ်ရင် အဲဒီ account နဲ့ restore လုပ်ပါ။ ဒီ restore စမ်းသပ်မှုမှာ --no-owner နဲ့ --no-privileges က မူလ owners နဲ့ grants ကို ပြန်မထည့်ပါဘူး။ အရေးကြီးတဲ့ table တွေရဲ့ row count တိုက်ပြီး app ကနေ data ဖတ်စမ်းပါ။ Roles နဲ့ cluster တစ်ခုလုံးသုံး objects တွေအတွက် သီးသန့် backup စီစဉ်ပါ။ သတ်မှတ်ထားတဲ့ အချိန်တစ်ခုဆီ ပြန်သွားဖို့ လိုရင် host ရဲ့ point-in-time recovery tools သုံးပါ။

createdb -h localhost -p 5432 -U postgres learning_db_restore
pg_restore -h localhost -p 5432 -U postgres -d learning_db_restore --exit-on-error --no-owner --no-privileges learning_db.dump
psql -h localhost -p 5432 -U postgres -d learning_db_restore -c "SELECT count(*) FROM students;"
createdb … learning_db_restore
Restore ပြန်သွင်းမယ့် database အလွတ်ကို postgres နဲ့ ဆောက်တယ်။
pg_restore … -d learning_db_restore --exit-on-error
learning_db.dump ကို -d နဲ့ ရွေးထားတဲ့ database ထဲ ပြန်သွင်းတယ်။ --exit-on-error ကြောင့် ပထမ error မှာ ရပ်တယ်။
psql … -c "SELECT count(*) FROM students;"
Quote ထဲက SQL ကို တစ်ခါ run ပြီး terminal ဆီ ပြန်ထွက်တယ်။ students row အရေအတွက်ကို မူလ database နဲ့ တိုက်ကြည့်ပါ။

တရားဝင် ကိုးကားချက်များ