The macosxhints Forums

The macosxhints Forums (http://hintsforums.macworld.com/index.php)
-   Applications (http://hintsforums.macworld.com/forumdisplay.php?f=5)
-   -   Mini-Hint: Automate backups to gmail using applescript (http://hintsforums.macworld.com/showthread.php?t=31163)

minton 11-26-2004 05:13 AM

Mini-Hint: Automate backups to gmail using applescript
 
I got a gmail account a week ago and was surprised to see the notification in the upper right that I now had pop access. Now to put that 1 gb of storage to work, I decided to set up mail.app to automatically archive and backup a few of my directories. Make sure you activate pop in your gmail preferences, and optionally setup mail.app for gmail (only necessary if, like me, gmail is your only ticket into the world of pop mail).
Here's the applescript to backup the Documents and Texts folders in my home directory:

Code:

do shell script "cd ~;tar -czf Backup/`date +%Y-%m-%d`.tgz Documents Texts"
set p to do shell script "ls -t1 ~/Backup |head -1"
set target_file to "/Users/username/Backup/" & p
tell application "Mail"
        set newMessage to make new outgoing message with properties ¬
                {subject:target_file, content:"Whatever reminder you want inside the message"}
       
        tell newMessage
                make new to recipient at beginning of to recipients with properties ¬
                        {address:"user@gmail.com"}
               
                tell content
                        make new attachment with properties {file name:target_file} ¬
                                at after the last word of the last paragraph
                end tell
        end tell
        send newMessage
end tell

It archives and zips 2 directories under the home directory called Documents and Texts to the ~/Backup directory - note how the filename of the created archive includes the date. The "ls ... | head" command is just a way to get the last modified file in the directory, which would be the zipped archive we just created. (The unix part of the script is inelegant and messy). It then makes a new email message with the archive as an attachment and sends it to your gmail account. Save the script as an application and you just need to double click it any time you want to backup your directories.

Obviously, to make this work as an online backup system, you'll have to select the "leave messages on server" option in your gmail settings. (You can always create a separate gmail account to be used solely for backups). Also, it's rumored that the attachment limit for gmail is 10megs per file, so it's probable that you'll have to modify the script to send multiple attachments if they are > 10 megs. It's a good idea to setup a filter to have your backups automatically stored in their own "folder" outside of the inbox. If you need to restore your files, just download the archive, cd to where you want to expand the archive (only cd to the original directories' parent directory - ~ in this example - if you want to overwrite the existing directories) and use "tar -zxvf /path/to/file" :
tar -zxvf /Users/karl/Desktop/2004-11-16.tgz

GlowingApple 12-08-2004 03:35 AM

Droplet
 
1 Attachment(s)
Wow, great hint. I love it, thanks! So here's a version of the same script but modified to allow for dropping of files.

spakers 01-09-2005 04:17 PM

G-Mail backup script question
 
1 Attachment(s)
good thinking- would this script work if i substituted "entourage" for "mail"- (don't even wan't to look at apple mail if i don't have to-too many bad memories)
I think I am gonna to make a folder action using this script- gonna attach it to this fancy icon I scrapped and put it on my Desktop-
- A make-shift G-mail drive for mac OS X!!! - (now if i could just make a "retrieve contents of drafts folder script)

GlowingApple 01-09-2005 09:27 PM

I'm sure it would. You'd just have to substute the lines that compose the mail message with those that do in Entourage. The way the script works is just creates a new mail message and adds an attachment to it. I'm not sure how scriptable Entourage is (Microsoft apps have been notoriously bad at including good scripting support), and since I don't use Entourage I have no way of testing it. You could look in the library for Entourage through Script Editor to look for similar commands in Entourage and just use the same variables.

spakers 01-09-2005 09:32 PM

Danke
 
1 Attachment(s)
Worked pretty good after some adjustments- didn't try to configure for entourage, just made it so I didn't have to look at mail (made it quit when it was done)- for small files I barely had to see it at all!





Wait there is more! - THE GMAIL DRIVE FOR OS X is here! (sorta)

MBHockey 02-09-2005 04:16 PM

Great! THanks so much!

davidw 04-17-2005 10:25 PM

I just made a few modifications for myself, and now its awsome.

1)
When I double click it dosent ask me if I want to launch Gmail and it goes directly to the label in which I store all my data.
I did this by control clicking my label that they are sent to and hiting open in new window. I copied that link and put it in the open location part of the script
2)
I added support to a prefix on the file. So when I drag a file over to it it asks for the title.
Then adds it to the front of the subject
Code:

tell application "Safari"
        open location "http://gmail.google.com/gmail"
end tell



property username : "" --gmail username
property newline : (ASCII character 10) --character for new line

on open of these_items
        --calls send_message with the list of files gotten as a droplet
        send_message(these_items)
end open

--accepts a list of files
on send_message(target_files)
        set theResult to display dialog "Title:" default answer ""
        set theSubject to text returned of theResult
        tell application "Mail"
               
                --combine filenames of all files into one string for subject
                set n to 1
                set filenames to ""
                repeat until n is the ((length of target_files) + 1)
                        set n_file to item n of target_files
                        if n is less than (length of target_files) then
                                set filenames to filenames & POSIX path of n_file & ", "
                        else
                                set filenames to filenames & POSIX path of n_file
                        end if
                        set n to n + 1
                end repeat
               
                --set message properties
                set newMessage to make new outgoing message with properties ¬
                        {subject:theSubject & ": " & filenames, content:newline}
               
                tell newMessage
                        --message should be sent to self
                        make new to recipient at beginning of to recipients with properties ¬
                                {address:username & "@gmail.com"}
                       
                        --add attachments from the list               
                        set n to 1
                        tell content
                                repeat until n is the ((length of target_files) + 1)
                                        set n_file to item n of target_files
                                        set filepath to POSIX path of n_file
                                        make new attachment with properties {file name:filepath} ¬
                                                at after the last word of the last paragraph
                                        set n to n + 1
                                end repeat
                        end tell
                end tell
               
                --send the message
                send newMessage
               
        end tell
end send_message


RTD 05-14-2005 02:17 PM

Tigered, and...
 
hmmm, I´m a newbie and not familiar with scripting,
I love to use this script, but I have problems to work with it.
Sometimes it works, sometimes not.
I´m running now TIGER and mail and maybe it has to do with that.
By dragging a file to the script an attachment would be applied to a new mail and mail will be instantly quit. Thats the normal way. But sometimes only mail will be opened and closed but nothing happens.
Is that right that i have to open mail after scripting the files to send everything?

Thx for reply. :(

GlowingApple 05-20-2005 04:07 PM

I've been getting some comments and ideas from a few people and have some ideas of my own to work in. Now that school is over for the summer I will have to play around with the code some more and see what I can do.


Quote:

Originally Posted by RTD
I´m running now TIGER and mail and maybe it has to do with that.
By dragging a file to the script an attachment would be applied to a new mail and mail will be instantly quit. Thats the normal way. But sometimes only mail will be opened and closed but nothing happens.
Is that right that i have to open mail after scripting the files to send everything?

I have Tiger and will be testing the script on Tiger, so I'll let you know what I find. The script should open Mail.app for you before it sends, but there could be some scripting changes for Mail.app in Tiger.

RTD 05-21-2005 05:24 AM

:o Thx, let me know.

richardyates 05-26-2005 09:16 AM

The only problem with the script that I'm having using Tiger is that it causes mail to quit before some of the messages have had time to send. I don't know anything about scripting (I've had my mac less than a month), but if it could be rewritten so that mail checked to make sure all messages were sent before closing, it would make it just about perfect - its already very good:-)

davidw 05-28-2005 04:08 PM

yea, im on 10.3.8
and it quits before it has time to send
I looked through the mail dictionary and found nothing that would return if it is done sending or not.

Maddoktor2 06-01-2005 11:58 AM

The premature Mail shutdown is easily fixed by removing this line of code at the end of the script:
Code:

tell application "Mail" to quit
The downside is you will now have to shut Mail down manually.

HTH :)

richardyates 06-01-2005 01:14 PM

Thanks - that's exactly what I did, it would just be nice to have it do it automatically.

Only other problem I've had with it is that the script keeps forgetting the gmail drive icon and reverting to a script icon - hardly the end of the world although it woiuld be nice to fix it once and for all

Thanks for the help, lots of what I've read on here has helped me in my move from XP

powderhound 06-02-2005 01:43 PM

avoid premature Mail shutdown
 
Quote:

Originally Posted by Maddoktor2
The premature Mail shutdown is easily fixed by removing this line of code at the end of the script:
Code:

tell application "Mail" to quit
The downside is you will now have to shut Mail down manually.

HTH :)

You can avoid the premature quitting of Mail by adding a line that tells the script to delay for a certain time (number of seconds). It works with a delay of only 1 second, even for multiple files. The end of the script should therefore be:

end tell
delay 1
tell application "Mail" to quit
end send_message

powderhound 06-04-2005 01:08 PM

An important addendum to my fix for premature quitting:

Depending upon the speed of your internet connection, you may have to increase the delay. For example, at work, I need only 1 second. However, at home (with average upload of 300-400 kbits/sec), I have increased the delay to 5 seconds.

davidw 06-05-2005 10:18 PM

use time. I never thought of that.
But what if your on slow internet at about 3 kb per second
if you could get the total size of the email in kb and divide that by your transferspeed you could get the correct delay for large and small files.

davidw 06-05-2005 10:22 PM

Quote:

Only other problem I've had with it is that the script keeps forgetting the gmail drive icon and reverting to a script icon - hardly the end of the world although it woiuld be nice to fix it once and for all
it did the same thing to me.
Do this.
First copy the folder that has the gmail drive icon to somewhere on your computer. like the documents folder or something. Then copy the icon from it and paste it on the gmail drive apple script. It should stay on now... well it at least worked for me.

euskir 08-21-2005 09:18 PM

A good one.
 
A good one is to use Disk Utility to compress and encrypt a folder or file, then you can attach that to a "fake" mail. It's working ok for me right now.

richardyates 08-31-2005 10:17 AM

Quote:

Originally Posted by davidw
it did the same thing to me.
Do this.
First copy the folder that has the gmail drive icon to somewhere on your computer. like the documents folder or something. Then copy the icon from it and paste it on the gmail drive apple script. It should stay on now... well it at least worked for me.

Thanks - did here too

chinarut 10-17-2005 03:56 AM

Nice to see the power of AppleScript at work here.

I came across gDisk just now - I haven't had a chance to take a look at the source code and see how the Python library is used but for those of you interested in merging the code above with this one:

http://gdisk.sourceforge.net/

still a work in progress but definitely a start!

WinterDarkmoon 02-26-2006 11:30 PM

Is this sort of thing possible for OS 9? I'm trying to upgrade to OS X, but don't have sufficient space to store files that I want backed up. Nor do I have a cd burner to burn the files to cd. So it's either lose the files or use gmail. ^_^ It'd be really great if this could be used with OS 9!

~ufo~ 08-04-2006 06:30 AM

Quote:

Originally Posted by chinarut
Nice to see the power of AppleScript at work here.

I came across gDisk just now - I haven't had a chance to take a look at the source code and see how the Python library is used but for those of you interested in merging the code above with this one:

http://gdisk.sourceforge.net/

still a work in progress but definitely a start!

looks like a nice app, but definitely a work in progress...

thing keeps quitting on me upon submitting my login data:rolleyes:

davidw 01-17-2007 05:03 PM

Entourage Version
 
1 Attachment(s)
Here is a version of the script modified to work with Entourage


All times are GMT -5. The time now is 10:49 AM.

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.