Clear Linux etc files.
[GalaxyCodeBases.git] / etc / Server / fmsbackup.Mac.sh
blob646d4ff34a036452a9b9d226b407d95dbb906e3a
1 #!/bin/sh
3 # http://web.sevensdoor.com/download/fmsbackup.sh.txt
5 # FileMaker Server Backup Script ver.1.5.2
6 # Backup FileMaker database files latest n hours and latest n days.
7 # This script is written for FileMaker Server 7-11 on Mac OS X.
9 # Copyright (C) 2007 Koji Takeuchi
11 # This script is free software; you can redistribute it and/or
12 # modify it under the terms of the GNU General Public License
13 # as published by the Free Software Foundation; either version 2
14 # of the License, or (at your option) any later version.
16 # This script is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
21 # Koji Takeuchi <takeuchi@nemoux.com>
22 # 2007.09.20
25 # To use this script:
26 # 0. Set permission of this file to executable.
27 # ex. $ chgrp fmsadmin fmsbackup.sh
28 # $ chmod 750 fmsbackup.sh
29 # 1. You can edit 'set variables' section of this file directly, then
30 # run this script.
31 # OR
32 # 2. You can separate 'set variables' section to other files as a 'conf' file.
33 # and run this script with arguments to include variables specified on
34 # separate 'conf' files.
35 # ex. $ ./fmsbackup.sh hourly.conf
36 # $ /usr/local/bin/fmsbackup.sh ~/Documents/daily.conf
38 # If you specify the same variables on both this file and the separate file,
39 # this file's variables are replaced by variables loaded from the
40 # separate file.
42 # To run this script periodically:
43 # 1. Put this script on '/Library/FileMaker Server/Data/Scripts', then
44 # use 'System-Level Script' schedule on FileMaker Server 9
45 # Admin Console.
46 # OR
47 # 2. Install your crontab.
48 # ex: $ crontab -e
49 # 03 * * * * /usr/local/bin/fmsbackup.sh ~/Documents/hourly.conf
50 # 30 04 * * * /usr/local/bin/fmsbackup.sh ~/Documents/daily.conf
52 # In this case, hourly schedule will launch every '**:03',
53 # and daily schedule will launch every '04:30'.
54 # If you lose your mind when you just enter 'crontab -e',
55 # see 'man vi'. (or just type ':q!' and forget it.)
56 # OR
57 # 3. If you are Apple Certified System Administrator, use launchd.
58 # Figure out a property list file and save as
59 # '/Library/LaunchDaemons/com.example.hourlybackup.plist'.
60 # And load the property list file.
61 # ex: $ sudo launchctl -w load /Library/LaunchDaemons/com.example.hourlybackup.plist
63 # - contents of property list file should be like this...
65 # <?xml version="1.0" encoding="UTF-8"?>
66 # <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
67 # <plist version="1.0">
68 # <dict>
69 # <key>Label</key> <string>com.example.hourlybackup</string>
70 # <key>Disabled</key> <false/>
71 # <key>ProgramArguments</key>
72 # <array>
73 # <string>/usr/local/bin/fmsbackup.sh</string>
74 # <string>/Users/YOURNAME/Documents/hourly.conf</string>
75 # </array>
76 # <key>StartCalendarInterval</key>
77 # <dict>
78 # <key>Minute</key> <integer>03</integer>
79 # </dict>
80 # <key>RunAtLoad</key> <true/>
81 # </dict>
82 # </plist>
84 # To get report:
85 # Simply redirect to mail command.
86 # You can get (sometimes too many) reports via mail.
87 # ex: /usr/local/bin/fmsbackup.sh ~/Documents/daily.conf | mail -s "fms backup report" yourname@example.com
92 ################################################
93 # set variables #
94 ################################################
95 ############# Required variables #############
96 # user name for fms admin console
97 user=USERNAME
99 # password for fms admin console
100 pass=PASSWORD
102 # 'data' path ( parent of Database )
103 data=/Library/'FileMaker Server'/Data
105 # Database directory path
106 databases="$data"/Databases
108 # backup directory name
109 backupdir=Databases
111 # backup directory path
112 backups=/Library/'FileMaker Server'/Data/Backups
114 # backup Database directory path
115 backupdatabases="$backups"/"$backupdir"
117 # backup Database directory path for 'fmsadmin backup' command
118 backupspath="filemac:/'Macintosh HD'$backupdatabases/"
120 # with sub directory? ( y = 1, n = 0 )
121 subdirectory=1
123 # verify option? ( y = -x, n = "" ) for ver.11 or above
124 verify=
126 # clone option? ( y = -n, n = "" ) for ver.11 or above
127 clone=
129 # prefix of archive file name
130 prefix=servername_fms11
132 # zip or tar.gz? ( zip/tar.gz )
133 # If you don't need any archiving, just leave it blank
134 archive=tar.gz
138 ########## options for archiving ###########
139 # Archive backup files and keep them latest xx hours ( and xx days )
140 # If necessary, set 'hours' or 'days' value to n and edit following variables
142 # how many hours do you want to keep shortest terms backups? ( y = n, n = 0 )
143 #hour_short=5
145 # If hour=n, set following variables
147 # hourly generation directory name
148 #shortlydirectory=archives_shortly
150 # hourly generation directory path
151 #shortlypath="$backups"/"$shortlydirectory"
154 # how many hours do you want to keep hourly backups? ( y = n, n = 0 )
155 #hour=72
157 # If hour=n, set following variables
159 # hourly generation directory name
160 #hourlydirectory=archives_hourly
162 # hourly generation directory path
163 #hourlypath="$backups"/"$hourlydirectory"
166 # how many days do you want to keep daily backups? ( y = n, n = 0 )
167 #day=30
169 # If day=n, set following variables
171 # daily generation directory name
172 #dailydirectory=archives_daily
174 # daily generation directory path
175 #dailypath="$backups"/"$dailydirectory"
178 # how many months do you keep monthly backups? ( y = n, n = 0 )
179 #month=120
181 # If month=n, set following variables
183 # monthly generation directory name
184 #monthlydirectory=archives_monthly
186 # monthly generation directory path
187 #monthlypath="$backups"/"$monthlydirectory"
191 ######## options for external volume #########
192 # Syncronize each backup directory to external backup volume
193 # If necessary, set 'ext_*ly' value to 1 and edit following variables
195 # mirror shortly directory to external volume? ( y = 1, n = 0 )
196 #ext_shortly=0
198 # mirror hourly directory to external volume? ( y = 1, n = 0 )
199 #ext_hourly=0
201 # mirror daily directory to external volume? ( y = 1, n = 0 )
202 #ext_daily=0
204 # mirror monthly directory to external volume? ( y = 1, n = 0 )
205 #ext_monthly=0
207 # If ext_*ly=1, set following variables
209 # external backup directory path
210 #external=/Volumes/backup_external/fms/
214 #### options for alternate backup server 1 #####
215 # Syncronize each backup directory to other host via network
216 # If necessary, set 'altn_*ly' value to 1 and edit the following variables
217 # To use this option, backup server needs the RSA key of FM Server
219 # mirror shortly directory to backup server 1? ( y = 1, n = 0 )
220 #alt1_shortly=0
222 # mirror hourly directory to backup server 1? ( y = 1, n = 0 )
223 #alt1_hourly=0
225 # mirror daily directory to backup server 1? ( y = 1, n = 0 )
226 #alt1_daily=1
228 # mirror monthly directory to backup server 1? ( y = 1, n = 0 )
229 #alt1_monthly=1
231 # If altn_*ly=1, set following variables
233 # user name of alternate backup server 1
234 #host1user=root
236 # host name or IP address of alternate backup server 1
237 #host1=host.example.com
239 # backup directory path of alternate backup server 1
240 #host1path=/Volumes/backup_external/foo/fms
244 #### options for alternate backup server 2 #####
245 # Syncronize each backup directory to other host via network
246 # If necessary, set 'altn_*ly' value to 1 and edit the following variables
247 # To use this option, backup server needs the RSA key of FM Server
249 # mirror shortly directory to backup server 2? ( y = 1, n = 0 )
250 #alt2_shortly=0
252 # mirror hourly directory to backup server 2? ( y = 1, n = 0 )
253 #alt2_hourly=0
255 # mirror daily directory to backup server 2? ( y = 1, n = 0 )
256 #alt2_daily=1
258 # mirror monthly directory to backup server 2? ( y = 1, n = 0 )
259 #alt2_monthly=1
261 # If altn_*ly=1, set following variables
263 # user name of alternate backup server 2
264 #host2user=root
266 # host name or IP address of alternate backup server 2
267 #host2=host.example.com
269 # backup directory path of alternate backup server 2
270 #host2path=/Volumes/backup_external/foo/fms
274 ################################################
275 # load variables #
276 ################################################
277 ####### set variables from other file ########
278 # If run this script with arguments, some of the variables you set above
279 # are replaced by contents of file specified by argument
281 # DO NOT EDIT following 5 lines
282 includes="$1"
284 if [ -r "$includes" ]; then
285 . "$includes"
288 ################################################
289 # fmserver backup #
290 ################################################
291 FMSALIVE=$(ps axww | grep fmserverd | grep -v grep)
292 ISCONSOLEARRIVE=$(/usr/bin/fmsadmin list files -u "$user" -p "$pass" | grep [Ff][Pp]7 | grep -v grep)
293 if [ -z "$FMSALIVE" ]; then
294 echo "FileMaker Server is not working."
295 echo "exit script."
296 exit 1
298 if [ -n "$ISCONSOLEARRIVE" ]; then
299 echo "fmsadmin is alive."
300 else
301 echo "is fmsadmin dead?\n\n$ISCONSOLEARRIVE" | mail -s "`hostname` fmsadmin failure" takeuchi@splash.jp
302 exit 1
304 echo "-------------------------------------------------------------------"
305 echo ""
306 echo " `hostname` FileMaker Server Backup Report!"
307 echo ""
308 echo "-------------------------------------------------------------------"
309 echo ""
310 echo ""
311 echo ""
312 echo "1. Backup database files"
314 # Delete old files in temporary backup directory
315 rm -rf "$backupdatabases"/*
317 # Synchronize source directory to temporary backup directory
318 cd "$data"
319 /usr/bin/fmsadmin backup "$backupdir"/*.[FfIi][PpNn][7Ss] "$verify" "$clone" -d "$backupspath" -u "$user" -p "$pass"
320 if [ "$subdirectory" -eq 1 ]; then
321 /usr/bin/fmsadmin backup "$backupdir"/*/*.[FfIi][PpNn][7Ss] "$verify" "$clone" -d "$backupspath" -u "$user" -p "$pass"
324 # Get timestamp for archive file name
325 shortlyfilename="$prefix"_`date +"%Y_%m_%d_%H%M%S"`
326 hourlyfilename="$prefix"_`date +"%Y_%m_%d_%H%M%S"`
327 dailyfilename="$prefix"_`date +"%Y_%m_%d"`
328 monthlyfilename="$prefix"_`date +"%Y_%m"`
330 echo " ... Done!"
331 echo ""
334 echo "2. Deleting old files"
336 # Delete old files in n hours directory and n days directory
337 if [ "$hour_short" -gt 0 ]; then
338 cd "$shortlypath"
339 find . -type f -mmin +`expr "$hour_short" \* 60` | xargs rm -fv
341 if [ "$hour" -gt 0 ]; then
342 cd "$hourlypath"
343 find . -type f -mmin +`expr "$hour" \* 60` | xargs rm -fv
345 if [ "$day" -gt 0 ]; then
346 cd "$dailypath"
347 find . -type f -mtime +"$day" | xargs rm -fv
349 echo " ... Done!"
350 echo ""
353 echo "3. Archiving files"
355 # Archive temporary backup directory to zip or tar ball
356 cd "$backups"
357 if [ "$archive" = "zip" ]; then
358 zip -r "$prefix".zip "$backupdir"
359 elif [ "$archive" = "tar.gz" ]; then
360 tar czvpf "$prefix".tar.gz "$backupdir"
363 # Copy archive file to 'nhours directory/prefix_yyyy_MM_dd_hhmmss.tar.gz'
364 # and 'ndays directory/prefix_yyyy_MM_dd.tar.gz'
365 # and 'nmonths directory/prefix_yyyy_MM.tar.gz'
366 if [ "$hour_short" -gt 0 ]; then
367 cp -fp "$prefix"."$archive" "$shortlydirectory"/$shortlyfilename."$archive"
369 if [ "$hour" -gt 0 ]; then
370 cp -fp "$prefix"."$archive" "$hourlydirectory"/$hourlyfilename."$archive"
372 if [ "$day" -gt 0 ]; then
373 cp -fp "$prefix"."$archive" "$dailydirectory"/$dailyfilename."$archive"
375 if [ "$month" -gt 0 ]; then
376 cp -fp "$prefix"."$archive" "$monthlydirectory"/$monthlyfilename."$archive"
378 echo " ... Done!"
379 echo ""
382 ## options for external volume ##
384 if [ "$ext_shortly" -eq 1 ] || [ "$ext_hourly" -eq 1 ] || [ "$ext_daily" -eq 1 ] || [ "$ext_monthly" -eq 1 ]; then
385 echo "4. Syncronizing archive files to external drive"
388 # Syncronize each backup directory to alternate backup directory
389 if [ "$ext_shortly" -eq 1 ]; then
390 rsync -vrpo --delete -u --ignore-existing "$shortlydirectory" "$external"
392 if [ "$ext_hourly" -eq 1 ]; then
393 rsync -vrpo --delete -u --ignore-existing "$hourlydirectory" "$external"
395 if [ "$ext_daily" -eq 1 ]; then
396 rsync -vrpo --delete -u --ignore-existing "$dailydirectory" "$external"
397 /bin/cp -vp "$dailydirectory"/$dailyfilename."$archive" "$external"/"$dailydirectory"/
399 if [ "$ext_monthly" -eq 1 ]; then
400 rsync -vrpo --delete -u --ignore-existing "$monthlydirectory" "$external"
401 /bin/cp -vp "$monthlydirectory"/$monthlyfilename."$archive" "$external"/"$monthlydirectory"/
403 currentnum=4
404 echo " ... Done!"
405 echo ""
406 else
407 currentnum=3
411 ## options for alternate backup server 1 ##
413 if [ "$alt1_shortly" -eq 1 ] || [ "$alt1_hourly" -eq 1 ] || [ "$alt1_daily" -eq 1 ] || [ "$alt1_monthly" -eq 1 ]; then
414 echo `expr "$currentnum" \+ 1`". Transfering archive files to alternate backup server 1";
417 # Syncronize each backup directory to other host via network
418 if [ "$alt1_shortly" -eq 1 ]; then
419 rsync -vrpo --delete -u --ignore-existing --copy-links -e ssh "$shortlydirectory" "$host1user"@"$host1":"$host1path"/
421 if [ "$alt1_hourly" -eq 1 ]; then
422 rsync -vrpo --delete -u --ignore-existing --copy-links -e ssh "$hourlydirectory" "$host1user"@"$host1":"$host1path"/
423 /usr/bin/scp -vp "$hourlydirectory"/$hourlyfilename."$archive" "$host1user"@"$host1":"$host1path"/"$hourlydirectory"/
425 if [ "$alt1_daily" -eq 1 ]; then
426 rsync -vrpo --delete -u --ignore-existing --copy-links -e ssh "$dailydirectory" "$host1user"@"$host1":"$host1path"/
427 /usr/bin/scp -vp "$dailydirectory"/$dailyfilename."$archive" "$host1user"@"$host1":"$host1path"/"$dailydirectory"/
429 if [ "$alt1_monthly" -eq 1 ]; then
430 rsync -vrpo --delete -u --ignore-existing --copy-links -e ssh "$monthlydirectory" "$host1user"@"$host1":"$host1path"/
431 /usr/bin/scp -vp "$monthlydirectory"/$monthlyfilename."$archive" "$host1user"@"$host1":"$host1path"/"$monthlydirectory"/
433 echo " ... Done!"
434 echo ""
435 echo ""
439 ## options for alternate backup server 2 ##
441 if [ "$alt2_shortly" -eq 1 ] || [ "$alt2_hourly" -eq 1 ] || [ "$alt2_daily" -eq 1 ] || [ "$alt2_monthly" -eq 1 ]; then
442 echo `expr "$currentnum" \+ 1`". Transfering archive files to alternate backup server 2";
445 # Syncronize each backup directory to other host via network
446 if [ "$alt2_shortly" -eq 1 ]; then
447 rsync -vrpo --delete -u --ignore-existing --copy-links -e ssh "$shortlydirectory" "$host2user"@"$host2":"$host2path"/
449 if [ "$alt2_hourly" -eq 1 ]; then
450 rsync -vrpo --delete -u --ignore-existing --copy-links -e ssh "$hourlydirectory" "$host2user"@"$host2":"$host2path"/
451 /usr/bin/scp -vp "$hourlydirectory"/$hourlyfilename."$archive" "$host2user"@"$host2":"$host2path"/"$hourlydirectory"/
453 if [ "$alt2_daily" -eq 1 ]; then
454 rsync -vrpo --delete -u --ignore-existing --copy-links -e ssh "$dailydirectory" "$host2user"@"$host2":"$host2path"/
455 /usr/bin/scp -vp "$dailydirectory"/$dailyfilename."$archive" "$host2user"@"$host2":"$host2path"/"$dailydirectory"/
457 if [ "$alt2_monthly" -eq 1 ]; then
458 rsync -vrpo --delete -u --ignore-existing --copy-links -e ssh "$monthlydirectory" "$host2user"@"$host2":"$host2path"/
459 /usr/bin/scp -vp "$monthlydirectory"/$monthlyfilename."$archive" "$host2user"@"$host2":"$host2path"/"$monthlydirectory"/
461 echo " ... Done!"
462 echo ""
463 echo ""
467 ## Report what was done ##
469 echo "Now $databases is copied to"
470 echo " $backupdatabases"
471 if [ "$hour_short" -gt 0 ]; then
472 echo "and archived to $shortlypath/$shortlyfilename.$archive."
474 if [ "$hour" -gt 0 ]; then
475 echo "and archived to $hourlypath/$hourlyfilename.$archive."
477 if [ "$day" -gt 0 ]; then
478 echo "and archived to $dailypath/$dailyfilename.$archive."
480 if [ "$month" -gt 0 ]; then
481 echo "and archived to $monthlypath/$monthlyfilename.$archive."
483 if [ "$ext_shortly" -eq 1 ]; then
484 echo "and $shortlypath directory is mirrored to"
485 echo " $external"
487 if [ "$ext_hourly" -eq 1 ]; then
488 echo "and $hourlypath directory is mirrored to"
489 echo " $external"
491 if [ "$ext_daily" -eq 1 ]; then
492 echo "and $dailypath directory is mirrored to"
493 echo " $external"
495 if [ "$ext_monthly" -eq 1 ]; then
496 echo "and $monthlypath directory is mirrored to"
497 echo " $external"
499 if [ "$alt1_shortly" -eq 1 ]; then
500 echo "and $shortlypath directory is mirrored to"
501 echo " $host1:$host1path/$shortlydirectory"
503 if [ "$alt1_hourly" -eq 1 ]; then
504 echo "and $hourlypath directory is mirrored to"
505 echo " $host1:$host1path/$hourlydirectory"
507 if [ "$alt1_daily" -eq 1 ]; then
508 echo "and $dailypath directory is mirrored to"
509 echo " $host1:$host1path/$dailydirectory"
511 if [ "$alt1_monthly" -eq 1 ]; then
512 echo "and $monthlypath directory is mirrored to"
513 echo " $host1:$host1path/$monthlydirectory"
515 if [ "$alt2_shortly" -eq 1 ]; then
516 echo "and $shortlypath directory is mirrored to"
517 echo " $host2:$host2path/$shortlydirectory"
519 if [ "$alt2_hourly" -eq 1 ]; then
520 echo "and $hourlypath directory is mirrored to"
521 echo " $host2:$host2path/$hourlydirectory"
523 if [ "$alt2_daily" -eq 1 ]; then
524 echo "and $dailypath directory is mirrored to"
525 echo " $host2:$host2path/$dailydirectory"
527 if [ "$alt2_monthly" -eq 1 ]; then
528 echo "and $monthlypath directory is mirrored to"
529 echo " $host2:$host2path/$monthlydirectory"
531 echo ""
532 echo ""
533 echo ""
534 echo "FileMaker Server Backup completed."
535 echo ""
536 echo ""
537 echo ""
538 echo "---------------------- end of Backup Report ---------------------"
539 echo ""
540 echo ""
541 exit 0