Backup PostgreSQL database and send backup to email on Linux |
| Written by Daniel | |
|
In this tutorial you will learn how to create a simple BASH
Script that backs up your PostgreSQL databases, creates an archive with the
date appended to the archive name and then sends this archive to your e-mail
address or. One of the most difficult task I encountered when I built this script was sending the e-mail with the attached database backup archive, which I found out needed a different mail application then sendmail. Please note that the system I am using is Fedora Core 4 but it should work on any Linux system with the proper mail service activated. Install MUTT
To be able to attach files to e-mails and then send them you
need a mail client. yum install mutt.i386
Or you could download the MUTT package from the website and manually
install. Backup and e-mail bash script
The next step is creating the bash script that will backup all the databases,
create the archive, append the date of the backup and e-mail it to a specified
address. vi /bin/backup-script.sh Note that this will only work if you have the VI editor installed, if you do not have VI you can use any editor you prefer, what you need to do is create a .sh file and type your script. To start of here is the actual script:
#!/bin/bash
Let us assume you are using VI as your default editor, after
you used vi /bin/backup-script.sh you
need to copy the script and paste it in the editor. To paste in VI you could
right click with your mouse or use Shift+Insert. :wq! and Enter. That’s it, you now have a working SHELL script that if executed will create a back-up of your PostgreSQL database and send it to your e-mail. Now let’s just have a quick look at the script so we can understand it better. The first line #!/bin/bash is just a declaration that this is a BASH script. The second, third and forth lines
BACKUP_DIR="/home/pg-bkp" are used for declaring variables the script needs to know like where to add the backups, the PostgreSQL host and username. In the next line time=`date '+%d'-'%m'` we are creating a new variable called time witch gets the current date and converts it to the day-month format. In the next line of code /usr/bin/pg_dumpall -h $PGHOST -U $PGUSER | gzip > $BACKUP_DIR/backup-$time.gz we issue the command to backup all databases pg_dumpall and create an archive of the files gzip > and append the current date to that archive name. The next line sleep 10 is just a safety measure that makes the script wait 10 seconds until it passes to the next line of code. Say for example you have a number of databases filled with lots of data, the backup and archive process could take some time. In the last line mutt -s "PostgreSQL Backup ($time)" -n -F /dev/null -a /home/pg-bkp/backup-$time.gz This e-mail address is being protected from spam bots, you need JavaScript enabled to view it < /dev/null we are using mutt to send an e-mail containing the subject “PostgreSQL Backup date” -s "PostgreSQL Backup ($time)" and attach the archive to the e-mail -a /home/pg-bkp/backup-$time.gz .
Now that we have our script ready we need to give it EXECUTE
permissions so it create the PostgreSQL backup archive, append the date and
send an e-mail using MUTT.
chmod +x/bin/backup-script.sh
Run the script every day at midnight using Crontab
Now that we have our script we need to make everything even
more automated and just sit back and relax as our PostgreSQL backup script
sends us an e-mail containing the backup archive everyday at midnight. 0 0 * * * /bin/backup-script.sh > /dev/null 2>&1 And then just save and quit using :wq! If you have any more questions or misunderstandings please feel free to comment.
written by hisyam , November 30, 2007
1. can the script automatic back-up dB end sent them? 2. if i want automaticly send database every time the database change or minimum every once in a minute, can the script be use? i'm using postgresql.
written by Daniel , January 03, 2008
Please read throughout the article, you will find answers to both your questions.
written by Daniel , February 05, 2008
Ayam, You can check the dir where you chose to store the files. BACKUP_DIR="/home/pg-bkp"
written by Mircea Moca , March 20, 2008
Wonderful script, Daniel, thanx But: line 7 says to create the archive name starting with backup- and line 9 tries to attach using an archive name starting with bkps- This makes mutt unable to attach the file, giving "/bin/backup-script.sh: line 6: /home/pg-bkp/backup-20-03.gz: No such file or directory Can't stat /home/pg-bkp/bkps-20-03.gz: No such file or directory /home/pg-bkp/bkps-20-03.gz: unable to attach file." So, I corrected bkps in line 9 with backup. It's a wonderful script and I rapidly solved my problem, thanx again!
written by Daniel , March 20, 2008
Hei Mircea, Thanks for the heads up, I correct the syntax and it should be working for everyone now. Glad we could be of use. Daniel Do you need more help? Ask now!
|
|
| Last Updated ( Thursday, 20 March 2008 ) |