Here something I quickly hacked to do the job
You need to “pip3 install dnspython” if it’s not already installed in your system.
stsimb@tesla4:~$ cat v-import-dns-zone.py
#!/usr/bin/env python3
# https://forum.hestiacp.com/t/import-dns-zone-files-bind/2373
""" import bind dns zone file to hestia """
import sys
import os
import dns.zone
import dns.ipv4
from dns.exception import DNSException
#from dns.rdataclass import *
from dns.rdatatype import *
if len(sys.argv) != 3:
print ("Usage: %s USER ZONEFILE" % sys.argv[0])
sys.exit(1)
user = sys.argv[1]
zone_file = sys.argv[2]
domain = os.path.basename(zone_file)
try:
zone = dns.zone.from_file(zone_file, domain)
for name, node in zone.nodes.items():
rdatasets = node.rdatasets
for rdataset in rdatasets:
for rdata in rdataset:
if rdataset.rdtype in [A, AAAA]:
print ("v-add-dns-record %s %s %s %s %s '' '' no %s" %
(user, domain, name, rdataset.rdtype.to_text(rdataset.rdtype),
rdata.address, rdataset.ttl))
elif rdataset.rdtype in [CNAME, NS]:
target = str(rdata.target)
if target == '@':
target = domain + '.'
elif target[-1] != '.':
target = target + '.' + domain + '.'
print ("v-add-dns-record %s %s %s %s %s '' '' no %s" %
(user, domain, name, rdataset.rdtype.to_text(rdataset.rdtype),
target, rdataset.ttl))
elif rdataset.rdtype == MX:
exchange = str(rdata.exchange)
if exchange == '@':
exchange = domain + '.'
elif exchange[-1] != '.':
exchange = exchange + '.' + domain + '.'
print ("v-add-dns-record %s %s %s MX %s %s '' no %s" %
(user, domain, name, exchange, rdata.preference, rdataset.ttl))
elif rdataset.rdtype in [TXT, SPF]:
print ("v-add-dns-record %s %s %s %s '%s' '' '' no %s" %
(user, domain, name, rdataset.rdtype.to_text(rdataset.rdtype),
b''.join(rdata.strings).decode('utf-8'), rdataset.ttl))
else:
print ("#skipped %s %s %s" %
(name, rdataset.rdtype.to_text(rdataset.rdtype), rdata))
except DNSException as ex:
print (ex.__class__, ex)
Instructions: suppose you want to import records from the zone file called “mitsos.gr” for hestiacp user mitsos.
v-add-dns-domain mitsos mitsos.gr 1.2.3.4 #1.2.3.4 is the ip of your server
python3 v-import-dns-zone.py mitsos mitsos.gr | tee -a import
bash -x import
rm -f import
This will add the domain in HestiaCP for user mitsos, generate a file called “import” with the commands that will add the corresponding records to hestia, and execute the commands.
Caveat: the default dns template contains about 10 records and the domain serial number increments with each record we import. If the zone file we import contains more than 90 records, then the serial number will be > 100. This is out of range for bind, and the zone will fail to load. You need to manually fix that by editing /usr/local/hestiacp/data/users/$USER/$DOMAIN.conf.
In fact, this is a more general limitation, because of the serial number format and auto-incrementing with every change, we cannot make more than 100 changes in a single day.