Jump to content

Shell Server Backup Script to remote location

- - - - -

  • Please log in to reply
No replies to this topic

#1
DEViANT

DEViANT

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 358 posts
This is a little shell script I wrote a while back to backup some of our important server data to a remote location. Currently It backs up our /www directory, all our emails, our sql database and some config files. The script delivers the backups in a <!-[DATE]->.tar.gz format. It also sends and sms to predefined cellphone numbers once the backup has been completed. This will only work in south africa though, unless you change the variables to match sms servers in your own country. You'd have to modify the script a bit to work for your own scenario, but Im sure it can be helpfull. I've marked the fields that needs to be changed with the following markup : <FIELD DESC>


#!/bin/sh

##### Backup www, sql & mail ##### 15/04/2010 #### Bennie Coetzer ####


##############################################################################

##########################

#Declare all the variables

##########################

##########################

#Initial Backup Variables

##########################

folder=`date "+%Y/%Y-%m-%d"`

yr=`date "+%Y"`

wpath=/usr/local/www

wtpath=/usr/home/backup/$folder/www.tar.gz

squsern=<INSERT SQL USERNAME>

sqpassw=<INSERT SQL PASSWORD>

dbp=/home/backup/$folder/mysql.dmp

mlpath=/usr/home/vmail

mtpath=/usr/home/backup/$folder/vmail.tar.gz

mispath=/usr/local/etc

mistpath=/usr/home/backup/$folder/etc.tar.gz

getpath=/usr/home/backup/getmail

targetpath=/usr/home/backup/$folder/getmail.tar.gz

##########################

#Samba Backup Variables

##########################

samuser=<SAMBA USERNAME>

sampath=<SAMBA FOLDER NAME>

samhost=<SAMBA HOST NAME>

mpath=/usr/home/backup/mountme

bname=mailserver

sycname=/usr/home/backup/mountme/mailserver/`date -v-8d "+%Y-%m-%d"`

##########################

#WinSMS Backup Variables

##########################

smsuser=<SMS USERNAME>

smspass=<SMS PASSWORD>

smsmsg="Server%20backup%20has%20been%20completed%20succesfully"

smsnumbers=<NUMBERS TO SMS>


##########################

# Start Initial Script

##########################

mkdir -p /usr/home/backup/$folder

echo $folder Created succesfully

##########################

#Backup www,sql,vmail & etc

##########################

echo Backing up www directory...

/usr/bin/tar -czf $wtpath $wpath

echo Backing up SQL databases...

/usr/local/bin/mysqldump -u $squsern -p$sqpassw --all-databases > $dbp

echo Backing up E-mails....

/usr/bin/tar -czf $mtpath $mlpath

#zip -r $mtpath $mlpath

echo Backing up etc folder...

/usr/bin/tar -czf $mistpath $mispath

echo Backing up Getmail folder...

/usr/bin/tar -czf $targetpath $getpath

##########################

#Mount, Copy, Unmount, RM

##########################

echo Moving files from server to backup directory...

/usr/sbin/mount_smbfs -N //$samuser@$samhost/$sampath $mpath/

/bin/cp -rv /usr/home/backup/$folder $mpath/$bname

echo Deleting obsolete files...

/bin/rm -rf /usr/home/backup/$folder

/bin/rm -rf /usr/home/backup/$yr

/bin/rm -rf $sycname

/sbin/umount $mpath

/usr/local/bin/tree /usr/home/backup

echo If there were no errors, congradulations! Backup complete

##########################

#WinSMS Script

##########################

curl "http://www.winsms.co.za/api/batchmessage.asp?User=$smsuser&Password=$smspass&Delivery=No&Message=$smsmsg&Numbers=$smsnumbers;"

echo SMS Report sent to $smsnumbers

#########################

#############################################################################


Happy Coding :)

Edited by DEViANT, 16 October 2010 - 11:53 AM.

:D You should rep+ me so that I can win :D

My Blog | Ask me!
Error : Satan did it




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users