Shell Backup 2 - local testing
Method overview
The overall process is fairly simple, and both a testing on a local mac protocol, and a server version are detailed in the following discussion. The basic process is:
- create a 2 line shell script in a text editor to specify the backup from where to where
- specify the appropriate shell script file permissions using BatChmod
- Create the launchd file using Lingon, that executes the shell scrip, and specifies when and how often to execute it.
- log out, log in
Local test procudure workup
It is a good idea to work through the process on one's dev box, before implementing an equivalent setup on the server. The script mimicks the /library/FileMaker Server/ path construct, but allows testing locally, without any potential FileMaker Server permissions issues.
Create local test source and destination directories
The FileMaker Server directory on the server has a space character in the name, and this needs to appropriately 'esacped' in the specified path syntax, So for the local test a simple AAA BBB directory is created to mimick the server directory name and illustrate the handling works.
- Create a directory AAA BBB under /Library/ and a subfolder CCC , and drop a couple of files into CCC
- Create a directory under Shared as the destination for the backups
- so the source is: Library/AAA BBB/CCC
- and the destination is: /Users/Shared/fms_backups
A simple backup test using the terminal and the ditto command
With the source directory in place and with contents, an appropriate ditto command can be run in a terminal shell. This will create the destination directory fms_backups under Shared, and a date stamped zip archive in the destination directory. This is just a simple way of initial testing. It is probably easier to get the syntax right using the shell script, which is required anyway.
ditto -c -k -rsrc /Library/AAA\ BBB/CCC /Volumes/cortex/Users/Shared/fms_backups/`date +%y-%m-%d`_ccc.zip
ditto -c -k -rsrc /Library/"AAA BBB"/CCC /Volumes/cortex/Users/Shared/fms_backups/`date +%y-%m-%d`_ccc.zip
Where a space exists in the name of a directory or file, the space needs to be escaped in the command syntax. The escape can either be a backslash character immediately prior to the space character, or by enclosing the space including name string in quotes: e.g. AAA\ BBB or "AAA BBB"
Normally the shell uses spaces to separate arguments to commands. While both syntax constructs illustrated here work, escaping the space with a backslash is probably better practice. Smultron will red flag the quotation string.
While both work, the backslash method is preferred. It is how the system handles it after all, which can be illustrated by opening a terminal, and dragging a directory named AAA BBB onto the terminal, this will automatically enter the path at the terminal prompt, and it will use the backslash syntax e.g. /Library/AAA\ BBB/CCC.
Create a local backup test shell script
Open a text editor (Smultron or Text Wrangler) , specify Unix Line endings (Smultron: Text|:Line Endings; TextWrangler: at save) and enter the equivalent to the following, and save the file (bup_test.sh) initially to the desktop for convenience. Working shell scripts can be saved into ~/Library/Scripts. The script starts with #!
. This is known as an interpreter line.
#!/bin/sh
ditto -c -k -rsrc /Library/"AAA BBB"/CCC /Volumes/cortex/Users/Shared/fms_backups/`date
+%y-%m-%d`_test.zip
- Using Smultron or Text Wrangler allows specify UNIX line endings, and avoids hidden character issues resulting from line ends that may otherwise arise using TextEdit and others.
- The easy way to parse the source and destination paths is to open terminal, and drag the relevant directory onto the terminal window. This will enter the full path at the prompt. Then just copy and paste the parsed path into the text editor. The example uses a directory named 'CCC' as the source, and specifies the destination as fms_backups.
- The destination directory (fms_backups) specified in the shell script example does not have to pre-exist, the shell process will create it, provided it is closed by a forward slash.
Define executable permissions to the shell script file
A shell script is a file that contains commands that the shell can execute. To execute a shell script by giving its name as a command, the executing user must have permissions to read and execute that file. (ie the eventual launchd). When a shell script is created using a text editor, it will not have the necessary permissions.
The shell script needs to be executable, so in terminal change directory to that containing the script and use chmod 755 script name, or use the BatChmod utility.
change directory to the directory containing the shell script file (desktop), then chmod, or include the path in the chmod command
chmod 755 ~/desktop/bup_test.sh
Alternately use BatChmod
Load the file into BatChmod and set the preferences
view the file permissions using ls -l, assuming the shell is temporarily on the desktop :
$
$ cd ~/desktop
$ ls -l
>> -rwxr-xr-x@ 1 cortical staff 101 30 Jun 11:30 bup_test.sh
Test the shell script
Open a Terminal window and type the path and name of the shell script (assuming execute permissions on the shell script). This will create a backup in the destination directory.
~/desktop/bup_test.sh
An even simpler way is to duplicate the shell script file and rename the extension to .command (bup_test.command). Then double clicking on the file opens a terminal and executes the script. Quick and easy.