LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-03-2010, 11:00 AM   #1
ifeatu
Member
 
Registered: Sep 2008
Distribution: Fedora 9
Posts: 68

Rep: Reputation: 15
Automate backup of Google Apps accounts


Hello,

I am trying to write a script that automates the backup of Google Apps accounts

say I have the following 5 users:

bob
sue
anthony
james
carl

I want to run getmail to an mbox file for each of them....

here is my getmail configuration:

Code:
retriever]
type = SimplePOP3SSLRetriever
server = pop.goo.com
username = bob@goo.com
password =blah123

[destination]
type = Mboxrd
path = ~/gmail-archive/gmail-backup.mbox

[options]
# print messages about each action (verbose = 2)
# Other options:
# 0 prints only warnings and errors
# 1 prints messages about retrieving and deleting messages only
verbose = 2
message_log = ~/.getmail/gmail.log
get mail runs great for a single user but I want to automate this process so I'm trying to create a script that creates a different file for each user:

Here is my thought process:

1) Search the file above (getmail.gmail) for the first '@' symbol and replace whatever comes right before it with 'bob' (or the first element in an array perhaps)

2) create new mbox file for email storage perhaps using name for convention

3) Run getmail

4) After gmailmail is finished, repeat.

I can't figure out how to do step one and two! Find an replace name@goo.com to name2@goo.com and find and replace path = ~/gmail-archive/name1.mbox with * name2.mbox

Code:
#/home/bin/bash

names= (bob sue anthony james carl)

i=count names

for (x=0, x<i, x++)
{
# Script that automates email backup 
#sed -i 's/????????????/names[x] ~/.getmail/getmail.gmail

#Make new mbox file for user
#sed -i 's/????????????/names[x] ~/.getmail/getmail.gmail


getmail -r /home/pmalbrough/.getmail/getmail.gmail
}
 
Old 02-03-2010, 11:39 AM   #2
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 167Reputation: 167
Could do something like this (although a bit overly simplified)-- make a template for each user with values that are consistent but easily replaced... like template@dom.com and template then run the script:

config:
Code:
[retriever]
type = SimplePOP3SSLRetriever
server = pop.dom.com
username = template@dom.com
password = blah123

[destination]
type = Mboxrd
path = /home/template/gmail-archive/gmail-backup.mbox

[options]
# print messages about each action (verbose = 2)
# Other options:
# 0 prints only warnings and errors
# 1 prints messages about retrieving and deleting messages only
verbose = 2
message_log = /home/template/.getmail/gmail.log
customization script (just doing echos so you can test to see if output looks right):
Code:
#!/bin/bash
names="bob@dom.com sue@dom.com anthony@dom.com james@dom.com carl@dom.com"
for i in $names; do
  echo "mkdir -p /home/`echo $i | cut -f1 -d\@`/gmail-archive"
  echo "sed -i 's/template@dom.com/$i/g' /home/`echo $i | cut -f1 -d\@`/.getmail/getmail.gmail"
  echo "sed -i 's/template/`echo $i | cut -f1 -d\@`/g' /home/`echo $i | cut -f1 -d\@`/.getmail/getmail.gmail"
  echo "getmail -r /home/`echo $i | cut -f1 -d\@`/.getmail/getmail.gmail"
done
Then I'd just cron something like:
0 * * * * /usr/local/sbin/archiver.sh

Last edited by rweaver; 02-03-2010 at 11:48 AM.
 
Old 02-03-2010, 12:54 PM   #3
ifeatu
Member
 
Registered: Sep 2008
Distribution: Fedora 9
Posts: 68

Original Poster
Rep: Reputation: 15
Can I assign a file to the variable? A file with a list of names

e.g

bob
jill
samson
jack
susie
 
Old 02-03-2010, 01:10 PM   #4
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 167Reputation: 167
sure:

Code:
core:~/test/test27$ cat test2.sh
#!/bin/bash
for i in $(cat ./names); do
  echo "Name: $i"
done
core:~/test/test27$ cat names
bob@dom.com
sue@dom.com
anthony@dom.com
james@dom.com
carl@dom.com
core:~/test/test27$ ./test2.sh
Name: bob@dom.com
Name: sue@dom.com
Name: anthony@dom.com
Name: james@dom.com
Name: carl@dom.com
core:~/test/test27$
or

Code:
VAR=$(cat /list-of-stuff)
(which in the above script would look like)

Code:
#!/bin/bash
VAR=$(cat ./names)
for i in $VAR; do
  echo "Name: $i"
done
If you really wanted to cheat you could just fully generate the file on the fly from something like emailaddr:homedirassword then use cut to fish the elements out, although that is NOT the most secure way to do things since you're storing passwords, etc.

setup.list
Code:
bob@bob.net:billybob:fredfredburger
fred@fred.net:freddy:bobbobburger
Code:
#!/bin/bash
for i in $(./setup.list); do
  EM=$(echo $i | cut -f1 -d:)
  UN=$(echo $i | cut -f2 -d:)
  PW=$(echo $i | cut -f3 -d:)
  echo "Email Address: $EM, Username: $UN, Password: $PW"
done
(non-encrypted passwords are bad to store)

Last edited by rweaver; 02-03-2010 at 01:19 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Transfer Postfix accounts to Google Apps Email. Is this possible? christopher_c Linux - Newbie 0 06-03-2009 11:40 AM
Automate backup when a particular thumb drive is plugged in duryodhan Slackware 7 09-13-2008 11:13 AM
LXer: Speaking Unix, Part 6: Automate, automate, automate! LXer Syndicated Linux News 0 01-04-2007 09:54 AM
cannot automate rsync backup jahjr Linux - Server 6 12-21-2006 04:54 AM
automate database backup Stormproof Linux - Networking 1 10-19-2004 03:19 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:00 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration