how to backup your server files && database

I was asked at spirula to write a simple script that will backup my server’s files ,and backup my db too ,

The files’ backup script

well as I am still a trainee and my server is v v simple they told me to write a small script that even copy ,it doesn’t matter but I used rsync to synchornise the files instead of copying over .anyways this scripts has to

1- take a daily backup of the files

2-.tar.gz it

3-keep the last 6 days only backups and delete any before

db_bkp.scr files_bkp.scr
zakishan:~# cat scripts/files_bkp.scr
#this script was writeen by usama el far []
#this script when put in corn and secheduled to run daily will backup directory with all its contents using sync instead of copying every time .keeping last 7 days’ backup files.,you just have to edit the next few lines and tell it the absolute path of both the directory which contain the files needed to be backed up and the directory which you want to backup your files to ,
#space needed is .tar.gz of your directory space*7 for storing + 1* your directory space for uncompressing and compressing
#please complete this
#the directory of the backup files
#the directory of the source files

yester=`date –date=”1 days ago” “+%d_%m_%Y”`
today=`date “+%d_%m_%Y”`

if test -f $yester.tar.gz
echo “founded yesterday’s backp and now copying it to begin sync”

tar xzvf $yester.tar.gz
cp -r $yester $today

echo “copy done”
echo “did not found previous bkps ,so i will create a new from now”
rsync -av $BKP_DIR $today
tar czvf $today.tar.gz $today

if test $? -eq 0
rm -r $today
echo “I couldn’t tar and gzip it so I left the directory without removing”

from7=`date –date=”7 days ago” “+%d_%m_%Y”`

echo “now i am searching for previos backups from more than 1 week and remove it”

#ls -l $from7.tar.gz >/dev/null 2>/dev/null

if test -f $from7.tar.gz
echo “found the backup from 1 week and removing now”
rm $from7.tar.gz
echo “i did not found backups from 7 days ago to delete”

The database backup script

Now lets go to the database part …

simply there is a single line command we can use to backup our db

which is

mysqldump –opt -Q -u [DB_USERNAME]  -p[DB_PASSWORD]  [DB_NAME] >[DBKP_FILE_NAME].sql

to restore we use



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s