LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices

Reply
 
Search this Thread
Old 10-12-2011, 08:17 AM   #1
ckiran04
LQ Newbie
 
Registered: Apr 2008
Posts: 21

Rep: Reputation: 0
database backup script


When i run this script the old files are replacing with the latest one, but i need it be created in new folder with current date and time stamp.

### MySQL Server Login Info ###
MUSER="user"
MPASS='#####'
MHOST="localhost"
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
BAK="/home/foldername
GZIP="$(which gzip)"
NOW=$(date +"%d-%m-%Y")



### TAKING DATABASE BACKUP ###
[ ! -d $BAK ] && mkdir -p $BAK || /bin/rm -f $BAK/*

#DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')" #This line is to backup all databases.
DBS=("DBname") #To Backup only specified databases use this line.
for db in ${DBS[@]}
do

FILE=$BAK/$db.$NOW-$(date +"%T").gz ##NOTE: To save some disk space the sql dump files are compressed using gzip;
$MYSQLDUMP -u $MUSER -h $MHOST -p$MPASS $db | $GZIP -9 > $FILE ## You can uncompress them using the command "gzip -d <FILENAME>"
done

till here i am getting back up correctly. i am confused here at . i need this DB to stored in filesystem in a specific folder /DBback . and i need script to place in crontab.
Please give me some solution.
 
Old 10-12-2011, 08:51 AM   #2
lithos
Senior Member
 
Registered: Jan 2010
Location: SI : 45.9531, 15.4894
Distribution: CentOS, OpenNA/Trustix, testing desktop openSuse 12.1 /Cinnamon/KDE4.8
Posts: 1,144

Rep: Reputation: 217Reputation: 217Reputation: 217
Why don't you make a new subdir in the
BAK="/home/foldername

with date and time stamp ?

modify this:
[ ! -d $BAK ] && mkdir -p $BAK || /bin/rm -f $BAK/*

because it's always using the same path BAK="/home/foldername and it removes it and creates again (as far as I understand your script.

You are posting only crippled script lines so post the whole script and use [code] wraps around it to make it readable for someone who will help you.

like
Code:
this
here 
is my 
script
 
Old 10-14-2011, 02:06 AM   #3
ckiran04
LQ Newbie
 
Registered: Apr 2008
Posts: 21

Original Poster
Rep: Reputation: 0
Actually this is completed code. next lines in this script are copying the Db's to FTP server
once gain i am sending the complete the script for reference.
### MySQL Server Login Info ###
MUSER="user"
MPASS='#####'
MHOST="localhost"
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
BAK="/home/foldername
GZIP="$(which gzip)"
NOW=$(date +"%d-%m-%Y")

### FTP SERVER Login info ###
FTPU="FTPuser"
FTPP="passwd"
FTPS="hostname"



### TAKING DATABASE BACKUP ###
[ ! -d $BAK ] && mkdir -p $BAK || /bin/rm -f $BAK/*

#DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')" #This line is to backup all databases.
DBS=("DBname") #To Backup only specified databases use this line.
for db in ${DBS[@]}
do

FILE=$BAK/$db.$NOW-$(date +"%T").gz ##NOTE: To save some disk space the sql dump files are compressed using gzip;
$MYSQLDUMP -u $MUSER -h $MHOST -p$MPASS $db | $GZIP -9 > $FILE ## You can uncompress them using the command "gzip -d <FILENAME>"
done


ncftp -u "$FTPU" -p "$FTPP" $FTPS<<EOF
mkdir $NOW
cd $NOW
lcd $BAK
mput *
quit
EOF


In this code . please guide in which line do i need to add the code to create the folder with time stamp.
I just need to take back up and save the files in same server not in the remote server.
Please give me some solution.
 
Old 10-15-2011, 01:08 PM   #4
lithos
Senior Member
 
Registered: Jan 2010
Location: SI : 45.9531, 15.4894
Distribution: CentOS, OpenNA/Trustix, testing desktop openSuse 12.1 /Cinnamon/KDE4.8
Posts: 1,144

Rep: Reputation: 217Reputation: 217Reputation: 217
Hello

finally I found time to reply to you.

Replace with this
Code:
[ ! -d $BAK ] && mkdir -p $BAK 
instead of "[ ! -d $BAK ] && mkdir -p $BAK || /bin/rm -f $BAK/* "

and you will have multiple files with date&time stamp in your "BAK="/home/foldername" directory
and don't forget to close " at BAK="/home/foldername" (<-- like this) as your code is missing closing quote "

If you don't want to FTP the files then comment out:
Code:
# ncftp -u "$FTPU" -p "$FTPP" $FTPS<<EOF
# mkdir $NOW
# cd $NOW
# lcd $BAK
# mput *
# quit
# EOF

Last edited by lithos; 10-15-2011 at 01:10 PM.
 
Old 10-17-2011, 05:20 AM   #5
ckiran04
LQ Newbie
 
Registered: Apr 2008
Posts: 21

Original Poster
Rep: Reputation: 0
thanks Lithos
 
Old 10-17-2011, 06:50 AM   #6
lithos
Senior Member
 
Registered: Jan 2010
Location: SI : 45.9531, 15.4894
Distribution: CentOS, OpenNA/Trustix, testing desktop openSuse 12.1 /Cinnamon/KDE4.8
Posts: 1,144

Rep: Reputation: 217Reputation: 217Reputation: 217
You're welcome. I hope it's what you wanted if it works for you.
 
Old 10-18-2011, 12:08 AM   #7
ckiran04
LQ Newbie
 
Registered: Apr 2008
Posts: 21

Original Poster
Rep: Reputation: 0
yes working .
 
Old 10-18-2011, 01:31 AM   #8
lithos
Senior Member
 
Registered: Jan 2010
Location: SI : 45.9531, 15.4894
Distribution: CentOS, OpenNA/Trustix, testing desktop openSuse 12.1 /Cinnamon/KDE4.8
Posts: 1,144

Rep: Reputation: 217Reputation: 217Reputation: 217
Can you use the Thread Tools to mark it SOLVED if so (read my signature)?
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Script to Backup MySQL Database on Server Automatically ? Hammad101 Linux - Server 7 12-23-2008 12:52 PM
how to create backup MYSQL Script to backup my database for every 1hour RMLinux Linux - Newbie 3 11-20-2008 10:13 AM
Bash script for database backup #cousin# Programming 8 08-29-2008 06:00 AM
backup script for database maooah Linux - Enterprise 9 06-15-2008 09:58 AM
Need script to dump database, ftp file to backup server glock19 Linux - Networking 17 05-12-2003 05:56 PM


All times are GMT -5. The time now is 03:51 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration