LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Automate backup of Google Apps accounts (https://www.linuxquestions.org/questions/programming-9/automate-backup-of-google-apps-accounts-786710/)

ifeatu 02-03-2010 11:00 AM

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
}


rweaver 02-03-2010 11:39 AM

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

ifeatu 02-03-2010 12:54 PM

Can I assign a file to the variable? A file with a list of names

e.g

bob
jill
samson
jack
susie

rweaver 02-03-2010 01:10 PM

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:homedir:password 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)


All times are GMT -5. The time now is 03:08 PM.