LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training 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 08-23-2005, 11:03 AM   #31
jonhewer
Member
 
Registered: Apr 2005
Location: London, UK
Distribution: Debian Lenny, Debian Etch, Ubuntu Hardy
Posts: 71

Original Poster
Rep: Reputation: 15

ok thanks
 
Old 08-23-2005, 11:12 AM   #32
tomj88
Member
 
Registered: Apr 2005
Location: Wolverhampton, England
Distribution: Ubuntu
Posts: 334

Rep: Reputation: 30
Ok setting up a cron task for it... if I wanted it to run a 21:00 everyday, would it be:
00 21 * * * root /root/mysqlbackup.sh
I can't remember whether the clock is in 24 hour format...
 
Old 08-23-2005, 11:20 AM   #33
tomj88
Member
 
Registered: Apr 2005
Location: Wolverhampton, England
Distribution: Ubuntu
Posts: 334

Rep: Reputation: 30
I modified the code slightly so it doesn't use ftp (because I don't have a ftp server to dump the data to), and added variables that are slightly easier to change, except it is now asking me everytime for a password even though I have added a variable for password. See if you can figure it out (I'm sure it's a noobish error). Here you go anyway:

Code:
#!/bin/sh

#backup directory
cd /var/backup

#date in format yyyy-mm-dd
date=`date -I`


mysqluname="your user"
mysqlpass="your pass"
mysqldb="your database"


echo "Backup: $date" >> backuplog

echo "Dumping MySQL data..." >> backuplog

#dumps database table structures and data into file backup-yyyy-mm-dd.sql
mysqldump -u $mysqluname -p $mysqlpass $mysqldb > backup-$date.sql 2>>backuplog

#check if this file exists and is not empty, as an with mysqldump in the command above
# will result in creating an empty file
if [ -s backup-$date.sql ]
then
	echo "MySQL dump successful." >> backuplog
	#remove files beginning with backup- which are older than 7 days
	find /var/backup -name "backup-*" -mtime +7 -exec rm -v '{}' \;
else
	echo "MySQL dump failed." >> backuplog
	echo "MySQL dump failed."
fi

echo "" >> backuplog

exit 0

Last edited by tomj88; 08-23-2005 at 11:22 AM.
 
Old 08-23-2005, 11:20 AM   #34
jonhewer
Member
 
Registered: Apr 2005
Location: London, UK
Distribution: Debian Lenny, Debian Etch, Ubuntu Hardy
Posts: 71

Original Poster
Rep: Reputation: 15
thanks, i'll test that out 2moro

now to modify my code to make it email the backup status to someone.....
 
Old 08-23-2005, 11:28 AM   #35
jonhewer
Member
 
Registered: Apr 2005
Location: London, UK
Distribution: Debian Lenny, Debian Etch, Ubuntu Hardy
Posts: 71

Original Poster
Rep: Reputation: 15
how would i use a variable to build up the log message, appending data at different points? i can't think how to do this..

then i can write the variable contents to the log file in one go, and also email it to someone

thanks
 
Old 08-23-2005, 11:28 AM   #36
tomj88
Member
 
Registered: Apr 2005
Location: Wolverhampton, England
Distribution: Ubuntu
Posts: 334

Rep: Reputation: 30
I haven't got a clue (new to shell scripting).

For the email I suppose you could just use sendmail, but I don't really know how to use this I will have a quick look, but I'm guessing all that needs to be done is a simple message (backuplog?) and an attachment (the .sql file). Maybe try compressing the sql file? Would be useful on large databases...

Last edited by tomj88; 08-23-2005 at 11:29 AM.
 
Old 08-23-2005, 11:31 AM   #37
jonhewer
Member
 
Registered: Apr 2005
Location: London, UK
Distribution: Debian Lenny, Debian Etch, Ubuntu Hardy
Posts: 71

Original Poster
Rep: Reputation: 15
ok no worries, i'll google it

thanks for the suggestions.
 
Old 08-23-2005, 11:33 AM   #38
tomj88
Member
 
Registered: Apr 2005
Location: Wolverhampton, England
Distribution: Ubuntu
Posts: 334

Rep: Reputation: 30
sendmail your@email.com < /var/backup/backup-$date.sql

Will that do for the emailing the backup to someone?
 
Old 08-23-2005, 11:35 AM   #39
jonhewer
Member
 
Registered: Apr 2005
Location: London, UK
Distribution: Debian Lenny, Debian Etch, Ubuntu Hardy
Posts: 71

Original Poster
Rep: Reputation: 15
i'll try that out later (i'm off home now) thanks again for your help - i'll get back to you with any further improvements i make
 
Old 08-24-2005, 05:50 AM   #40
jonhewer
Member
 
Registered: Apr 2005
Location: London, UK
Distribution: Debian Lenny, Debian Etch, Ubuntu Hardy
Posts: 71

Original Poster
Rep: Reputation: 15
i decided to use mail instead of sendmail because you can specify the email subject using -s flag, with sendmail u'd need to actually include teh necessary headers in the email

i now have two log files, one is the entire log, and the other is the most recent log entry - this file is appended to the entire log and emailed as the message body. while having a second log like this is a little messy, its the only way i can think of doing this
 
Old 08-24-2005, 07:31 AM   #41
fotoguy
Senior Member
 
Registered: Mar 2003
Location: Brisbane Queensland Australia
Distribution: KirraMail Live Email Server
Posts: 1,285

Rep: Reputation: 61
I too have something similar and need some help with. I making a small script for backing up to a directory and burning to a cdrom and I'd like it to keep the backups around for seven days before deleting, who would i use in my script:

find /var/backup -name "backup-*" -mtime +7 -exec rm -v '{}' \;

I'm a noob when it comes to programming here's my script if someone can help

#!/bin/bash
#
Backup_Dirs="/tempinst" # eg. /etc /usr/local /opt /var /root /boot
Backup_Dest_Dir=/tmp/backup
Backup_Date=`date +%b%d%Y`
Speed=8 # Use best speed for CD-R/RW disks on YOUR system

# Check to see of backup directory exists, if not then create it

if [ ! -d $Backup_Dest_Dir ]; then
{
mkdir $Backup_Dest_Dir;
}
else
{
echo "Backup Directory already exists";
}
fi

# Create tar file with todays Month Day Year prepended for easy identification

tar -cvzf $Backup_Dest_Dir/$Backup_Date.tar.gz $Backup_Dirs > $Backup_Dest_Dir/backup_$Backup_Date.log
echo "Create $Backup_Date.img including log file"

# Create a image that can be written to writeable media

mkisofs -r -o $Backup_Dest_Dir/$Backup_Date.img $Backup_Dest_Dir/$Backup_Date.tar.gz $Backup_Dest_Dir/backup_$Backup_Date.log

# Check size of directory to burn in MB

Size=`du -m $Backup_Dest_Dir/$Backup_Date.img | cut -c 1-3`
if [ $Size -lt 680 ];then
{
echo "Size of $Backup_Date.img is $Size MB, OK to Burn";
}
else
{
echo "Size of $Backup_Date.img too Large to burn too CD-R....Maybe time for DvD Burner";
exit1
}
fi

# Burn the CD-RW

echo "Burning back-up to disc."
cdrecord dev=ATAPI:0,1,0 -v -eject fs=64M driveropts=burnproof speed=$Speed -tao $Backup_Dest_Dir/$Backup_Date.img
echo "Successfully backed-up $Backup_Date.img to cd-r/rw"

exit 0
 
Old 08-24-2005, 08:13 AM   #42
jonhewer
Member
 
Registered: Apr 2005
Location: London, UK
Distribution: Debian Lenny, Debian Etch, Ubuntu Hardy
Posts: 71

Original Poster
Rep: Reputation: 15
Code:
find /var/backup -name "backup-*" -mtime +7 -exec rm -v '{}' \;
find is a tool used to search for files - i will explain what the above command does:

/var/backup is the base directory to search in

-name "backup-*" means i want all files beginning with backup-

-mtime +7 matches files last modified more than 7 days ago

-exec rm-v '{}' \; deletes the files, -v for verbose

this is all explained in the manual

HTH

Last edited by jonhewer; 08-24-2005 at 08:24 AM.
 
Old 08-24-2005, 10:32 AM   #43
tomj88
Member
 
Registered: Apr 2005
Location: Wolverhampton, England
Distribution: Ubuntu
Posts: 334

Rep: Reputation: 30
Would you be interested in writing something similar to this in python? I have never wrote anything in it before and would like to learn, so it seems like a nice project to start on. How good are you at writing in python Jon? And thanks for the tip about using mail! Just changed the sendmail line to mail -s "mysql backup" tom@tomj.sytes.net < /var/backup/backup-$date.sql and it works perfectly!
 
Old 08-24-2005, 10:43 AM   #44
jonhewer
Member
 
Registered: Apr 2005
Location: London, UK
Distribution: Debian Lenny, Debian Etch, Ubuntu Hardy
Posts: 71

Original Poster
Rep: Reputation: 15
i've only recently started learning python - i started writing a very basic rss reader which grabs the rss file, downloads it, parses it and outputs some of the data, but i've got a bit bored of that now

i may well write a similar script in python, depends really if i am set any more work for my last week at my internship i'm currently doing (and for which i am writing this script to backup the database backend of my project), or if i have some free time.

i have a python network programming book at home which covers ftp, and i have some knowledge already of reading and writing to files.
 
Old 08-24-2005, 10:53 AM   #45
tomj88
Member
 
Registered: Apr 2005
Location: Wolverhampton, England
Distribution: Ubuntu
Posts: 334

Rep: Reputation: 30
I've still got 3 weeks left before I go back to school hehe... starting my computing course in 6th form then, and the school has just purchased visual basic .net .... but I believe we can use c, c++ and other languages, let's hope that I can use something other than vb.net haha
 
  


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
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 03:14 AM
shell interface vs shell scripting? I'm confused jcchenz Linux - Software 1 10-26-2005 04:32 PM
bash scripting --- some help needed rajsharma Linux - Software 1 09-09-2005 03:49 AM
Scripting help needed. stonelee Linux - Software 2 09-29-2003 10:47 AM
Shell scripting and background processes - help needed. trafalgar Programming 3 06-08-2003 10:15 AM

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

All times are GMT -5. The time now is 03:56 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration