Shell Scripting MySQLdump and FTP File Backup
The purpose of this article is to give you a quick fire introduction to the world of shell scripting. It is by no stretch of the imagination a complete guide to the technique of shell scripting but It will give you the basis of a script for backing up your web files and mysql databases, locally and remotely. You can then take this and work with it to make it fit your needs and requirements.
During this article we will be using the Bourne shell interpreter to perform the tasks and functions we need. We will be writing our script in a plain text editor and then saving it and providing the file with the necessary:
Scripting Tools
For the most part, I either write my own scripts in BBedit or in a little shareware app called ScriptGUI. The latter allows you to quickly and easily test the results of your scripts as you go along, and also allows you to save the script as a droplet so that it can be run from the desktop with a simple double click, or by dragging and dropping files onto it.
Getting Started
As with any shell script you need to let the host computer know which shell interpreter to use for the tasks you are going to perform. You do this by opening the first line of your script with the following syntax:
#! /bin/sh
After this first line the hash/pound key is then used to comment out lines so that the interpreter can ignore them as it runs through your script.
To kick off the script then, we will start by creating certain number of fixed variables. User-assigned variables tend to be written in lowercase whilst system and environment variables are handled in uppercase.
Assuming we are keeping the backups on a second hard drive in OS X called Backups we will create a user defined variable containing a path to this as follows:
pathtobackup="/Volumes/Backups";
In calling this variable at a later date you will precede it by a dollar ($) sign as follows:
$pathtobackup
Another aspect of shell scripting that is very important to understand is how to handle back (`), double (“) and single quotes ("). Double and single quotes are used to contain literals. That means that anything contained within double quotes will be treated as is. Anything in back quotes is used for piping output from a command into a variable stripping out carriage returns as it does so.
Back quotes would be used in our script as follows:
date = `date {y}-{m}-{d}_{H}{m}{s}`
this would assign a value of the current date and time (24hour clock) into a variable called date.
As we quickly run through a series of ways to handle variables, perhaps the last important way to handle variables for the purpose of this article is user input. In order to achieve this you simply need to precede a dollar-ed variable with the command "read", for example:
echo Please Enter Username:
read $username
This will output the words "Please Enter Username:" and prompt the end user for input. Having typed in the value and pressed return, the value will be assigned to the respectively named variable and referenced in the script in the same way any other user-assigned variable is, preceding it with a dollar sign.
