Last active
June 19, 2016 10:40
-
-
Save dicbobz/391860f16263d967ffdbfb53c3f3efb1 to your computer and use it in GitHub Desktop.
Backup BIND zone files to S3
This file contains 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/sh | |
# Any copyright is dedicated to the Public Domain. | |
# http://creativecommons.org/publicdomain/zero/1.0/ | |
#### BEGIN CONFIG #### | |
# Make a temp file for list of file to backup | |
# Chroot breaks the ability to just backup the directory | |
OF=$(mktemp /tmp/tar_config.XXXX) || { echo "Can't get temp file"; exit 1; } | |
LIST=$OF | |
#Who to email | |
NOTIFY="some@email.tld" | |
# set dates for backup rotation | |
NOWDATE=`date +%Y-%m-%d-%H` | |
# set backup directory variables | |
SRCDIR='/tmp' | |
DESTDIR='named_config' | |
S3CONF='/root/.s3cfg-file' | |
BUCKET='s3-bucket-name' | |
#commands | |
TARCMD='/bin/tar -czf' | |
TARFAIL='mail -s 'Tar Creation has failed' ${NOTIFY} </dev/null' | |
S3CMD='/usr/bin/s3cmd --mime-type=application/x-gzip -c ${S3CONF}' | |
S3FAIL='mail -a /tmp/named_config-${NOWDATE}.tar.gz -s 'DNS Backup File, s3 put failed' ${NOTIFY} </dev/null' | |
#### END CONFIG ### | |
doBackup(){ | |
# Make a list of files to backup | |
# Chroot breaks the ability to just backup the directory | |
##This for loop looks for reverse zonefiles, zone file for domain, and dns-sec keys wildcards accepted## | |
for i in $(ls -l /var/named/{*.arpa,domain.tld,K*} | awk {'print $9'}); do echo "${i}" >> $LIST;done | |
$TARCMD $SRCDIR/named_config-$NOWDATE.tar.gz -T $LIST || $TARFAIL | |
} | |
putBackup(){ | |
$S3CMD put $SRCDIR/named_config-$NOWDATE.tar.gz s3://$BUCKET/$DESTDIR/ || $S3FAIL | |
} | |
#Not needed for now. Currently using S3 bucket policy for rotation | |
#This nicely removes 1 week old files | |
#delS3Backup(){ | |
# LASTDATE=$(date +%Y-%m-%d-$H --date='1 week ago') | |
# $S3CMD del --recursive s3://$BUCKET/$DESTDIR/named_config-$LASTDATE.tar.gz | |
#} | |
cleanTemp(){ | |
rm -f /tmp/tar_config.*; rm -f /tmp/named_config-* | |
} | |
#doBackup && putBackup && delS3Backup && cleanTemp | |
doBackup && putBackup && cleanTemp |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment