Upgraded to Mac OS Server 10.6 and an old script no longer works.

Hello,

Hopefully some one here will be able to help out as I’m pulling my hair out. I’m an Applescript novice at best.

I wrote the following script some years ago to do a very lo-tech backup system of files off our xserve to an external hard drive. It has worked fine for three years, but since upgrading the server software to 10.6 it has ceased to work, stopping with a time out error. “Backup” is the name of the external hard drive.

Any ideas? The first section (delete old folder) still works fine, it’s timing out after that, and never creates the new folder or does any copying.

Thanks so much in advance for any help you can give.


--Backup Script Version 3 30/6/08

set never_end to false

repeat until never_end is true
	
	set time_now to time string of (current date)
	
	if time_now contains "8:30:" and time_now contains "PM" then
		
		--delete old folder
		set backup_alias to "Backup:" as alias
		tell application "Finder" to set full_list to get (name of folders of backup_alias)
		repeat with i from 1 to number of items in full_list
			if (item i of full_list as string) contains "backup" then
				tell application "Finder" to move ("Backup:" & item i of full_list as alias) to trash
			end if
		end repeat
		tell application "Finder" to empty trash
		delay 1800
		
		-- do backup
		set new_folder to {"Backup "} & short date string of (current date) as string
		tell application "Finder" to make new folder at "Backup:" with properties {name:new_folder}
		set back_up to "Backup:" & new_folder as alias
		
		with timeout of 36000 seconds
			
			tell application "Finder" to copy folder "XServe:Server:Guidelines" to folder back_up
			delay 300
			
			tell application "Finder" to copy folder "XServe:Server:Admin" to folder back_up
			delay 300
			
			tell application "Finder" to copy folder "XServe:Server:Artwork" to folder back_up
			delay 300
			
			tell application "Finder" to copy folder "XServe:Server:Concept" to folder back_up
			delay 300
			
		end timeout
		
	end if
	
	delay 60
	
end repeat

Model: Xserve
Browser: Safari 533.19.4
Operating System: Mac OS X (10.6)

Hi,

I doubt that the script really worked
there are some problems in these lines


.
set back_up to "Backup:" & new_folder as alias
       
       with timeout of 36000 seconds
           
           tell application "Finder" to copy folder "XServe:Server:Guidelines" to folder back_up
.

the keyword copy does not copy items, it assignes a value (1st operand) to a variable (2nd operand).
The proper Finder command to copy files is duplicate
As the variable back_up is an alias specifier, the destination folder is referenced as folder [alias], which is actually wrong syntax

the best backup solution for a daily backup is rsync, but anyway I recommend to use shell commands.
This script is a shell version of yours, no delays or timeout checks are necessary.
To avoid wasting resources by an endless repeat loop better use a launchd agent which triggers the script every day at 8:30 pm


-- define backup volume path
set backupVolume to "/Volumes/Backup/"
--delete old folder(s)
do shell script "/bin/rm -rf " & quoted form of backupVolume & "Backup*"

-- setup folder paths to be copied
set xserveBaseFolder to "/Volumes/Xserve/Server/"
set sourceDirectories to xserveBaseFolder & "Guidelines " & xserveBaseFolder & "Admin " & xserveBaseFolder & "Artwork " & xserveBaseFolder & "Concept"

-- define destination folder path
set new_folder to backupVolume & "Backup " & short date string of (current date)
-- create new folder
do shell script "/bin/mkdir -p " & quoted form of new_folder
-- do backup
do shell script "/bin/cp -pR " & sourceDirectories & space & quoted form of new_folder

Then add Stefan’s script to ‘iCal’ and run it at what ever time. scheduling options are pretty expansive. and easy to use.

It did and worked fine :slight_smile: until last Friday :frowning: I have no reason to lie about it working.

Anyway, thank you very much for your reply. I will implement your suggestion tonight. Thank you.

Regards

John

Well it could have been working but it wasn’t the way it should be working and considerable lucky. Like stefan said the copy command will create an copy of the object

for example working with script objects like this dummy

script prototype
property x : missing value
end script

setting this script object will only set a reference of that object inot the variable and not a new instance of it

set myScript to prototype --this is a refernce
set anotherScript to prototype --this is a reference

set myScript's x to 1
return anotherScript's x --result: 1

when using the copy command you create a copy of the object to create a new new instance of that script object.

copy prototype to myScript --makes a copy of the object 
copy prototype to anotherScript --makes a copy of the object

set myScript's x to 1
return anotherScript's x --result: missing value

This is very important when working with script objects when to use set or copy command in applescript. Back to your problem is that teh copy command of the finder will be overruled with applescript’s own copy command. Why it didn’t overrule earier is not clear to me.

The answer is use duplicate command instead of copy

Although ‘duplicate’ has been the correct Finder command for the job for many years and ‘copy’ wrong, ‘copy’ did in fact work in this context until a few system versions ago. I was surprised when I tried it just now to find that it didn’t work on my 10.4.11 system. If jontyhunter’s upgraded from a system earlier than that, it’s possible the script was working before. None of the other undesirable stuff in it, tested out of context, seems to be an impediment to it working in 10.6.

Thanks for all your replies.

The original script was created in Mac OS 10.4, though I couldn’t say what the version number was at the time. By the time I upgraded to 10.6.6, the old OS was up to 10.4.11.

I consider myself very ‘lucky’ indeed that it worked for three years!

Anyway, I am currently trialing Stefan’s script but I have a few observations.

The delete old folder line…

--delete old folder(s)
do shell script "/bin/rm -rf " & quoted form of backupVolume & "Backup*"

…doesn’t seem able to delete anything if it comes up against a locked file. When I ran the script last night it stopped at that point

My original script for deleting folders does seem to work ok so I may revert to that.

Also the create new folder shell script…

-- define destination folder path
set new_folder to backupVolume & "Backup " & short date string of (current date)
-- create new folder
do shell script "/bin/mkdir -p " & quoted form of new_folder

…makes a series of nested folders rather than a single one:

Backup 2, then a sub folder ‘10’ then a subfolder ‘11’. All the copied folders are then placed into the final subfolder.

The copying shell script seems to be functioning fine (I am running it as I type) but seems to be taking longer than if I had just used Copy within Finder but that could be my imagination.

Also could running this script cause the xserve to run slightly slower, as some users are experiencing very slight slow down this morning (folders taking a few seconds to open up and show their contents).

Thanks once again for all the replies, I’d forgotten how good this forum is.

@nigel: Like I said in my last line of post :wink:

You can’t use

do shell script " " & quoted form of aVar & anotherVar

try instead

do shell script " " & quoted form of (aVar & anotherVar)

as you can see this way you’ll force applescript to combine the two strings and then make a quoted form of it and not only making quoted form of the first piece of string.

Your second question:
Does backup volume ends with an “/” if not you have to put it between those string. And don’t use -p option with mkdir if you don’t need it. You’re only making a new subdirectory of an existing directory. If existing directory don’t exists there is something wrong and you get an healty error you can catch if you want. The -p option can led into a strange result like you already described.

Well because you used copy it could be possible that the script continued without waiting for the finder to be ready. CP is the simplest and fastest way to copy. To do a simple file copy it’s almost similar to “cat source > destination” (only this is not binary safe and only ascii safe). Ditto in shell is similar to the duplicate command in Finder it preserves the finder info which cp doesn’t. Therefore I use ditto more often than cp commands. When it comes to speed is that the carbon copies (ditto and finder’s duplicate) are slower than the simple binary copies specially when it comes to a large amount of small files.

A slow server can some from that you have a large file archive shared and the client asks the server to calculate all it’s folder sizes. The server cpu usage is low but hdd activity very high. So it seems that it’s not your server that becomes overloaded but the harddrive can’t work that fast so your server is waiting for your hdd.

Thank you,

Though your reply has gone way above my level of knowledge, so am not sure how to take what you have said and adapt Stefan’s script.

I have reverted to my original script to delete the original folder.

And for now I just have to accept that it doesn’t create a folder “Backup 2/10/11” but a nest of folders, because at least it’s backing up the relevant folders OK.

Our xserve went back to normal speed once the script had finished running so it was causing the slow down to some degree, but as this script runs through the night this is of no concern.

Thanks

I didn’t know what your local date format was.
The problem is, the underlying UNIX system regards slashes as path delimiters, so the nested folders are created.
Personally I never use slashes in folder names, in rare cases they could cause problems.
So passing colons as delimiters you get slashes in the Finder view :wink:
This changed version should also delete locked folders


-- define backup volume path
set backupVolume to "/Volumes/Backup/"
--delete old folder(s)
do shell script "cd " & quoted form of backupVolume & ";chflags -R nouchg Backup*; /bin/rm -rf Backup*"

-- setup folder paths to be copied
set xserveBaseFolder to "/Volumes/Xserve/Server/"
set sourceDirectories to xserveBaseFolder & "Guidelines " & xserveBaseFolder & "Admin " & xserveBaseFolder & "Artwork " & xserveBaseFolder & "Concept"

--- define destination folder path
set new_folder to backupVolume & "Backup " & (do shell script "date +%m:%d:%y")
-- create new folder
do shell script "/bin/mkdir -p " & quoted form of new_folder
-- do backup
do shell script "/bin/cp -pR " & sourceDirectories & space & quoted form of new_folder


Sorry that I make it so difficult. well you just replace in your old code, like I said before, the copy command for a duplicate and your script is back online :)…

My difficult answer was also an addition to stefan’s post. It seems that you script is a backup script. I’ve written such scripts in shell myself but I never used cp command because you should never use cp for backup system of OS X servers. It’s saver to make a copy with Finder’s duplicate but the disadvantage you’ll get is that it can only run with the Finder running as well (the user on the server needs to be logged in). Well I have a series of predefined path’s to backup as you do and uses ditto instead for the finder’s duplicate. Then I put my script in launchd so my script will be started automatically.

Here’s how your script could look like (in it most simpelest form):
set backupRootFolder to “/Volumes/ExternalBackupdrive” --change it to your folder

set foldersToBackup to {}
set end of foldersToBackup to “/Server/Guidelines”
set end of foldersToBackup to “/Server/Admin”
set end of foldersToBackup to “/Server/Artwork”
set end of foldersToBackup to “/Server/Concept”
–add lines to backup more folders

set backupFolder to backupRootFolder & “/Backup_” & (do shell script “date "+%Y%m%d"” as string)

do shell script "mkdir " & quoted form of backupFolder
repeat with currentPosixPath in foldersToBackup
do shell script "ditto " & quoted form of currentPosixPath & " " & quoted form of (backupFolder & currentPosixPath)
–you could use a try catch here to print out messages to a log file to see later if everything had worked properly
end repeat

Sorry. I can’t see anything in any of your posts in this thread which says that the Finder’s former ‘copy’ command used to do the same as ‘duplicate’ and that therefore the original script could have worked before it was tried in OS 10.6.

jontyhunter says he was using OS 10.4.11 immediately before the upgrade. Although compiling the script on that system produces a language ‘copy’ command rather than an application one, it’s entirely possible that the underlying token hadn’t been removed at that point and was still functional, in which case, a script compiled on an earlier system would still work in 10.4.11. I’ve tried to revive my old PowerBook (OS 10.2.8) to write a script to test this, but the machine seems to have died in its sleep. :frowning:

The last line of your post, by the way, is like Stefan said in the middle of his. :wink:

Indeed you can and you must while using a * wildcard otherwise the wildcard is not been considered
In the shell it’s allowed to quote any single path component for example


set a to quoted form of "Library"
set b to quoted form of "Application Support"
do shell script "ls " & "/" & a & "/" & b

Why not?

OK, not sure I fully understand all the replies or how it works… :slight_smile:

But it is now working again as it should. :smiley:

Thanks everybody for all your help and assistance.

Regards

jontyhunter

Hello,

OK it’s still not working exactly at it should, as when running the copy files shell script, a lot of files return errors with a permission denied error. It seems to affect files that are locked or open on the server.

Copying files manually by dragging over to the external hard drive doesn’t have the same effect so the Finder’s Copy is obviously more than happy still to copy everything regardless of its open status or locked status.

Any way round this? It’s not affecting that many files thankfully and the script seems to still complete and do everything else (in other words it doesn’t stop at the point it reached the first error).

Thanks

jontyhunter

Files that are open, even with a write flag, can be copied without problems. The copy command opens the source with a read flag. This can be opened as many times as wanted, you can only open a file with write flag once.

Doesn’t the finder ask you for administrator’s credentials? Did you also use DItto instead of CP? To which file systems are you copying?

Well on a server you want to save good copies of your files don’t you? I hate the thought that when a crash happens that not 100% of all my files are usable in my backup.

Thank you.

Yes, I’ll admit this backup solution is lo-tech at best but it does the job for me now.

This is the portion of Stefan’s script I am using that is failing to copy certain files. Last night I made sure there were no Macs connected to the xserve at all so I could be sure there were no open files across the network. It fails to copy certain files with a ‘permission denied’ error.

-- do backup
do shell script "/bin/cp -pR " & sourceDirectories & space & quoted form of new_folder

Yes, admittedly if I do a manual copy of files and/or folders within the Finder it does ask for administrator password to proceed, but my old script that ran fine under Mac OS 10.4 didn’t:


 -- do backup
       set new_folder to {"Backup "} & short date string of (current date) as string
       tell application "Finder" to make new folder at "Backup:" with properties {name:new_folder}
       set back_up to "Backup:" & new_folder as alias
       
       with timeout of 36000 seconds
           
           tell application "Finder" to copy folder "XServe:Server:Guidelines" to folder back_up
           delay 300
           
           tell application "Finder" to copy folder "XServe:Server:Admin" to folder back_up
           delay 300
           
           tell application "Finder" to copy folder "XServe:Server:Artwork" to folder back_up
           delay 300
           
           tell application "Finder" to copy folder "XServe:Server:Concept" to folder back_up
           delay 300.

I’m not too concerned overall at the small amount of files that don’t copy, but it would be nice to sort this out anyway.

Jonty,

If you are using a shell script with no user interaction you should set your script in laucnhd to launch with an interval and run as root (lingon is quite a handy tool for this and there are more). With this settings you don’t have permission problems and you don’t need to be logged into the server to let the script run.

Thank you,

I will investigate lingon.

Jontyhunter

try


do shell script "/bin/cp -pR " & sourceDirectories & space & quoted form of new_folder password "¢¢¢¢" with administrator privileges

administrator privileges executes the shell script as root
password must be an admin password
If you run the script in a non-admin account, add also the user name parameter