![]() |
Tar and AFP copy a folder of random name?
I'm trying to write script to do the following (and will make an AppleScript droplet or app if that ends up being easier, I'm pretty well versed with applescript).
Take a typed in name as an argument, tar the directories in the specified folder into their own .tar file named based on the folder name and the argument, then upload them to an AFP share. Example: Folder containing: Backup 1 User 8 Backup 4 Archive 7 Run the script, it asks for a name, you give it "jd". Results in "Backup 1 - jd.tar", "User 8 - jd.tar", "Backup 4 - jd.tar", "Archive 7 - jd.tar". Then uploads the .tar files to afp://macpro.local./archive/ I could probably do this with a lot of work in AppleScript, but I would think a shell script would be cleaner. |
Something like the following should work: (completely untested)
Code:
#!/bin/bash |
Hayne: You wrote $filename once where you meant $foldername, and you left out a whole bunch of quotes.
(still completely untested) (still doesn't upload the tar files as requested) Code:
#!/bin/bash |
in applescript this would be something like the following, I think:
Code:
set baseFolder to "/path/to/basefolder/"
|
If you look at the curl command it will allow you to copy files from remote hosts or to remote hosts of HTTP/S and S/FTP. I don't have any way of testing it short of setting up a FTP server and build a test environment from scratch, but that is where I would start.
|
Quote:
Code:
...You're also mixing Posix paths with HFS paths. How about (still untested)? Code:
|
Quote:
Quote:
|
Quote:
But yeah, I've been at this long enough to know how elusive perfection can be. I was just taken aback to see quoting omissions in the post immediately following my post lamenting quoting omissions. :o Quote:
For an example of a difficult case, suppose one of the folders we're trying to tar has the name "Version 2010/12/5". Those slashes are perfectly legal in an HFS filename. In a POSIX path, though, the slash character is reserved for the path separator, so: set baseFolder to "/path/to/basefolder/" set theRootName to "bv" -- for example ... set thisFolderName to "Version 2010/12/5" -- implicitly by repeat with ... in subFolders set newName to thisFolderName & " - " & theRootName & ".tar" do shell script ("tar -cvf " & newName & " " & baseFolder & thisFolderName) What you pass to do shell script is: tar -cvf Version 2010/12/5 - bv.tar /path/to/basefolder/Version 2010/12/5 and no amount of quoting can get around the fact that some of those slashes are unix path separators, and some are from the folder's HFS name. One of the things that "POSIX path of ..." does is to swap "/" (the POSIX path separator) with ":" (the HFS path separator). The shell script should have read: tar -cvf 'Version 2010:12:5 - bv.tar' '/path/to/basefolder/Version 2010:12:5' but there's no way to get there without being scrupulously careful about which strings are POSIX paths and which are HFS paths. Concatenating them willy-nilly has bad results. |
Quote:
Quote:
|
ok, in that case, we'd probably want to rewrite the center part of the script like so. I think this takes care of the problematic cases:
Code:
tell application "System Events" |
Quote:
set newPath to rootPath & "/" & fileName & " - " & theRootName & ".tar" My version gets it all right, by staying with the HFS paths that AppleScript, Finder, System Events, and pretty much everybody except the shell is happy with, right up until I need to pass paths to the shell. It's only in do shell script that I need to even mention POSIX (or quoted form of ..., for that matter). Although... Working with POSIX paths throughout isn't so bad, once you realize there's no need to parse them (for this problem): Code:
set basefolder to (path to home folder as text) & "Junk_:xx2:" |
Quote:
|
Hahaha, I had forgotten how funny some of your guys' threads could get (I haven't posted regularly here for years).
For some reason the forum didn't email me on replies, so I haven't looked at the thread until just now. edit: I had forgotten my email address was an old non-existant one... whoops, fixed. You can switch between posix paths and finder/applescript aliases, but the code I had to do that is at home, and I'm at work. Here's the take on it I threw together in applescript last weekend. It handles spaces in the source. For the record, there won't ever be a /, :, or \ in the original folder names. I know its ugly, and I ran into some weird overflow, I really need to find my old code snippits collection... Code:
on open droppedFiles |
Cleaner Code
Here's my cleaner attempt at it. It mostly works, except I'm running into an issue (which I remember from years ago but am forgetting how to deal with it), where when doing the finder copy at the end, the loop tries to continue.
Anyone know how to stall the end by checking for file busy or done? Code:
property quoteChar : (ASCII character 34) |
I'm not sure what you mean by "the loop tries to continue", but you don't actually need that last repeat loop anyway - the Finder will copy lists just fine. here's a tweaked version of what you gave (with a couple of other stylistic changes -combining the first two finder tells, removing the variable 'currentDone', etc - just because):
Code:
property quoteChar : (ASCII character 34) |
Well that makes life easier, thank you. I've never tried to copy a list before, always just individual files.
The excess variables and finder statements are mainly due to the way I build and test pieces of AppleScript at a time. Once it all works I'll usually do a full cleanup and consolidation. ;) To clarify my earlier statement, what I mean is this... Code:
repeat with eachFile in compressedFiles |
Quote:
Code:
repeat with i from 1 to count of compressedFiles |
Code:
tell application "Finder"Back at it, but you've given me more ideas at least. |
Nope, same <<class cdis>> error as trying to copy the entire list at once.
Quote:
|
sorry, I wasn't paying attention - Copy means something other than you think it does to the Finder, and doesn't work anyway. Use duplicate instead:
Code:
tell application "Finder" |
*facepalm* Of course. Not the first time that has bitten me either.
|
Quote:
|
More fun than trying to duplicate this functionality on the windows machines at my shop.
Quote:
|
Well it works, usually. I'm still trying to find what is breaking it. Sometimes (seemingly right after the shell scripts), I get an apple event error and it all stops. I believe its actually timing out after a large (>50GB) directory to tar, but I'm not sure since it doesn't say time out.
I'm about ready to re-write this in x-code so I can setup breakpoints, as it is I have put alerts at various points to check flow, but it isn't ideal. |
Quote:
Code:
with timeout of 3600 seconds -- one hour - don't worry, it will exit earlier if it can |
I remember that now. Though none of the old code I brought with me today has it. Some of these things could run for hours (multiples of >200GB folders).
I'll give it a shot for troubleshooting. |
well, if they could really run for hours (yikes) you might want to rewrite this as a stay-open application: basically fire off the tar command as a subprocess then use the idle loop to periodically check to see if the subprocess is complete. that's a more convoluted script (you'll have to recover and store the process id of the tar process and check for it in the idle loop, and if it's more efficient you might launch multiple subprocess and wait for them all to be done), but at least you'll avoid having that frozen script thing you get when you run long tasks as simple scripts.
|
Yay rewrites! They literally can run for hours.
Just a high level overview, this project is for a tech shop I do work for that does quite a bit of data recovery. The goal is that once a recovery is done (typical is 80GB, but we're seeing more an more multiple hundred GB ones), to tar up the recovered data, put it on our server for safekeeping until pickup, and clear the space off the tech's workstation's drives so they can keep working. This is something we'd run overnight, and I wouldn't be surprised to see it take hours. Thanks for everyone's help and advice so far, by the way. I really appreciate it. |
And, just for fun, I tried the timeout thing. Same error (and now I've also seen it happen within 5 minutes, so probably not a timing thing).
|
Quote:
|
Quote:
The point I was trying to make was that, although you can keep switching back and forth between POSIX paths and HFS paths, the constant switching makes your code both harder to read and more error-prone. It's too easy to get the path separators mixed up and/or duplicated and/or omitted, too easy to forget which variables currently hold POSIX paths, which hold HFS paths, and which hold actual files/folders/aliases/URLs. The constant repetition of the "POSIX path of" mantra clutters up the code, making it less readable. As foreign as HFS paths may seem to people more comfortable at the command line, your AppleScript code is cleaner if you use them. When in Rome, speak Latin. When in AppleScript, speak AppleScriptese. Translate to POSIX when you need to go to POSIX-land, but translating prematurely just makes things more complicated. And, how do you say Piña Colada in Spanish? |
Quote:
|
By way of example, consider:
Create a test file with, for example: date > ~/Desktop/Summary\ for\ 12:25:2010.txt and then run: Code:
tell application "Finder"And if you even dream of playing games with text item delimiters and manipulating path separators explicitly, you're missing the point. |
I haven't been able to test again until this weekend. It is throwing "AppleEvent handler failed."
It appears to between the end of the file getting tarred, and the beginning of the finder copy. Usually the error comes up when the .tar.gz file is its final size, but the finder copy never starts. Quote:
|
Quote:
|
Logic eludes me when I'm banging my head on a brick wall sometimes...
Archive is a local volume on the machine I'm testing on, it should always be true. I'll loop the volume check in another script and see what happens. |
I'll also put it out there that the same error occurs on 3 machines. 1 running 10.6.8 and the rest running 10.6.7. Archive is a local disk on mine, the other two have to mount it over the network.
|
Quote:
Quote:
Code:
tell application "Finder" |
That is something I never knew, and might help with troubleshooting.
Also, I just realized I never posted the current code. That tell block is now: Code:
tell application "Finder"Quote:
|
Quote:
It's more complex than that, obviously - you often can use SA commands inside app tell blocks - but I don't know where or why it works when it does, so I take the conservative route and try not to use osax commands in tell blocks. |
That's a lot of good info. I haven't really used AppleScript since 10.5. I made a few adjustments to the script and am testing it again (unfortunately my test file is 300GB, so it takes a bit to fail).
|
And it works, finally.
Code:
Thanks everyone. Now I get to learn how to do essentially the same thing on a Windows machine, as well as find a good way to make .tgz files on windows. |
Ack! Just did a 1MB test file for a "how to" video for my co-workers, and it threw an error again. Back to the drawing board.
|
| All times are GMT -5. The time now is 06:01 PM. |
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.