Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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
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...
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.
#!/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
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
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)?
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?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.