LinuxQuestions.org
Visit Jeremy's Blog.
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 04-11-2008, 05:10 AM   #1
GuyWood13
LQ Newbie
 
Registered: Mar 2008
Posts: 11

Rep: Reputation: 0
Script to backup Maildir emails within home areas


Hi

I'm looking for a bash shell, php or perl script to backup all /home/username/Maildir directories (and there content) within the /home/ directory to another folder location such as /backup/username/Maildir. This will enable me to backup emails within my postfix mail server environment.

I've already got an idea of what the program should do and I'm assuming to somebody in the know that it would be easy.

-It needs read the contents of the /home dir to find all the directories that contain a Maildir directory
-then somehow stick all these into an array or something
-then in a loop feed in all the values and run a cp command on top level directory (/home/username) and then a cp on the Maildir directory (and all its contents). Obviously this would have to contain the mkdir command to generate the new folders in the location its backing up to.
-And when all are copied, come out of the loop and end the script

I've never really done any scripting and don't have a scooby doo where to start but with my basic knowledge of programming, I have a rough idea of what is needed.

So if anybody could point me in the direction of some good resources, get me started or even better, just write it for me - that would be absolutely fantastic! Seriously though, any help would be much appreciated.

Thanks
 
Old 04-11-2008, 05:40 AM   #2
blacky_5251
Member
 
Registered: Oct 2004
Location: Adelaide Hills, South Australia
Distribution: RHEL 5&6 CentOS 5, 6 & 7
Posts: 573

Rep: Reputation: 61
I love Linux, if only for the fact that there are always a million ways to do anything.

If you want to use a loop to process each home directory, then you could do something like this:-
Code:
cd /home
for user in *
do
  if [ -d $user/maildir ]  # This user has a maildir...
  then
    cp -r $user/maildir/* /backup/$user/maildir/.
  fi
done
Let me know if you want anything clarified.

There are, as I suggested, other ways.
 
Old 04-11-2008, 07:08 AM   #3
Stéphane Ascoët
Member
 
Registered: Feb 2004
Location: Fleury-les-Aubrais, 120 km south of Paris
Distribution: Devuan, Debian, Mandrake, Freeduc (the one I used to work on), Slackware, MacOS X
Posts: 251

Rep: Reputation: 49
Post

Quote:
Originally Posted by GuyWood13 View Post
-It needs read the contents of the /home dir to find all the directories that contain a Maildir directory
-then somehow stick all these into an array or something
-then in a loop feed in all the values and run a cp command on top level directory (/home/username) and then a cp on the Maildir directory (and all its contents). Obviously this would have to contain the mkdir command to generate the new folders in the location its backing up to.
-And when all are copied, come out of the loop and end the script
I think that the find command, with -exec option is valuable.
 
Old 04-11-2008, 04:44 PM   #4
blacky_5251
Member
 
Registered: Oct 2004
Location: Adelaide Hills, South Australia
Distribution: RHEL 5&6 CentOS 5, 6 & 7
Posts: 573

Rep: Reputation: 61
As Stephane suggests, find will work as well. Like I said there are always many ways to do one thing.
Code:
cd /home
find . -name maildir -type d -exec cp -r {} /backup/. \;
 
Old 04-12-2008, 10:37 AM   #5
GuyWood13
LQ Newbie
 
Registered: Mar 2008
Posts: 11

Original Poster
Rep: Reputation: 0
Hi chaps, thanks for the replies.

I assume this is some none specific kind of script so I just stick it in a file and run it like so

#./filename

?

Excuse my lack of experience
 
Old 04-12-2008, 05:54 PM   #6
blacky_5251
Member
 
Registered: Oct 2004
Location: Adelaide Hills, South Australia
Distribution: RHEL 5&6 CentOS 5, 6 & 7
Posts: 573

Rep: Reputation: 61
Yes, but you also need to set the execution permissions with "chmod 755 filename". When you do "ls -l filename" you need to see "x" in the permission list - e.g. rwxr-xr-x
 
Old 04-17-2008, 06:03 AM   #7
GuyWood13
LQ Newbie
 
Registered: Mar 2008
Posts: 11

Original Poster
Rep: Reputation: 0
Hi blacky, I've tried running this today but its having troubling copying the directory structure as the same folder doesnt exist in the location its backing up to.

I get the following output:

#./backupscript
cp: cannot stat '/backup/dave/': No such file or directory
cp: cannot stat '/backup/guy/': No such file or directory
cp: cannot stat '/backup/john/': No such file or directory
#

I tried fixing this with the adding some mkdir's to the loop but there are many files and it still did something wrong. Is there a workaround for this?
 
Old 04-17-2008, 06:12 AM   #8
blacky_5251
Member
 
Registered: Oct 2004
Location: Adelaide Hills, South Australia
Distribution: RHEL 5&6 CentOS 5, 6 & 7
Posts: 573

Rep: Reputation: 61
If you're using the "for" loop approach, try modifying like this:-
Code:
cd /home
for user in *
do
  if [ -d $user/maildir ]  # This user has a maildir...
  then
    mkdir -p /backup/$user/maildir
    cp -r $user/maildir/* /backup/$user/maildir/.
  fi
done
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
A script to convert maildir to mailbox format i_s Linux - General 7 12-14-2009 03:20 PM
ERR this user has no $home/maildir gooseygander Linux - Newbie 3 12-14-2007 08:49 AM
Script to sort maildir messages dlublink Linux - Software 1 08-10-2006 07:24 PM
Writing a script to backup up /home/username ginda Programming 3 01-05-2006 06:47 AM
Backup Emails in Thunderbird IBall Linux - Software 1 02-17-2004 09:53 AM

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

All times are GMT -5. The time now is 03:57 AM.

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