Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- # Script purpose:
- # Copy FileMaker backups to remote FTP server for backups.
- # Rotate remote copies of backups on FTP server.
- # Written by William Smith, 318, Inc.
- # August 26, 2015
- # Notes:
- # This script executes when called by the com.318.FTPFileMakerBackups.plist launch daemon
- # in /Library/LaunchDaemons. Log files are located in /Library/Logs
- ##### Definitions
- scriptname=$( /usr/bin/basename -s .sh "$0" )
- filemakerbackupfolder="/Library/FileMaker Server/Data/Backups"
- ftpaddress="192.168.5.250"
- ftpusername="ftpadmin"
- ftpuserpassword="p@55w0rd"
- workingfolder="/private/tmp/fmpftpfiles"
- logfolder="/Library/Logs/$scriptname"
- logfile="$logfolder/$scriptname - $( /bin/date '+%Y-%m-%d' ).log"
- ###### Functions
- function logresult() {
- if [ $? = 0 ] ; then
- /bin/date "+%Y-%m-%d %H:%M:%S $1" >> "$logfile"
- else
- /bin/date "+%Y-%m-%d %H:%M:%S $2" >> "$logfile"
- fi
- }
- ###### Logging
- # start the log
- mkdir -p "$logfolder"
- logresult "--------------------- Begin Script ---------------------"
- # rotate logs -- delete all but the 14 most recent log files
- deleteoldlogs=$( /bin/ls -1t "$logfolder/$scriptname"*.log | /usr/bin/tail -n +15 )
- while IFS= read -r afile
- do
- logfilename=$( /usr/bin/basename "$afile" )
- /bin/rm "$afile"
- logresult "Deleting old log file: $logfilename." "No old logs to delete."
- done <<< "$deleteoldlogs"
- ##### Create list of local FileMaker Server backup folders.
- locallist=$( ls "$filemakerbackupfolder" )
- ##### Create list of remote backup files.
- remotelist=$( curl -l ftp://$ftpaddress/ --user "$ftpusername:$ftpuserpassword" )
- # create temporary working directory
- mkdir -p "$workingfolder"
- logresult "SUCCESS: Created temporary working directory: $workingfolder" "ERROR: Unable to create temporary working directory: $workingfolder"
- ##### Compare local list with remote list and
- ##### create archive of each local FileMaker backup not in the FTP server list.
- echo "$locallist" | while IFS= read -r afile
- do
- if [[ $remotelist != *"$afile"* ]] ; then
- zip -r "$workingfolder/$afile.zip" "$filemakerbackupfolder/$afile"
- logresult "SUCCESS: Created archive \"$afile.zip\"" "ERROR: Unable to create archive \"$afile.zip\""
- fi
- done
- ##### Copy archives to FTP server.
- ftp -inv $ftpaddress << End-Of-Session
- user $ftpusername $ftpuserpassword
- binary
- lcd $workingfolder
- mput *
- bye
- End-Of-Session
- logresult "SUCCESS: Transferred new archive files to FTP server $ftpaddress" "ERROR: Unable to transfer new archive files to FTP server $ftpaddress"
- ##### Remove remote backups not on local server.
- echo "$remotelist" | while IFS= read -r afile
- do
- afilename=$( echo ${afile%.*} )
- if [[ "$locallist" != *"$afilename"* ]] ; then
- curl -v -u "$ftpusername:$ftpuserpassword" "ftp://$ftpaddress" -Q "DELE $afile"
- logresult "SUCCESS: Deleted remote file \"$afile\"" "ERROR: Unable to delete remote file \"$afile\""
- fi
- done
- ##### Clean up
- # delete temporary working directory and contents
- rm -R "$workingfolder"
- logresult "SUCCESS: Deleted temporary working directory: $workingfolder" "ERROR: Unable to delete temporary working directory: $workingfolder"
- echo "" >> $logfile
- exit 0
Add Comment
Please, Sign In to add comment