Skip to content

Instantly share code, notes, and snippets.

@martijngastkemper
Last active December 31, 2015 12:39
Show Gist options
  • Save martijngastkemper/7987612 to your computer and use it in GitHub Desktop.
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.
cpanel-mailbox-files
#!/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