LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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, 10:53 AM   #1
your_shadow03
Senior Member
 
Registered: Jun 2008
Location: Germany
Distribution: Slackware
Posts: 1,466
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 10:54 AM.
 
Old 02-19-2014, 10:56 AM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
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 11:02 AM.
 
1 members found this post helpful.
Old 02-19-2014, 10:57 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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, 11:23 AM   #4
your_shadow03
Senior Member
 
Registered: Jun 2008
Location: Germany
Distribution: Slackware
Posts: 1,466

Original Poster
Blog Entries: 6

Rep: Reputation: 51
colucix & sch,
Thanks. It works great.
 
Old 02-19-2014, 11:32 AM   #5
your_shadow03
Senior Member
 
Registered: Jun 2008
Location: Germany
Distribution: Slackware
Posts: 1,466

Original Poster
Blog Entries: 6

Rep: Reputation: 51
Any idea how to add email notification if the backup is successfully performed?
 
Old 02-19-2014, 11:52 AM   #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, 11:56 AM   #7
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
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 11:58 AM.
 
Old 02-19-2014, 12:04 PM   #8
your_shadow03
Senior Member
 
Registered: Jun 2008
Location: Germany
Distribution: Slackware
Posts: 1,466

Original Poster
Blog Entries: 6

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

Original Poster
Blog Entries: 6

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, 12:20 PM   #10
your_shadow03
Senior Member
 
Registered: Jun 2008
Location: Germany
Distribution: Slackware
Posts: 1,466

Original Poster
Blog Entries: 6

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, 12:56 PM   #11
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
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, 03: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



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 09: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 08:52 AM
Suggestion for deploying a data backup server karthi26 Linux - Server 2 08-03-2009 12:52 PM
Suggestion for Backup? jerryk Linux - Newbie 1 12-23-2006 03:57 PM
Need suggestion on biweekly backup job cojo Linux - Software 12 11-06-2006 08:17 AM

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

All times are GMT -5. The time now is 02:39 PM.

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
Open Source Consulting | Domain Registration