Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Automate iCal backups Apps
This is a tip about using iCal and AppleScript to create automatic daily backups of iCal data. Full credit for this goes to BusyMac.

Here is the brief procedure, and there are some helpful illustrations at the BusyMac site, and some suggested modifications as well.
  • Save this iCal Backup script to your Applications folder.
  • Create a new event in iCal that repeats every day (e.g. 11:00 PM).
  • Set the Alarm type to Run Script. Then choose Other... and select the iCal Backup script stored in the Applications folder.
Now every night at 11:00 PM, iCal will run the AppleScript and back itself up. And, iCal does not have to be running for the script to run.

By default, the script creates backup files that are date and time stamped (iCal 2008-11-14 23.00.00.icbu) and stores them in ~/Documents/iCal Backups. It archives up to ten days worth of backups and deletes the older backups.

[crarko adds: I tested this, and it works as described. I've used iCal scheduled scripts for a number of things elsewhere, and it's proven to be a handy technique. This is a good script, and since it's available as source can serve as a good lesson for things like OS version checking.]
    •    
  • Currently 2.95 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (20 votes cast)
 
[14,068 views]  

Automate iCal backups | 15 comments | Create New Account
Click here to return to the 'Automate iCal backups' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Automate iCal backups
Authored by: osxpounder on Aug 13, '10 12:39:54PM

This can be wonderful. Thanks very much for sharing this hint.

Fellow AppleScript newbies, note: the script ignores the difference between upper- and lower-case characters in the name of the folder it uses to store backups.

In other words, "iCal Backups" and "iCal backups" work the same. I found this because I already had a folder called "iCal backups". The script ran fine and put its backups there.

Edited on Aug 13, '10 12:43:28PM by osxpounder



[ Reply to This | # ]
Automate iCal backups
Authored by: TvE on Aug 14, '10 04:07:43AM

Well - that's not the script that is causing this behavior - it's OS X's "default filesystem formatting caracteristica".

It's a case RESPECTING as opposed to case SENSITIVE

On a case SENSITIVE filesystem you would be able to have both the named folders existing - that is not possible in the former filesystem.

TvE-iMac27:TMP tve$ mkdir CASE-RESPECTING
TvE-iMac27:TMP tve$ mkdir case-respecting
mkdir: case-respecting: File exists



[ Reply to This | # ]
Automate iCal backups
Authored by: osxpounder on Aug 17, '10 02:53:48PM

Thank you for explaining that. Interesting. I appreciate it.



[ Reply to This | # ]
Automate iCal backups
Authored by: agordona on Aug 14, '10 03:12:29AM

Thank You :=))

---
Tony
Mac Pro 2.93 GHz Quad-Core Intel Xeon - 16 GB ram
MacBook Pro 17" 2.66 Ghz Intel Core i7 - 8 GB ram
Mac OS 10.6.4



[ Reply to This | # ]
Automate iCal backups
Authored by: regulus on Aug 14, '10 07:55:53AM

I'm not sure why this is useful. First, why do you need daily backups? Do you often revert to an older version? Are you putting in information that you need to remove? Does ical get corrupted on you? This isn't making sense. And second, if you did need a backup isn't that what Time Machine is for?



[ Reply to This | # ]
Automate iCal backups
Authored by: osxpounder on Aug 17, '10 03:01:43PM

Good questions.

I can tell you why I feel the need for frequent backups (not daily, though):

1. I rely on iCal's sync feature with MobileMe.
2. I like to keep iCal events going back more than a year.
3. In the past, iCal sync often failed (see #1, above).
4. Apple support eventually helped me figure out solution to #3: my iCal sync file was too large. Apparently there was an upper limit to its size, and I discovered it the hard way. First, I had occasional sync probs, then frequent sync probs, then, no sync at all.
5. Because of #4, I was often forced to restore my iCal from a backup.
6. Now that I can't keep an iCal that goes back many years, I want to keep backups so that I can at least have a chance of looking over my calendars for, say, 2006.

Nowadays, I don't know if the upper size limit still exists, but... I expect I'll find out, because I still want to keep a long iCal calendar with a long memory. I'm too lazy to keep a diary, but the iCal record helps me with all kinds of stuff. This hint will help me keep it working the way *I* want.



[ Reply to This | # ]
Automate iCal backups
Authored by: Dave Creek on Aug 14, '10 01:49:38PM

Is there a way to automatically backup the Address Book?



[ Reply to This | # ]
Automate iCal backups
Authored by: tedw on Aug 15, '10 01:08:54AM
yes, you can follow the same basic procedure: copy all the files/folders in ~/Library/Application Support/AddressBook into a folder called Address Book - YYYY-MM-DD, then give it the extension .abbu. I'd add it to the script, but the script is such a mess I'd feel compelled to rewrite it first, and that will have to wait till tomorrow.

[ Reply to This | # ]
Automate iCal backups
Authored by: Dave Creek on Aug 15, '10 09:36:15AM

Thank you, tedw
Dave



[ Reply to This | # ]
Automate iCal backups
Authored by: tedw on Aug 15, '10 11:24:43AM
Here's a modified script that handles both ical and address book. I've only tested it on 10.6 - it should work on 10.5, probably on 10.4, but no guarantees. Use at your own risk, and test it before using it. testing means:
  1. use the script to make a back up
  2. make a manual backup
  3. try restoring from the script-generated backup
  4. check to make sure it worked correctly
the script...
property keepFor : 10 * days -- how long to keep backups
property BUFoldName : "iCal and AB Backups" -- main backup folder name

-- set backup folder variables (creating if needed)
set dateStamp to ymdDateFormat(current date)
set mainBackupFolder to getFolder(BUFoldName, path to documents folder from user domain)
set iCalTimeStampedFolder to getFolder("iCal " & dateStamp, mainBackupFolder)
set ABTimeStampedFolder to getFolder("Address Book - " & dateStamp, mainBackupFolder)

tell application "Finder"
	-- find outdated backup files and move them to trash
	set oldBackups to files of mainBackupFolder where (name ends with ".icbu" or name ends with ".abbu") and modification date is less than ((get current date) - keepFor)
	delete oldBackups
	
	-- copy the folders *as folders* to the backup location, moving unneeded files and folders to trash
	if version ≥ 10.5 then
		-- iCal data location for Leopard and Snow Leopard
		set plistVers to "2.0"
		set iCalDuplicatedFold to (duplicate (folder "Calendars" of (path to library folder from user domain)) to iCalTimeStampedFolder)
		tell iCalDuplicatedFold
			set deletables to items whose name does not end with ".calendar"
		end tell
	else
		-- iCal data location for Tiger
		set plistVers to "1.0"
		set iCalDuplicatedFold to (duplicate (folder "iCal" of (path to application support from user domain)) to iCalTimeStampedFolder)
		tell iCalDuplicatedFold
			set deletables to {file "alarmsCache.plist"} & files whose name begins with "sync" and file extension is "plist"
		end tell
	end if
	delete deletables
	
	-- Address Book data location for all versions (I think)
	set ABDuplicatedFold to (duplicate (folder "AddressBook" of (path to application support from user domain)) to ABTimeStampedFolder)
	
	empty trash
end tell

tell application "System Events"
	-- make info.plist for the iCal folders
	set the rootDictionary to make new property list item with properties {kind:record}
	set the plistPath to (POSIX path of (iCalTimeStampedFolder as alias)) & "/info.plist"
	set plistFile to make new property list file with properties {contents:rootDictionary, name:plistPath}
	tell plistFile
		make new property list item at end of property list items with properties {kind:date, name:"date", value:(current date)}
		make new property list item at end of property list items with properties {kind:string, name:"version", value:plistVers}
	end tell
	
	-- convert folders to appropriate backup files
	set name of iCalTimeStampedFolder to name of iCalTimeStampedFolder & ".icbu"
	set name of ABTimeStampedFolder to name of ABTimeStampedFolder & ".abbu"
end tell

to ymdDateFormat(inDate) -- Old_date is text, not a date.
	--subroutine makes YYYY-MM-DD date strings from applescript date
	set {tid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "-"}
	set outDate to (year of inDate & (month of inDate) * 1 & day of inDate) as text
	set AppleScript's text item delimiters to tid
	return outDate
end ymdDateFormat

to getFolder(f, p)
	-- subroutine returns reference to folder, creating folder if needed
	tell application "Finder"
		if exists folder f of p then
			return folder f of p
		else
			return (make new folder at p with properties {name:f})
		end if
	end tell
end getFolder


[ Reply to This | # ]
Automate iCal backups
Authored by: Dolf on Aug 15, '10 02:09:19AM

This script essentially copies the iCal files, so does Timemachine. What is the reason for such a scipt?



[ Reply to This | # ]
Automate iCal backups
Authored by: tedw on Aug 15, '10 11:07:53AM

no reason at all, if you use time machine. if you don't...



[ Reply to This | # ]
Automate iCal backups
Authored by: wdc88 on Aug 17, '10 03:27:05PM

Where does one find Time Machine's iCal backups? Recently my calendar was corrupted, and I thought I could recover from Time Machine, but I couldn't locate the files. All help much appreciated.



[ Reply to This | # ]
Automate iCal backups
Authored by: Adagioblue on Aug 18, '10 02:36:19AM

To find your iCal calendars look in your Username/Library/Calendars.
Inside the Calendars folder, each folder is an iCal calendar.
Inside each calendar are the events belonging to that calendar along with a plist file. You can look at the last few lines of the plist and see which calendar that group of events belongs to.

Example:

<key>Title</key>
<string>US Holidays</string>
<key>Type</key>
<string>Subscription</string>
</dict>
</plist>



[ Reply to This | # ]
Automate iCal backups
Authored by: Ilovemac on Oct 07, '10 12:37:53AM

Apologies for my ignorance.
You gave a zip file to be saved in application folder. But then you also wrote the modified script to backup both iCal and Address book automatically. Now I do not know how exactly I can use this script to save to application folder. Should I copy-paste to text and then zip and then save.....or some other way. Sorry but I have never copied any script before.
Thanks in advance



[ Reply to This | # ]