LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Script to backup Maildir emails within home areas (https://www.linuxquestions.org/questions/programming-9/script-to-backup-maildir-emails-within-home-areas-634615/)

GuyWood13 04-11-2008 05:10 AM

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

blacky_5251 04-11-2008 05:40 AM

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.

Stéphane Ascoët 04-11-2008 07:08 AM

Quote:

Originally Posted by GuyWood13 (Post 3117796)
-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.

blacky_5251 04-11-2008 04:44 PM

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/. \;


GuyWood13 04-12-2008 10:37 AM

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

blacky_5251 04-12-2008 05:54 PM

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

GuyWood13 04-17-2008 06:03 AM

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?

blacky_5251 04-17-2008 06:12 AM

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



All times are GMT -5. The time now is 09:53 AM.