Incron creating multiple database entries

Discussion in 'Programming/Scripts' started by davemcp, Jan 20, 2014.

  1. davemcp

    davemcp New Member

    This has got me stumped. I am updating a powerdns server backend postgres sql database to make powerdns act as a Dynamic DNS server.

    On the client side I have a script that polls hostname and ip address. If the ip address changes I write to a "myip" file and scp it over to the powerdns server.

    On the server I am attempting to use incron to watch my ./updates directory for a new "myip" file.

    The script opens the "myip" file then queries the database to see if the entry exists, inserts a new entry if it doesn't, updates the ip if it's different and exits if they are the same deleting the "myip" file to wait for a new one.

    I have tested the script manually and it all works as expected and only updates or inserts when the entry needs to be changed. When I use incron I get multiple entries in the database.

    the incrontab entry is ~/updates IN_CLOSE_WRITE ~/dbupdate $#

    the script dbupdate is as follows



    while IFS=$'\t' read -r -a client
    echo ${client[0]}
    echo ${client[1]}
    done < "/home/ddns/updates/myip"


    ## Echo the ip and hostname from myip
    echo "the values in myip are ""IP = "$IP "NAME = "$NAME ""

    ## Read values in the database
    echo -e "select content,name from records where name='$NAME'" | psql -t -A pdns | tr "|" " " > tmp.db

    ##place the values in global variables
    while read tip tname; do
    #echo ip=$tip name=$tname
    done < tmp.db

    rm ./tmp.db

    ## Echo the database values
    echo the database values are ip = "$ip" name = "$name"

    if [ "$NAME" != "$name" ]; then
    echo "$NAME" is not the same as "$name"
    echo -e "insert into records (content,ttl,prio,type,domain_id,name) values('$IP','3600','0','A','7','$NAME');" | psql -A -t drdns
    echo "I just created a new database entry"
    echo "The names are the same"
    if [ "$IP" != "$ip" ]; then
    echo "the IPs are not the same"
    echo "update \"public\".\"records\" set \"content\"='$IP' where \"name\"='$NAME'" | psql -A -t pdns
    echo "I will update the existing database entry"
    echo "The IPs are the same"

    echo "I am done running this script I will now delete the myip file"

    rm ~/updates/myip

    Anyone have any idea why this is happening and prevent multiple duplicate entries?


Share This Page