Shell Scripting MySQLdump and FTP File Backup
Creating The Script
Okay! so now we understand a little more about handling variables we will proceed by initialising a series of variables that our script will need to know in order to operate successfully.
The beginning of the script should therefore look like something as follows:
#! /bin/sh
# path for backups
pathtobackup = "/Volumes/Backups";
# path to files being backed up
pathtofiles = "/Library/WebServer/Documents";
# name for backup folder date and time stamped
foldername = `date {y}-{m}-{d}_{h}{m}{s}`
# mysql user name
dbuname = "<mysql username here>";
# mysql password
dbpwd = "<mysql user passwword here>";
# mysql db name
dbname = "<mysql db name here>";
# mysql host name
dbhost = "127.0.0.1";
Should you wish also to set up an FTP transfer of the files to an online storage space somewhere, you can initialise the following variables also:
# switch ftp on or off // 1=on 0=off
ftpswitch = 0;
# ftp server
ftphost = "ftp.yourdomain.com";
# ftp username
ftpuname = "<ftp username here>";
# ftp pass
ftppwd = "<ftp user password here>";
# ftp path
ftppath = "<path to ftp file store here>";
Because of course we are storing passwords in the script, if your computer is publicly accessible, you may want to make sure that security of the file is paramount. You can do this by ensuring you store the file in the right place perhaps in your ~/bin folder, and also to give it no group/everyone read or execution permissions
Ok so now we should be ready to proceed.
Intelligent Scripting
There are two things I like to do when writing a script. The first is to allow the end user running the script to be able to see output so they know at what point the script is in the process of running. Admittedly, a script may run too fast for your to read the text scrolling up the terminal window, but there are times when you may be dumping a large database or tarring the dump file, when the output will pause and you will be able to read it.
Screen output can be achieved by using echo statements, as follows:
echo Backup Script Processing // - - -
The second thing I like to do is to run the script through a series of if/else statements so that you can always give your script the option to do something, like exit depending on the result of an action. An example of this would be if you checked the amount of disk space left on your backup hard drive and found it to be too low for the file size then you could simple exit.
Results from if/else statements can be achieved as follows:
if
cd $pathtobackup
then
echo ... successfully navigated to Backup Dir!
else
echo ... Backup Dir NOT Mounted! Exiting Script
exit 0
fi

















