LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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, 09:34 AM   #16
jonhewer
Member
 
Registered: Apr 2005
Location: London, UK
Distribution: Debian Lenny, Debian Etch, Ubuntu Hardy
Posts: 71

Original Poster
Rep: Reputation: 15

cool, thanks.

i have got it checking whether the file has been uploaded successfully using mls but its not really the nicest code - having to create the temporary file, read its contents, then delete it.

also the code i have so far will always run all of the ftp commands, even if the connection to the server cannot be established, and this outputs a load of errors to the terminal. ideally the code should only run 'user username password' if the connection has been established, and it should only run the rest of the ftp commands if the username and password are correct.
 
Old 08-23-2005, 09:44 AM   #17
tomj88
Member
 
Registered: Apr 2005
Location: Wolverhampton, England
Distribution: Ubuntu
Posts: 334

Rep: Reputation: 30
and 3 million codes of lines later you have the answer! I hope not anyway... sounds like it is becoming rather more complicated...
 
Old 08-23-2005, 09:53 AM   #18
jonhewer
Member
 
Registered: Apr 2005
Location: London, UK
Distribution: Debian Lenny, Debian Etch, Ubuntu Hardy
Posts: 71

Original Poster
Rep: Reputation: 15
yeh

given that this script will be used to backup data to an server on the local internal network, there shouldn't be any problems, so maybe i'll just strip out any ftp error detection and keep it simple
 
Old 08-23-2005, 09:55 AM   #19
tomj88
Member
 
Registered: Apr 2005
Location: Wolverhampton, England
Distribution: Ubuntu
Posts: 334

Rep: Reputation: 30
sounds like a good idea, but it would be pretty cool to have that error detection
 
Old 08-23-2005, 10:03 AM   #20
jonhewer
Member
 
Registered: Apr 2005
Location: London, UK
Distribution: Debian Lenny, Debian Etch, Ubuntu Hardy
Posts: 71

Original Poster
Rep: Reputation: 15
yeh

but then i suppose shell scripts are designed for simple tasks, if i required fancy error detection etc then something like python or perl would be better suited for that...
 
Old 08-23-2005, 10:07 AM   #21
tomj88
Member
 
Registered: Apr 2005
Location: Wolverhampton, England
Distribution: Ubuntu
Posts: 334

Rep: Reputation: 30
Yeah actually thats a fair point. Might look into making a python version when I have some spare time
 
Old 08-23-2005, 10:16 AM   #22
jonhewer
Member
 
Registered: Apr 2005
Location: London, UK
Distribution: Debian Lenny, Debian Etch, Ubuntu Hardy
Posts: 71

Original Poster
Rep: Reputation: 15
ditto

i'm just tweaking the script so far so the ftp sends all stdout and stderr to a log file, and there is a little bit of error handling.....will post this here shortly.
 
Old 08-23-2005, 10:17 AM   #23
tomj88
Member
 
Registered: Apr 2005
Location: Wolverhampton, England
Distribution: Ubuntu
Posts: 334

Rep: Reputation: 30
script.sh >> output.log

would that not do it? All be it very basic and not ordered
 
Old 08-23-2005, 10:34 AM   #24
jonhewer
Member
 
Registered: Apr 2005
Location: London, UK
Distribution: Debian Lenny, Debian Etch, Ubuntu Hardy
Posts: 71

Original Poster
Rep: Reputation: 15
yep that would do it but i'm adding comments to the log for the status of the mysql dump and ftp upload, so i'm doing it in my code
 
Old 08-23-2005, 10:35 AM   #25
tomj88
Member
 
Registered: Apr 2005
Location: Wolverhampton, England
Distribution: Ubuntu
Posts: 334

Rep: Reputation: 30
fair enough, good look
 
Old 08-23-2005, 10:39 AM   #26
jonhewer
Member
 
Registered: Apr 2005
Location: London, UK
Distribution: Debian Lenny, Debian Etch, Ubuntu Hardy
Posts: 71

Original Poster
Rep: Reputation: 15
Code:
#!/bin/sh

#backup directory
cd /var/backup

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

echo "Backup: $date" >> backuplog

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

#dumps database table structures and data into file backup-yyyy-mm-dd.sql
mysqldump -u username -p password dbname > 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

        echo "Uploading to FTP Server..." >> backuplog

        #log into server and upload
        ftp -n 2>>backuplog 1>>backuplog << END-OF-SESSION
        open server
        user username password
        bin
        hash
        prompt
        cd ~/backup
        put backup-$date.sql
        mls backup-$date.sql temp
        bye
END-OF-SESSION

        if [ -s temp ]
        then
                uploaded=`cat temp`

                if [ $uploaded = "backup-$date.sql" ]
                then
                        echo "Backup of '$uploaded' successful." >> backuplog
                        echo "Backup successful."
                else
                        echo "File upload failed." >> backuplog
                        echo "Backup unsuccessful.  FTP upload failed."
                fi
                rm temp
        else
                echo "Problem establishing FTP connection" >> backuplog
                echo "Backup unsuccessful.  Problem establishing FTP connection."
        fi
        #remove files beginning with backup- which are older than 7 days
        find /var/backup -name "backup-*" -mtime +7 -exec rm -v '{}' \;
        #nb - old backup files not removed from ftp server
else
        echo "MySQL dump failed." >> backuplog
        echo "MySQL dump failed."
fi

echo "" >> backuplog

exit 0
if anyone has any suggestions on ways to improve the above code i would be very grateful to hear of them
 
Old 08-23-2005, 10:46 AM   #27
jonhewer
Member
 
Registered: Apr 2005
Location: London, UK
Distribution: Debian Lenny, Debian Etch, Ubuntu Hardy
Posts: 71

Original Poster
Rep: Reputation: 15
errr, do i need to insert sleep commands between ftp commands to give them time to complete, i've seen this done in some sample scripts - can anyone explain this to me? thanks
 
Old 08-23-2005, 10:52 AM   #28
tomj88
Member
 
Registered: Apr 2005
Location: Wolverhampton, England
Distribution: Ubuntu
Posts: 334

Rep: Reputation: 30
I don't know but I'm impressed . Well done!!! May I suggest it would be easier to put things like user names into variables at the top of the script (then unset them at the end for security)?
 
Old 08-23-2005, 10:58 AM   #29
jonhewer
Member
 
Registered: Apr 2005
Location: London, UK
Distribution: Debian Lenny, Debian Etch, Ubuntu Hardy
Posts: 71

Original Poster
Rep: Reputation: 15
Quote:
Originally posted by tomj88
I don't know but I'm impressed . Well done!!!
cheers
Quote:
May I suggest it would be easier to put things like user names into variables at the top of the script (then unset them at the end for security)?
ok will do, but could you explain why this is a good idea for security? i would have thought that each variable is scope is the duration of the script?
 
Old 08-23-2005, 11:00 AM   #30
tomj88
Member
 
Registered: Apr 2005
Location: Wolverhampton, England
Distribution: Ubuntu
Posts: 334

Rep: Reputation: 30
You mean about unsetting? I don't know but I tend to do stuff like that just to make sure .
 
  


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


All times are GMT -5. The time now is 01:20 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