#!/bin/bash
#
# vmail-stack
# https://git.stack-source.com/msb/vmail-stack
# Copyright (c) 2023 Matthew Saunders Brown <matthewsaundersbrown@gmail.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

# load include file
source $(dirname $0)/vmail.sh

help()
{
  thisfilename=$(basename -- "$0")
  echo "Create archive of vmail domain on this server. Includes emails and all email settings."
  echo ""
  echo "usage: $thisfilename -d <domain> [OPTIONS]"
  echo ""
  echo "  -h            Print this help."
  echo "  -d <domain>   Domain name of vmail domain to create archive for."
  exit
}

vmail:getoptions "$@"

# check for domain
if [[ -z $domain ]]; then
  echo "domain is required"
  exit
fi

if [[ ! -d /var/vmail/$domain ]]; then
  echo "vmail for $domain does not exist"
  exit 1
fi

# create export of vmail settings
/usr/local/bin/vmail-domains-exp.sh -d $domain

# make sure archive dir exists
if [[ ! -d /opt/archives ]]; then
  mkdir /opt/archives
fi

# create tarball
tar --directory=/var/vmail/ -czf /opt/archives/mail.$domain.tar.gz $domain