LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-19-2014, 11:53 AM   #1
your_shadow03
Senior Member
 
Registered: Jun 2008
Location: Germany
Distribution: Slackware
Posts: 1,462
Blog Entries: 6

Rep: Reputation: 51
Need suggestion for a backup script..


I have written a backup script for Mysql database as shown below:

Code:
#!/bin/bash
# Robert Feb-14
# backup each mysql db into a different file, rather than one big file
# as with --all-databases - will make restores easier

USER1="user1"
USER2="user2"
USER3="user3"
PASSWORD1=""
PASSWORD2=""
PASSWORD3=""
db1="database1"
db2="database2"
db3="database3"
OUTPUTDIR="/var/lib/psa/dumps/domains/logic.tv/custom-database                                                                                       e-backup"
MYSQLDUMP="/usr/bin/mysqldump"
MYSQL="/usr/bin/mysql"

# clean up any old backups - save space
#rm "$OUTPUTDIR/*bak" > /dev/null 2>&1

# get a list of databases
#databases=`$MYSQL --user=$USER1 --password=$PASSWORD1 \
# -e "SHOW DATABASES;" | tr -d "| " | grep -v Database`
#
# dump each database in turn
#for db in $databases; do
#    echo $db
$MYSQLDUMP --force --opt --user=$USER1 --password=$PASSWORD1 \
    --databases $db1 > "$OUTPUTDIR/$db1.bak"

$MYSQLDUMP --force --opt --user=$USER2 --password=$PASSWORD2 \
    --databases $db2 > "$OUTPUTDIR/$db2.bak"
$MYSQLDUMP --force --opt --user=$USER3 --password=$PASSWORD3 \
    --databases $db3 > "$OUTPUTDIR/$db3.bak"
The above code is working fine and dumping it to the required folder.All I need is adding date and time to the filename and sending mail notification in case the database backup is successfully performed. Can anyone suggest?

Last edited by your_shadow03; 02-19-2014 at 11:54 AM.
 
Old 02-19-2014, 11:56 AM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,167

Rep: Reputation: 889Reputation: 889Reputation: 889Reputation: 889Reputation: 889Reputation: 889Reputation: 889
man date. e.g.:
Code:
$MYSQLDUMP --force --opt --user=$USER1 --password=$PASSWORD1 \
    --databases $db1 > "$OUTPUTDIR/$db1`date +%Y-%j-%s`.bak"

Last edited by schneidz; 02-19-2014 at 12:02 PM.
 
1 members found this post helpful.
Old 02-19-2014, 11:57 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Use command substitution inside the filename, e.g.
Code:
"$OUTPUTDIR/$db1.$(date +%F).bak"
produces
Code:
/var/lib/psa/dumps/domains/logic.tv/custom-database/database1.2014-02-19.bak
 
1 members found this post helpful.
Old 02-19-2014, 12:23 PM   #4
your_shadow03
Senior Member
 
Registered: Jun 2008
Location: Germany
Distribution: Slackware
Posts: 1,462
Blog Entries: 6

Original Poster
Rep: Reputation: 51
colucix & sch,
Thanks. It works great.
 
Old 02-19-2014, 12:32 PM   #5
your_shadow03
Senior Member
 
Registered: Jun 2008
Location: Germany
Distribution: Slackware
Posts: 1,462
Blog Entries: 6

Original Poster
Rep: Reputation: 51
Any idea how to add email notification if the backup is successfully performed?
 
Old 02-19-2014, 12:52 PM   #6
sjreilly
Member
 
Registered: Oct 2007
Location: Edinburgh, Scotland
Distribution: Debian, CentOS, RHEL, Mint, Ubuntu
Posts: 79

Rep: Reputation: 15
Why not just use automysqlbackup?
It does email notification as well as daily, weekly and monthly backups.
 
Old 02-19-2014, 12:56 PM   #7
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,167

Rep: Reputation: 889Reputation: 889Reputation: 889Reputation: 889Reputation: 889Reputation: 889Reputation: 889
Quote:
Originally Posted by your_shadow03 View Post
Any idea how to add email notification if the backup is successfully performed?
man mail
using an if statement you can check the result of the previous command ($!).

Last edited by schneidz; 02-19-2014 at 12:58 PM.
 
Old 02-19-2014, 01:04 PM   #8
your_shadow03
Senior Member
 
Registered: Jun 2008
Location: Germany
Distribution: Slackware
Posts: 1,462
Blog Entries: 6

Original Poster
Rep: Reputation: 51
I dont think automysqlbackup takes care of individual database backup. Does it?
 
Old 02-19-2014, 01:06 PM   #9
your_shadow03
Senior Member
 
Registered: Jun 2008
Location: Germany
Distribution: Slackware
Posts: 1,462
Blog Entries: 6

Original Poster
Rep: Reputation: 51
Sch,
I implemented it through echo "Backup Performed" | mail -s "Subject" <myemailID>
It works but mail comes from root@<hostname>
How shall I make it come from different name other than root. Like administrator@<hostname>
 
Old 02-19-2014, 01:20 PM   #10
your_shadow03
Senior Member
 
Registered: Jun 2008
Location: Germany
Distribution: Slackware
Posts: 1,462
Blog Entries: 6

Original Poster
Rep: Reputation: 51
sjrelly,

I read through the script. It does take daily backup. I am trying to see which option to enable for daily backup.
 
Old 02-19-2014, 01:56 PM   #11
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,167

Rep: Reputation: 889Reputation: 889Reputation: 889Reputation: 889Reputation: 889Reputation: 889Reputation: 889
Quote:
Originally Posted by your_shadow03 View Post
Sch,
I implemented it through echo "Backup Performed" | mail -s "Subject" <myemailID>
It works but mail comes from root@<hostname>
How shall I make it come from different name other than root. Like administrator@<hostname>
run command as another user:
Code:
su - schneidz -c "ls -l /home/schneidz"
 
Old 02-20-2014, 04:07 AM   #12
sjreilly
Member
 
Registered: Oct 2007
Location: Edinburgh, Scotland
Distribution: Debian, CentOS, RHEL, Mint, Ubuntu
Posts: 79

Rep: Reputation: 15
Yes, you can select which databases to back up (or all) with the DBNAMES variable
 
  


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
[SOLVED] Suggestion needed for Network backup Server distro selection. robinselvam Linux - Newbie 1 01-06-2014 10:40 AM
Newbie trying to write a simple backup script to backup a single folder Nd for school stryker759a Linux - Newbie 2 09-16-2009 09:52 AM
Suggestion for deploying a data backup server karthi26 Linux - Server 2 08-03-2009 01:52 PM
Suggestion for Backup? jerryk Linux - Newbie 1 12-23-2006 04:57 PM
Need suggestion on biweekly backup job cojo Linux - Software 12 11-06-2006 09:17 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:40 AM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration