# Copy script



## Sylhouette (Nov 15, 2010)

Hello all, I am a little ashamed to ask this, but my scripting power is very bad.

I have inherited a mail server, and it has the maildir on another location than we used to have.

Our structure looks like the following.


```
/usr/local/domain1/users/Maildir
/usr/local/domain2/users/Maildir
```

The server I inherited has it almost like this.

Only this way 


```
/usr/local/domain1/users
/usr/local/domain2/users
```

Now I want a little copy script that creates the Maildir folder in the /usr/local/domain1/user folder and then copy everything from the /usr/local/domain/user folder to the created Maildir folder.

I have tried some options, but it does not run through all the username folders, and sometimes it stops at the different domains.

There are in total 11 domain folders, and each domain has about 400 to 500 user folders.

Thanks for your time.

Gr
Johan


----------



## wblock@ (Nov 15, 2010)

net/rsync will do this without a script.  cp(1) will too, but rsync is smarter.


----------



## AndyUKG (Nov 15, 2010)

I would have thought a "for" command can cope with 500 files/directories. Use at your own risk, but something like this should work Id think:


```
#!/usr/local/bin/bash

TMPDIR=/usr/local/thisisatempdir
if ! mkdir $TMPDIR
  then
  echo $TMPDIR already exists! exiting!
  exit
fi

for DOMAIN in /usr/local/*
do

  for USER in "$DOMAIN"/*
  do
  # make sure we dont move maildirs that are already in the correct format
  BASENAME=`basename "$USER"`
  if [ "$BASENAME" != "Maildir" ]
    then
    mv "$USER" "$TMPDIR"/Maildir
    mkdir "$USER"
    mv "$TMPDIR"/Maildir "$USER"/Maildir
  fi
  done
done

rmdir $TMPDIR
```

Prefix the mv and mkdir commands with "echo" for testing before running this,

ta Andy.


----------



## Sylhouette (Nov 16, 2010)

Thanks for the script.

/usr/local/domain was /usr/local/virtual, so i changed that

If i do an echo, it all seems good.
Only the first one with an echo is a *
And it can not copy or remove that.

Here the first part with echo in front.

```
mv /usr/local/virtual/thisisatempdir/* /usr/local/virtual/thisisatempdir/Maildir
mkdir /usr/local/virtual/thisisatempdir/*
mv /usr/local/virtual/thisisatempdir/Maildir /usr/local/virtual/thisisatempdir/*/Maildir
mv /usr/local/virtual/domain1/corrie /usr/local/virtual/thisisatempdir/Maildir
mkdir /usr/local/virtual/domain1/corrie
mv /usr/local/virtual/thisisatempdir/Maildir /usr/local/virtual/domain1/corrie/Maildir
mv /usr/local/virtual/htcnl.com/guusje /usr/local/virtual/thisisatempdir/Maildir
mkdir /usr/local/virtual/domain1/guusje
mv /usr/local/virtual/thisisatempdir/Maildir /usr/local/virtual/domain1/guusje/Maildir
mv /usr/local/virtual/domain1/jan /usr/local/virtual/thisisatempdir/Maildir
mkdir /usr/local/virtual/domain1/jan
mv /usr/local/virtual/thisisatempdir/Maildir /usr/local/virtual/domain1/jan/Maildir
```

If i run it without the echo it stops by the first user. actually the *

i get this 


```
/root/scripts/copy-maildir.sh  | more
mv: rename /usr/local/virtual/thisisatempdir/* to /usr/local/virtual/thisisatempdir/Maildir: No such file or directory
mv: rename /usr/local/virtual/thisisatempdir/Maildir to /usr/local/virtual/thisisatempdir/*/Maildir: No such file or directory
rmdir: /usr/local/virtual/thisisatempdir: Directory not empty
```

I think it has something to do with the wildcard sign not beeing a username!

thanks for your time btw..

regards,
Johan


----------



## AndyUKG (Nov 16, 2010)

Ah, ok cos I put the temp dir in the same place so its getting in a mess, ok try this:


```
#!/usr/local/bin/bash

TMPDIR=/usr/local/thisisatempdir
if ! mkdir $TMPDIR
  then
  echo $TMPDIR already exists! exiting!
  exit
fi

for DOMAIN in /usr/local/*
do

  for USER in "$DOMAIN"/*
  do
  # make sure we dont move maildirs that are already in the correct format
  BASENAME=`basename "$USER"`
  if [ "$BASENAME" != "Maildir" ] && [ -d "$USER" ]
    then
    mv "$USER" "$TMPDIR"/Maildir
    mkdir "$USER"
    mv "$TMPDIR"/Maildir "$USER"/Maildir
  fi
  done
done

rmdir $TMPDIR
```


----------



## Sylhouette (Nov 16, 2010)

Thanks that worked.

I moved the temp dir also out of 'harmsway'

I myself tryed something else also, and that seems to work also.


```
for DOMAIN in `ls /usr/local/virtual/`
```

I used the ls command to set $DOMAIN, found  this in another topic.
That way it seems to work also.
So i thought i could use that for the $USER also 

```
for USER in `ls "$DOMAIN"/`
```

But then all mailboxes got copied to /usr/local/virtual/ without the domein.
Maybe something with the ` and "....

regards,
Johan

And Thank you very much.


----------

