Rsync script to restore mail accounts
Rsync script to restore mail accounts
Every one need to reduce work load, so we go for scripting. It’s very difficult to rsync manually if you have more 300 + mail accounts with (100 GB).
Here is a simple script you can rsync the mail accounts to remote server.
First you have to install the sshpass on your servers so we can assign the server password on the script otherwise you have to enable SSH_Key gen
Let’s do the sync.
First you have to take all the list of mail accounts on your server to a file /usr/local/src/mail.txt
Eg: info, contact,sales,floki ..etc
In cPanel server goto
cd /home/user_name/mail/domain_name.
You could see something like info, sales etc..
ls -al |awk {‘print $9’}|sed ‘s/.$//’
Copy the result to the file /usr/local/src/mail.txt
FYI : Avoid . .. characters
Script
vim /root/mail_sync.sh
#!bin/bash for i in `cat /usr/local/src/mail.txt`;do sshpass -p 'remote_server_root_password' rsync -avzh /home/user/mail/your_domain/$i/cur/* [email protected]_IP:/home/user/mail/your_domain/$i/cur/ sshpass -p 'remote_server_root_password' rsync -avzh /home/user/mail/your_domain/$i/new/* [email protected]_IP:/home/user/mail/your_domain/$i/new/ sshpass -p 'remote_server_root_password' rsync -avzh /home/user/mail/your_domain/$i/.Sent/new/* [email protected]_IP:/home/user/mail/your_domain/$i/.Sent/new/ sshpass -p 'remote_server_root_password' rsync -avzh /home/user/mail/your_domain/$i/.Sent/cur/* [email protected]_IP:/home/user/mail/your_domain/$i/.Sent/cur/ done
FYI : Add your username & domain name , Remote server IP & it’s Password in the script. If you are using other control panel like virtualmin, the mail Dir may different so correct paths accordingly
chmod 755 /root/mail_sync.sh
Create screen
screen -S mail_sync
Run the script
bash /root/mail_sync.sh