django-orchestra-test/scripts/migration/bind9.sh

57 lines
2.1 KiB
Bash
Raw Permalink Normal View History

2014-05-08 16:59:35 +00:00
#!/bin/bash
# GENERATES Python code that will fill your Orchestra database with the existing zone files
#
# DEPENDS on bind9utils
# sudo apt-get install bind9utils
#
# EXAMPLE
# 1) bash bind9-domains.sh /etc/bind/master > bind9-domains.migrate.py
# 2) python manage.py shell < bind9-domains9.migrate.py
ZONE_PATH=${1:-/etc/bind/master/}
2014-10-24 14:19:34 +00:00
ACCOUNT_ID=${2:-1}
2014-05-08 16:59:35 +00:00
echo "from orchestra.apps.domains.models import Domain"
echo "from orchestra.apps.accounts.models import Account"
2014-10-24 14:19:34 +00:00
echo "account = Account.objects.get(pk=${ACCOUNT_ID})"
2014-05-08 16:59:35 +00:00
ERRORS=""
while read name; do
2014-10-17 13:09:56 +00:00
[[ -f $name ]] && {
2014-10-20 10:20:18 +00:00
[[ ! $(echo "$name" | grep '/' > /dev/null ) ]] && name="./${name}"
2014-10-17 13:09:56 +00:00
ZONE_PATH=${name%/*}
name=${name##*/}
}
2014-05-08 16:59:35 +00:00
ZONE=$(named-checkzone -D $name ${ZONE_PATH}/$name)
if [[ $? != 0 ]]; then
ERRORS="${ERRORS} $name"
else
for DOMAIN in $(echo "$ZONE" | awk {'print $1'} | uniq); do
echo "try:"
echo " domain = Domain.objects.get(name='${DOMAIN%?}')"
echo "except:"
echo " domain = Domain.objects.create(name='${DOMAIN%?}', account=account)"
echo ""
RECORDS=$(echo "$ZONE" | grep '\sIN\s' | grep "^${DOMAIN}\s")
echo "$RECORDS" | while read record; do
TYPE=$(echo "$record" | awk {'print $4'})
VALUE=$(echo "$record" | sed "s/.*IN\s[A-Z]*\s*//")
# WARNING This is example code for exclude default records !!
if [[
! ( $TYPE == 'SOA' ) &&
! ( $TYPE == 'MX' && $(echo $VALUE | grep 'pangea.org') ) &&
! ( $TYPE == 'A' && $VALUE == '77.246.179.81' ) &&
! ( $TYPE == 'CNAME' && $VALUE = 'web.pangea.org.' ) &&
! ( $TYPE == 'NS' && $(echo $VALUE | grep 'pangea.org') )
]]; then
echo "domain.records.get_or_create(type='$TYPE', value='$VALUE')"
fi
done
done
fi
done < <(ls $ZONE_PATH)
[[ $ERRORS != "" ]] && echo "Not included due to errors:$ERRORS" >& 2