Minggu, Juni 15, 2008

Tutorial Auto Backup Database on Linux Server

Yesterday I and my wife went to RSUD Karanganyar to install and create auto backup database on Linux server.
I use Mandriva 2008 as an OS because it's easy to install.

After success installation, I created script to crontab every hour.
Here is my script, of course it's not my own script.

Ok, coffee and smoking mode on


Once again, this is not my own script, I modified from here.


1. Type this command in shell :


bash$ echo your_host:5432:your_user:your_db:your_pass > ~/.pgpass
bash$ chmod 600 ~/.pgpass


2. Create backup.sh in directory /backup and name it as backup.sh
:

#!/bin/sh
/usr/bin/pg_dump -D -U qwe simrs --no-owner > /backup/simrs.sql
cd /
cd /backup
tar --overwrite --remove-files -czf /backup/simrs-`date '+%Y-%m-%d'`.tar.gz -R *.sql
chmod ugo+r -R *
cp -f *.* /var/www/html/backup
cp -f *.* /mnt/win_d/backup


3. Edit /etc/crontab and add bold line in crontab file :


SHELL=/bin/bash

PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# run-parts
01 * * * * root nice -n 19 run-parts --report /etc/cron.hourly
01 * * * * root /backup/backup.sh #<-- add this line
02 4 * * * root nice -n 19 run-parts --report /etc/cron.daily
22 4 * * 0 root nice -n 19 run-parts --report /etc/cron.weekly
42 4 1 * * root nice -n 19 run-parts --report /etc/cron.monthly


4. run crontab as root

root@server:~# crontab -u root /etc/crontab


5. check one hour later, or just type :

root@server:~# crontab


After I check /backup directory there is new file simrs-data.tar.gz
I check again at /var/www/html/backup directory, there is also new file simrs-date.tar.gz
I double check again at /mnt/win_d/backup directory, well... it's succeeded

Thanks to open source community :)

3 comments:

Anonim mengatakan...

ati2 loh, nanti kena petir backupnya ilang semua :D

Anonim mengatakan...

tokit : santai bos, backup nya dah 3 tempat
1. /backup
2. /var/www/html/
3. /mnt/win_d

dah pesen sama bu ewi supaya backupnya diambil tiap satu jam sekali :D

Tamrin mengatakan...

Aq juga sebenernya pengen nyerah ber Adsense, tapi kalo ada ngajarin, why not? Aq juga bru 8 hari gaBung dan udah baca sebagian modul. Ternyata ada sesuatu yang selalu aku lupakan ketika hendak mencoba Intrnt Mrktng. Dan sekarang aq menemukan jawabannya. ^_+