Backup Script Kinda working!

Hi,

(still a newbie) . . . . I’m attempting to compile a short script which will take all files from an external USB drive, and copy them to a folder on my backup drive.

Having never used the shell or POSIX commands before, I decided to give a script I found on these forums, a try.

set original_folder to contents of "USB"
set backup_location to "/Users/myimac/Desktop/Backuptestfolder"

do shell script "/usr/bin/rsync -atE " & quoted form of POSIX path of original_folder & space & quoted form of POSIX path of backup_location

it works as a very basic script, but I’d like some guidance getting it to work as I’d like:

  1. Each time I copy a file from “USB” to my external backup drive, I’d like it to date and time stamp the file so I can find the files quickly in future (maybe archive the files as they’re copied too?).

  2. Presently its creating a folder called “USB” in the “Backuptestfolder” folder, but I just need to copy the files.

  3. Is there a way I can show the progress of the backup while its happening? Even a dialog box which stays visible until the process finishes would help!

Finally, I’d like to know what the “E” is for after the rsync command - can’t seem to find a reference for this anywhere!

Any help would be appreciated.

Hi,

to answer some of your questions …

Have a look at http://manpagez.com/man/1/rsync/ for explanations to “rsync”, you’ll find the “E” :wink:

  1. If the Sourcepath ends up with a slash only the contents will be synced …
  2. Have a look at the unscripted board to find a article called “using growl …”

You could use “launchd” to trigger a "rsync " by a special event ->startup, mount etc. …
→ Read the manpage
Google for “Lingon” which is a GUI for “launchd”

If you save the following code as StayOpenScript, it will do a sync every 5 Minutes and write a logfile to your desktop …

Hope it works …

property USBDrive : "USB:"
property BackUpFolder : (path to desktop as text) & "Backup:"

do shell script "mkdir -p " & quoted form of POSIX path of BackUpFolder

property delaycheck : 300

on idle
	try
		alias USBDrive
		my dorsync(alias USBDrive, alias BackUpFolder)
	on error
		return delaycheck
	end try
end idle

on dorsync(theSource, theDest)
	
	set theReport to do shell script "rsync -avz " & quoted form of POSIX path of theSource & space & quoted form of POSIX path of theDest
	
	set logfile to (((path to desktop folder) as text) & "Sync Log File.log") --path to logfile
	my writeReport(theReport, logfile, true) --call handler
	
	return delaycheck
end dorsync

on writeReport(this_data, target_file, append_data) --write logfile
	try
		set the target_file to the target_file as text
		set the open_target_file to ¬
			open for access file target_file with write permission
		if append_data is false then ¬
			set eof of the open_target_file to 0
		write this_data to the open_target_file starting at eof
		close access the open_target_file
		return true
	on error
		try
			close access file target_file
		end try
		return false
	end try
end writeReport

Hans,

Thanks for the response, the link you provided will take a while to read, so I’ll save that for the weekend when i have more time. I’ve been looking around for a site like this for a while, trying to make sense of the rsync command - so this resource is great!

Regarding the script - I tried to run it but it doesn’t seem to work unfortunately!

When I try to run it I get the following:

In the Result window I get simply " "
In the Events window I have

tell current application
	do shell script "mkdir -p '/Users/steevg/Desktop/Backup/'"
		--> ""
end tell
Result:
""

Thanks again for the information, hopefully I can build on this after I’ve divulged the information in the link!

Hi,

you should save it as Programm with option stay open
it won’t run from Scripteditor

I changed it a bit so you’re forced to select the Source- and Destfolder the first time you start it …

Have a nice day

Hans

property USBDrive : ""
property BackUpFolder : ""


property delaycheck : 30

on run
	if USBDrive is "" then
		set USBDrive to choose folder with prompt "Please choose the Sourcefolder"
	end if
	if BackUpFolder is "" then
		set BackUpFolder to choose folder with prompt "Please choose the Destinationfolder"
	end if
end run

on idle
	my dorsync(USBDrive, BackUpFolder)
	return delaycheck
end idle

on dorsync(theSource, theDest)
	try
		set theReport to do shell script "rsync -avz " & quoted form of POSIX path of theSource & space & quoted form of POSIX path of theDest
		
		set logfile to (((path to desktop folder) as text) & "Sync Log File.log") --path to logfile
		my writeReport(theReport, logfile, true) --call handler
	end try
	
end dorsync

on writeReport(this_data, target_file, append_data) --write logfile
	try
		set the target_file to the target_file as text
		set the open_target_file to ¬
			open for access file target_file with write permission
		if append_data is false then ¬
			set eof of the open_target_file to 0
		write this_data to the open_target_file starting at eof
		close access the open_target_file
		return true
	on error
		try
			close access file target_file
		end try
		return false
	end try
end writeReport