tell application "Finder"
activate
-- Configure source and destination folders
set source_folder to folder "Documents" of home
set destination_folder to folder "docs" of disk "BackupDisk"
-- Create a config file 1st time script runs to keep track of latest time run
if not (exists file "backup_script_config.txt" in source_folder) then
make new document file at source_folder with properties {name:"backup_script_config.txt"}
duplicate every item in source_folder to destination_folder with replacing
else
set last_run to modification date of file "backup_script_config.txt" in source_folder
duplicate (every item in source_folder whose modification date comes after last_run) to destination_folder with replacing
-- Update config file modification date
set the open_file to open for access file ((document file "backup_script_config.txt" of source_folder) as string) with write permission
set eof of the open_file to 0
write last_run to the open_file starting at eof
close access the open_file
end if
end tell
Targeted backups
Although Time Machine will do automated backups for those with modern versions of OS X, some readers might want an easy way to, say, keep all files related to a critical project together for an additional "in case of catastrophe" backup, or perhaps for something that could be easily downloaded to a USB drive.
It's pretty intuitive to select files to back up by name or date. For example, take this line of AppleScript:
tell application "Finder" to duplicate (every item in folder "Documents" of home whose modification date comes after (current date) - 7 * days) to folder "backup_docs" of disk "Backups" with replacing
It copies all items in your Documents folder that were modified in the last week to the backup folder on your "Backups" disk; modify the folder locations and dates as needed (note that yesterday would be current date - 1 * days [not day]). "With replacing" tells the script to overwrite any existing files with the same name.
The same "whose" construction can be used to find other things, such as these two statements that can be used for all types of script or image files:
whose name contains "script"
or
whose kind ends with "image"
.
Since I prefer to select files to back up based on "everything changed since I last ran this script," here's a quick modification created with Sal Soghoian's help:
Code
Immediate backup on import
This script is designed for tasks like saving photographic images, where (conscientious) users might want to immediately back up files as soon as they're imported. Apple offers a sample script for download, which you set up after you enter source and destination folders; it then continues to run in the background.
I encountered a bit of a performance lag when I played with it, but for some, the security of assured immediate backup is worth a bit of sluggishness.
Getting the most out of AppleScript
- 10 Hot Big Data Startups to Watch
- 11 Unique Uses for Google Glass, Demonstrated by Celebs
- How to Export Your Google Reader Account
- How to Better Engage Millennials (and Why They Aren't Really so Different)
- Telltale signs of ATM skimming
- 20 security and privacy apps for Androids and iPhones
- Big screen con artists: 7 great movies about social engineering
- IT Certification Study Tips
- Register for this Computerworld Insider Study Tip guide and gain access to hundreds of premium content articles, cheat sheets, product reviews and more.
- Software Asset Management: Ensuring Today's Assets Today's trends like BYOD and SaaS are new and exciting in terms of how they will help make our jobs more productive but...
- Software Asset Management: Getting Started Find out what steps to take that can lead your organization down the smooth path to SAM deployment.
- Top Three Reasons Why Customers Deploy EMC VNX with EMC VPLEX What if you could build a cost effective, continuously available storage infrastructure? Learn the top reasons users are deploying EMC VNX with EMC...
- Clearing the Clouds for Midmarket Businesses The 10-point checklist included in this expert brief has been developed to help small and midsize businesses select the cloud model and cloud...
- The ServiceNow Service Automation Platform During this webinar, you will discover how ServiceNow is enabling organizations to increase their competitive agility, user satisfaction and productivity, all while enhancing...
- Building a Business Case for Service Management & Automation As an IT infrastructure and operations (I&O) leader you understand the business and IT impact of service management and automation (SMA). All Operating Systems White Papers | Webcasts
From invoking 'God Mode' to hacking the lock screen, here are 10 ways to make Windows 8 act the way you want. Read more...