Shell Scripting MySQLdump and FTP File Backup
Putting Everything Together
Ok so now that we have the three parts of the jigsaw in place in order to write our script, we can start by piecing the series of actions we need together.
I am assuming from this point forward that you have the correct first line for the shell interpreter in place and you have all the required variables initialised, so that our script can work with those and generate a result without any errors.
we will start by echoing an opening processing statement.
# echo message
echo backup script // processing ....
Navigating to the folder into which we will store our backup files locally can then follow this.
# navigate to backup dir
if
cd $pathtobackup
then
echo ... successfully navigated to Backup Dir!
else
echo ... Backup Dir NOT Mounted! Exiting Script
#exit script with error
exit 0
fi
Now we need to make the date and time stamped directory into which we will copy the files for the backup.
# echo message
echo ..... making export directory
# create directory for the current backup script
if
mkdir $foldername
then
echo .......... exporting directory successfully created
# chmod the directory
chmod 777 $foldername
fi
At this point we will go to the mysqldump application and dump out all the structure and data from our selected database for storage.
# echo message
echo ...... exporting SQL dump
if
# dump the db into a .sql file in the directory
mysqldump --user=$dbuname \
--password=$dbpwd --flush-logs \
--lock-tables $dbname > \
$pathtobackup/$foldername/$dbname.sql;
then
# echo message
echo ...... SQL dump file created successfully
else
# echo message
echo ...... mysqldump error!
exit 0
fi
The reason for using the \ symbol is to ensure that if you are putting this in into a terminal window using PICO or VI then keeping the command lines short enough to avoid the command scrolling automatically onto a new line and then causing errors when your script runs.
Having done this we will now compress the database dump file into a gzipped tar format:
# echo message
echo ...... compressing SQL dump file
# navigate itno folder
cd $foldername
# compress and gzip
tar -cf $dbname.tar $dbname.sql
if
gzip $DB.tar
then
echo .......... $dbname.tar successfully gzipped
fi
We can now remove the uncompressed SQL file which if it is large we wont necessarily bother transferring via ftp!
# echo message
echo ...... removing uncompressed SQL dump file
if
# compress and gzip
rm -f $dbname.sql
then
echo .......... $dbname.sql successfully deleted
fi
To avoid going through essentially very similar code, you should be able to mimic the above processes using CP commands instead of mysqldump etc to generate a copy of the web files into the backup folder, gzip’ing the copied folder and then removing the contents of it once this is done.
Once this has been done you can then start thinking about ftp-ing the backup files over to your remote host. This can be achieved in the following code:
# if we turned the feature on in the initialize variables
if
$ftpswitch -eq 1
then
# login to ftp assigning out put to END SCRIPT
ftp -n $ftphost <<END_SCRIPT
# specify a user
user $ftpuname
# provide pass
$ftppwd
# navigate to folder store and put files
cd $ftppath
mkdir $foldername
cd $foldername
put $dbname.tar.gz.enc
put $webfiles.tar.gz.enc
# quit the ftp
quit
fi
if you then wish to echo the output from the ftp session you can run an echo statement on the END_SCRIPT variable
echo END_SCRIPT
Now that his has been done your script is essentially finished and ready to close off and exit. To finish off we will echo one last process statement and tie up things with an exit result.
# echo message
echo ... // processing completed
# return result
exit 1
This concludes the make up of a vary basic shell script for performing an essential function.
Having stored the file in some location you then simply need to give it the correct permissions to run, and then you can Use a utility like cronnix in OSX to manage the cron jobs and assign the shell script to one on a regular basis.
To assign the permissions to your file, simply navigate to the folder where it is stored and assuming you saved the file as backup.sh enter in the terminal the following:
chmod 600 backup.sh
To run it you would then simply type:
sh backup.sh
Conclusion
Now you should be ready to roll for backups. This script could easily be used in OSX as in any other *nix variant. By way of a follow up you can look up more complex case/switch arguments and more complex pipe commands for checking actions that are being performed by the script. You may also want to learn how to create shell functions for repeating tasks in a complex script over and over without repeatedly typing the same code!
By way of a last hint and quick tip. If your host runs php and is web accessible, it would be very easy to call the script from a php web page whenever you wish in a file that looks as follows:
<?php
$ouput = shell_exec(“/path/to/backup.sh”);
echo $output;
?>
