Last active
December 31, 2015 12:39
-
-
Save martijngastkemper/7987612 to your computer and use it in GitHub Desktop.
Backup and restore cPanel e-mail accounts by domain. You can use this script to move e-mailboxes between cPanel accounts.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cpanel-mailbox-files |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
read -p "Do want to backup or restore? " ACTION | |
read -p "cPanel username? " USER | |
read -p "Which domain? " DOMAIN | |
HOMEDIR=/home/$USER | |
if [ ! -d "$HOMEDIR" ] | |
then | |
echo "User doesn't exists." | |
exit 1 | |
fi | |
BACKUPDIR=`pwd`/cpanel-mailbox-files | |
mkdir -p $BACKUPDIR | |
if [ "$ACTION" = "backup" ]; | |
then | |
pushd $HOMEDIR | |
tar -cf $BACKUPDIR/$DOMAIN.tar mail/$DOMAIN etc/$DOMAIN | |
popd | |
pushd /etc/valiases | |
tar -cf $BACKUPDIR/$DOMAIN-aliases.tar $DOMAIN | |
popd | |
echo "Backup e-mail accounts for $DOMAIN is ready." | |
elif [ "$ACTION" = "restore" ]; | |
then | |
pushd $HOMEDIR | |
tar -xf $BACKUPDIR/$DOMAIN.tar | |
chown -R $USER:$USER mail/$DOMAIN | |
chown -R $USER:mail etc/$DOMAIN | |
popd | |
pushd /etc/valiases | |
tar -xf $BACKUPDIR/$DOMAIN-aliases.tar | |
chown $USER:mail $DOMAIN | |
popd | |
echo "Restoring e-mail accounts for $DOMAIN is ready." | |
else | |
echo "Please provide an action (backup,restore)" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment