ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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:
Quote:
Originally Posted by GuyWood13
-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.
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
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?
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.