catatanlepas.com

  • Increase font size
  • Default font size
  • Decrease font size
  • default color
  • black color
Home Komputer Pemrograman Script Untuk Membackup Database Postgresql

Script Untuk Membackup Database Postgresql

Email Cetak PDF
Pada artikel sebelumnya sudah dijelaskan tentang bagaimana caranya membackup mysql menggunakan script. Artikel kali ini menjelaskan tentang bagaimana caranya membackup seluruh database postgresql menggunakan script. Script yang digunakan sama seperti script yang digunakan untuk membackup mysql, hanya saja ada beberapa bagian yang harus dirubah. Misalnya database postgresql kita seperti berikut:
# su - postgres -c"psql -l"
        
 List of databases
     Name     |  Owner   | Encoding
--------------+----------+----------
 balance      | postgres | LATIN1
 hrmis        | postgres | UTF8
 account      | postgres | UTF8
 dwind        | postgres | UTF8
 postgres     | postgres | UTF8
 temp         | postgres | UTF8
 template0    | postgres | UTF8
 template1    | postgres | UTF8
 test1        | postgres | UTF8

Dan kita ingin membackup seluruh database kecuali template0 dan template1. Berikut adalah scriptnya:

#!/bin/bash
date=`date -d "-1 day" +%Y%m%d`
backupdir="/tmp/"
echo "Start All backup => `date`"
cd $backupdir
postgres=`su - postgres -c"psql -l"`
echo $postgres | tr -s " " "\n" | sed 1,8d | sed '/postgres/d' | sed '/(/d' | sed '/)/d' | sed '/UTF/d' | sed '/|/d' | sed '/template/d' | sed '/-/d' | sed '/LATIN1/d' | while read line;
do
index=1
echo -n "[$index]Backup Database : $line..."
echo "Start Backup => `date`"
pg_dump -i -h localhost -p 5432 -U postgres -F c -b -v -f $line.backup $line
echo "Finish Backup => `date`"
if [ $? -eq 0 ]; then
echo "OK[`du -h $backupdir/$line.backup | awk '{print $1}'`]"
else
echo "Fail"
fi
index=`expr $index + 1`
done
echo "Finish All Backup => `date`"
echo
directory="/tmp/backup_$date/"
if [ ! -d $directory ]; then
echo "$directory not existing, create it now automatically"
mkdir -p $directory
fi
chown -R postgres:postgres $directory
mv *.backup $directory
mv $directory /home/arief/postgres
chown -R noc:noc /home/arief/postgres
echo "Finish All => `date`"
echo "==============================================================="

Script diatas akan menghasilkan file backup postgres yang berakhiran .backup kemudian dipindahkan ke folder postgres yang ada di folder arief. Dan jangan lupa tuliskan file tersebut dalam user root.

Referensi:

Dari Berbagai Sumber
 

26 Shafar 1433 H
20 Januari 2012 M

+ 0
+ 0
Comments
Add New Search
Write comment
Name:
Email:
 
Website:
Title:
UBBCode:
[b] [i] [u] [url] [quote] [code] [img] 
 
 
:angry::0:confused::cheer:B):evil::silly::dry::lol::kiss::D:pinch:
:(:shock::X:side::):P:unsure::woohoo::huh::whistle:;):s
:!::?::idea::arrow:
 
Please input the anti-spam code that you can read in the image.

3.26 Copyright (C) 2008 Compojoom.com / Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved."

Terakhir Diperbaharui ( Jum'at, 20 Januari 2012 13:34 )  

Tanggal dan Jam


Ulti Clocks content

Browse this website in:


Mutiara Hari Ini

Dan Kami (Allah) tidak menciptakan langit dan bumi dan apa yang diantara keduanya dengan bermain-main. (Ad-Dukhan:38)

Poling

Apakah artikel-artikel di situs ini bermanfaat untuk anda?

(251 votes)

8.4%
91.6%

Waktu Shalat