Tuesday, January 18, 2011

Automated Backup Script Using tar and FTP


I am posting here a script I made to automate backup via tar ang FTP. I hope that you may find this helpful. In the next post, I will try to explain each entries on the script. Thanks, take care and God Bless!!!

#!/bin/bash
# A Simple (Poor Man) disk based backup script
# Will backup all user home directories in seperate archives
# as a single /home backup file can be quite large and unwieldly.
# Does not need to be modified if users are added/deleted

# Step - 1 Create Timestamp and set up variables and functions
# BUDTSTAMP = Backup Date/Time Stamp
BUTDSTAMP=$(date +%Y%m%d)

# variable holding directories containing files to backup eg: BACKUPTHESE="/home /root /etc"
BACKUPTHESE="/var/www/html/adldap"

# Backup base place the back up base

B_BASE="/trynga"

cd $B_BASE

mkdir $B_BASE/vol
mkdir $B_BASE/vol/backup
mkdir $B_BASE/vol/backup/daily
mkdir $B_BASE/vol/backup/yesterday
mkdir $B_BASE/vol/backup/database

#directory containing today's backup
BKUPDIR="$B_BASE/vol/backup/daily"
#directory containing yesterday's backup
YDBKUPDIR="$B_BASE/vol/backup/yesterday"
#directory containing the day before yesterday's backup
DBBKUPDIR="$B_BASE/vol/backup/daybefore"
# DATABASE BACKUP DIR
DATABASEBACKUP="$B_BASE/vol/backup/database"
NCFTP="/usr/local/bin"

# device to monitor upon backup
DEVICE="/"

# recipient to send the backup report
#EMAILADD="backupadmin"
EMAILADD="rowell@mindragon.com"

# FTP SetUP
FTPS="192.168.1.118"
FTPU="rowell"
FTPP="mdi2005!"
NOW=$(date +"%d-%m-%Y")
BACKUP="$B_BASE/vol/backup/daily"
FTPD="serbackupfiles"

### MySQL Setup ###
MUSER="root"
MPASS="mdi2009!"
MHOST="localhost"
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
GZIP="$(which gzip)"

# log files
LOG="/var/log/mail.txt"
ERRORLOG="/var/log/ERROR.txt"

# tag for backup file name
TAG="OpenSERbackup"

# function that quits and logs on error
exiterror()
{
# use this function by supplying $LINENO as first arg
echo "Fatal error caused by line ${1} of ${0}" >> $ERRORLOG
mail $EMAILADD -s "Backup Job ERROR" -v < $ERRORLOG
mail $EMAILADD -s "Backup Job Report" -v < $LOG
exit 1
}

# Step - 2 Start Email Message To Be Sent

# Remove mail message from previous backup
# I do this at the beginning of the script instead of the end
# in case the mail does not send for whatever reason or
# I need to debug it

rm $LOG > /dev/null 2>&1
echo "System Backup $BUTDSTAMP" >> $LOG

# The email sends a user friendly note showing the start and
# end time/dates. This is important so you can compare logs
# and see if a backup ran ok.

echo "Backup Began $(date)" >> $LOG

# Step - 3 Rotate Backups
# Simple three backupset rotation, keeps only last three
# Use directory /vol/backup as an example, make sure you change this path
# to fit your local settings. Have three directories in /vol/backup named daily,
# yesterday and daybefore.
# Make sure /vol/backup is on a different disk (preferably a different machine)
# than the files you are backing up

#looks specifically for backup files in case other files are kept in these directories
#for bkfile in $DBBKUPDIR/*; do
#echo $bkfile | grep $TAG >/dev/null &&
#(rm $bkfile || exiterror $LINENO )
#done
for bkfile in $YDBKUPDIR/*; do
echo $bkfile | grep $TAG >/dev/null &&
#(mv $bkfile $DBBKUPDIR/ || exiterror $LINENO )
(rm $bkfile || exiterror $LINENO )
done
for bkfile in $BKUPDIR/*; do
echo $bkfile | grep $TAG >/dev/null &&
(mv $bkfile $YDBKUPDIR/ || exiterror $LINENO )
done

# Step - 4 Archive Home Directories
# Creates a seperate tar file for each directory in the directories in $BACKUPTHESE


# change to dir to backup. Check in case cd failed for some reason.
cd $BACKUPTHIS || exiterror $LINENO
#backup all files listed in BACKUPTHESE
#for DIRTOBACKUP in $BACKUPTHESE; do
#for FOLDERNAME in $DIRTOBACKUP/*
#do
# Archives are created in the format someuser-$TAG-datetime.tar.gz
# basename is used here so as not to include absolute paths
# -p preserves permissions
#echo -e "-------------------\n>>>taring ${FOLDERNAME}\n\n" >> $LOG
#tar -czvpf ${BKUPDIR}/$( basename $FOLDERNAME )-${TAG}-${BUTDSTAMP}.tar.gz ${FOLDERNAME} >> $LOG || exiterror $LINENO
#done
#done

for FOLDERNAME in $BACKUPTHESE; do
echo -e "-------------------\n>>>taring ${FOLDERNAME}\n\n" >> $LOG
#tar -czvpf ${BKUPDIR}/$( basename $FOLDERNAME )-${TAG}-${BUTDSTAMP}.tar.gz ${FOLDERNAME} >> $LOG || exiterror $LINENO
tar -czvpf ${BKUPDIR}/$( basename $FOLDERNAME )-${TAG}-${BUTDSTAMP}.tar.gz ${FOLDERNAME} > /dev/null 2>&1 || exiterror $LINENO
done

# For MySQL backup database
echo -e "------------------\n>>>>back up the database\n\n" >> $LOG
### Start MySQL Backup ###
# Get all databases name

#mkdir $BACKUP/$NOW
mkdir $DATABASEBACKUP/$NOW

DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')"
echo "$DBS \n\n " >> $LOG
for db in $DBS
do
#FILE=$BACKUP/mysql-$db.$NOW-$(date +"%T").gz
FILE=$DATABASEBACKUP/$NOW/mysql-$db.${BUTDSTAMP}.gz
$MYSQLDUMP -u $MUSER -h $MHOST -p$MPASS $db | $GZIP -9 > $FILE
done

# Step - 5 FTP backup start

echo -e "------------------\n>>>>Transfer backups to FTP server\n\n" >> $LOG

# not yet done Do an an FTP back up
# set the settings for FTP

### Dump backup using FTP ###
#Start FTP backup using ncftp
RETVAL=`$NCFTP/ncftp -u"$FTPU" -p"$FTPP" $FTPS <<EOF
mkdir $FTPD
mkdir $FTPD/$NOW
cd $FTPD/$NOW
lcd $BACKUP
mput *
lcd $DATABASEBACKUP/$NOW
mput *
quit
EOF`

echo -e "$RETVAL \n\n" >> $LOG

##### Let us use a a different version
#mkdir $FTPD
#mkdir $FTPD/$NOW
#ncftpput -m -z -u "$FTPU" -p "$FTPP" $FTPS


# Step - 6 Finish Email Report and Send

echo "Backup ended $(date)" >> $LOG

# df -h includes a human readable disk usage report of the media that /vol/backup
# is mounted on. Good to now if your backup disk is running out of space.
# Of course /dev/hdb1 is the device I use, modify it for your local settings

df -h $DEVICE >> $LOG
mail $EMAILADD -s "Backup Job Report" -v < $LOG

# Step - 7 All Done

Saturday, August 29, 2009

Installing and setting up phpPgAdmin

In this blog, I'll discuss on how to setup phpPgAdmin. But before going any further where can phpPgAdmin be used? PhpPgAdmin is a counter part of phpMyAdmin. If phpMyAdmin is the web admin user interface managing MySQL server then phpPgAdmin is for PostgreSQL. PhpPgAdmin was created to ease the administration of PostgreSQL. Below are the requirements needed to build the admin.

Requirements:

- PHP installed
- PostgreSQL installed (for instruction on how to install postgreSQL server see http://www.designmagick.com/article/2/Starting-Out/Installing-PostgreSQL)

- if you are running CentOS distro or the like then you can just do yum to install all of these needed packages.

Note: by default the installation of Postgres creates user postgres with no password (postgres by default is the superuser for PostgreSQL).

Here are the steps to take to be able to make phpPgAdmin running:

1. Download phpPgAdmin

# cd /usr/src/
# wget http://downloads.sourceforge.net/project/phppgadmin/phpPgAdmin%20%5Bstable%5D/phpPgAdmin-4.2.2/phpPgAdmin-4.2.2.tar.gz?use_mirror=nchc

2. Extract the package

# tar xvzf phpPgAdmin-4.2.2.tar.gz

3. Rename the folder and move folder to the web server document root (assuming your document root is at /var/www/html)

# mv phpPgAdmin-4.2.2 /var/www/html/phpPgAdmin

4. You should be able to browse phpPgAdmin on browser

http://xxx.xxx.xxx.xxx/phpPgAdmin/

5. Notice that what ever you put you always get "Login Failed". To correct that edit config.inc.php (located at /var/www/html/phpPgAdmin/conf/config.inc.php), set $conf['extra_login_security'] to false for now.

from

$conf['extra_login_security'] = true;

to

$conf['extra_login_security'] = false;

6. Do also the changes as follow on config.inc.php

from

$conf['servers'][0]['host'] = '';

to

$conf['servers'][0]['host'] = 'localhost'; // this is to allow tcp login

7. Now login to phpPgAdmin using username postgres without password

8. Create a user with password. (Another admin since it is advisable to use other super user account to avoid hack).

9. Once a new admin user has been created. Logout from the phpPgAdmin.

10. Edit the postgreSQL configuration (pg_hba.conf) to set md5 authentication for all connecting client via tcp. (By default all created user are trusted even without authentication). Do the changes as follow.

# vi /var/lib/pgsql/data/pg_hba.conf

from

host all all 127.0.0.1 255.255.255.255 trust

to

host all postgres 127.0.0.1 255.255.255.255 trust
host all all 127.0.0.1 255.255.255.255 md5

save the file and restart postgresql

# service postgresql restart

11. Edit again the config.inc.php and do the changes as follow:

# vi /var/www/html/conf/config.inc.php

from

$conf['extra_login_security'] = false;

to

$conf['extra_login_security'] = true;

12. Now you should be able to login using the new admin account created with password. Postgres super user will always be denied to login on phpPgAdmin for security reason.

All should be working fine from this point. You now have a running phpPgAdmin. I hope this helps for those who are establishing their PostgreSQL web admin. Cheers, Take care and God Bless!!!!

Tuesday, August 18, 2009

Doing FTP in PHP

I had an issue during a development of one of my projects. One of the modules deals with FTP'ing files from one server to another. My script was able to login with no issues but I wondered why it was not able to write or ftp_put anything. I googled for few hours finding out how to overcome this issue that I have and came over with these links...

http://www.php.net/manual/en/function.ftp-pasv.php
http://us.php.net/manual/en/function.ftp-put.php#73168

Both of these links tell that you have to use ftp_pasv to set the ftp connection in passive mode. Meaning the ftp connection will be initiated from the client instead from the server. Also take note that ftp_pasv should be done immediately after calling the ftp_login.

Here is the comment I read there which gave or shed me the light for succesfully getting around with my problem.

"If you are having timeouts uploading a file, even very small files, you might have a look at ftp_pasv()

And don't forget to do it after your ftp_login();"

The sample code below shows how to do it.

$file = 'somefile.txt';
$remote_file = 'readme.txt';

// set up basic connection
$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

// turn passive mode on
ftp_pasv($conn_id, true);

// upload a file
if (ftp_put($conn_id, $remote_file, $file, FTP_ASCII)) {
echo
"successfully uploaded $file\n";
} else {
echo
"There was a problem while uploading $file\n";
}

// close the connection
ftp_close($conn_id);
?>

I hope this experience that I had and I shared with you will help you out fixing issues doing FTP in PHP. Thanks, Cheers and God Bless!!!!

Tuesday, August 4, 2009

Silky Heart

Every hour of the day
Whenever I think about you
This impatient feelings
Just overflow

It would be easier if I just tell you that I love you
But when you appear in front of me
my personality stands up and interferes

I can usually pull through with my assertiveness
That's just the way I am
But no matter how hard I try, I can't break the wall that's between us

I want you to realize my feelings
That's why I always gaze at you with the message of...
"I love you"
With all of my courage!

My heart made of silk feels like it's about to rip apart
After meeting you, I finally realized
That I'm really clumsy when it comes to love
My scars from love that I've almost forgotten
Have started to ache all of a sudden
someday, like how I normally would...
I must tell you that I love you, or
else I'll become even weaker

If I say ‘I love you’, then I can be at ease
The truth is, even though I know that
When I sound it out, you seem to leave me

I wonder, what do you think
Of me?
At our current distance we’re merely friends, right?

I just might be whitewashing it
But I don’t want to get hurt, it’s just that alone…
Or so I persuaded myself
And I just ran away

If my flimsy silky heart
Gets hurt this time, then I’m sure
I won’t be able to love anyone ever again
The splitting sound that echoes in my chest
Is at the bottom of my memories, I’m sure
The sewing kit that I forgot to put away
On that day should be around somewhere…

Monday, June 22, 2009

I’m going to meet you now

Within the people I simply brush by
I wanted to find more than anyone
The eyes welled up with tears, that voice
I only want to hold you
If I miss you and call out to you my dear
Wherever I may be, it is for you

I’m calling your name again
Because I’m like this
Because your are the one I love
I’m going to see you again
A love deeper than my tears

The lamp on the street where we
First spoke shines a light on my heart
Evething that catches my eyes,everything
Are memories that remind me of you
If I tell you that I’ve gotten too really like you
Will you feel unsettled? I wonder

I’m calling your name again
Because I’m like this
Because you are the one I love
I’m going to see you again
A love deeper than my tears

With only you
No matter how many things
Even if I’m reborn
Will you feel insettled? I wonder

I love you more than anyone
Inside my heart,
Because you live in my heart
I’m going to meet you now
Bringing along the promise of forever
I’m so happy to be loving you

Friday, June 12, 2009

Hikbi ng Pusong Umaasa

alam kong mahirap pero kailangang gawin
di ko alam kung matatagalan ko at mapaninindigan ang nais ko
ang lumayo sa kanya at limutin na ng tuluyan ang nararamdaman ko
marahil ito yung tamang paraan, marahil ito ang solusyon

marahil panahon na para limutin ang lahat
ang mga panahon na nangarap akong maging kami kahit minsan
dahil alam ko naman ang katotohanang di magkakatotoo ang panaginip ko
ang panaginip na minsan ay nagbigay ng kahit kunting ngiti sa puso ko

sadya sigurong ganito pero sa katagalan makakalimutan ko rin sya
kahit mahirap kakayanin dahil kailangan
kailangan na akong magising sa katotothan at harapin ang realidad ng buhay

Pero di pa rin titigil ang pusong ito na umasa kahit pasaglit saglit lang
Pero pagod na rin ang pusong ito, pagod na itong isipan ko sa mga ilusyon
Pagod na rin ako sa kakaasa .....

Thursday, June 11, 2009

Kainis lang

Naiinis ako di ko alam kung ano ba dapat kong gawin
Masyado na ba akong naiinip sa pagdating nya
Masyado na ba akong matagal naghihintay
Di ko alam kung bulag ba ako manhid o kung anuman
baka naman nasa paligid lang sya pero ayokong tingnan
baka naman matagal na pala syang nasa paligid
Pero nagbubulag bulagan lang ako
Masyado lang ba akong takot na makita ko sya
O masyado lang akong nadala sa nangyari sa nakaraan
pero ayoko mawalan ng pag asa sa buhay
ayokong sayangin ung natitira kong pag asa na makita sya
ganito ba ako kabulag at di ko makita kung nasan sya
di ko alam nandito na ba sya sa paligid ko
O kailangan ko po syang hanapin
Wla akong clue na nakikita eh
O tanga lang ako na obvious ng nasa paligid
Pero nag mamaang ma angan lang ako
ewan ko ba ano ba dapat kong gawin
napaka gulo di ko maintindihan
palibhasa di ko pa nararanasan ung magmahal at mahalin ka
ewan ko ang gulo ano
kainis kasi napaka torpe ko
tapos parang wala pa kong ganon katatag na confidence sa sarili
baka kasi ung hinahanap ng mga babae sa paligid ko
ung mga alam mo na ung mga guwapo ung mga pang matinee idol baga
ayoko naman na tumandang binata
nakakatakot din, pero eto naman akong engot
wla namang magawang aksyon kung anong gagawin
kakainis lang ng konti pero minsan
ayoko ng paniwalaan pa ung nararamdaman ko
baka makuryente na nman ako
tapos haun uuwi na naman luhaan
kainis kung pwde lang sana lagi sure ball
pero hindi rin eh