├── Licence.md ├── README.md ├── cloudflare ├── tobackup.sh └── tomain.sh └── monit_conf └── cloudflare.conf /Licence.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) [year] [fullname] 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # dns-failover-cloudflare-monit 2 | Setup DNS Failover for Cloudflare with monit 3 | 4 | 5 | ## How does it work ? 6 | 7 | Best configuration requires 3 servers : primary webserver, secondary webserver and monit server. Still works with only two servers. 8 | 9 | Cloudflare DNS is configured to redirect internet traffic to the primary webserver. 10 | 11 | Monit is constantly checking health of primary webserver. 12 | 13 | If primary webserver stops responding to ping requests, monit will launch a script. The script will modifiy Cloudflare DNS entry to redirect traffic to second webserver. 14 | 15 | Once primary webserver responds to ping again, a second script will be started and Cloudflare DNS entry will be updated to redirect traffic to primary webserver. 16 | 17 | 18 | ## Installation 19 | - Move cloudflare directory to root folder 20 | - Create a directory named logs in /root/cloudflare/ 21 | - Replace all variables in tomain.sh and tobackup.sh according to your cloudflare configuration and servers configuration 22 | - edit yourwebsite.com, yourwebsiteip, your.email@domain.tld, http://yourwebsite.com in monit_conf/cloudflare.conf 23 | - move cloudflare.conf into /etc/monit/conf.d 24 | - restart monit 25 | - enjoy 26 | 27 | Full installation instructions in french available [here] (https://www.noobunbox.net/serveur/configurer-un-dns-failover-cloudflare) 28 | 29 | 30 | ## Source 31 | Originally created by [Slayerduck] (http://blog.booru.org/?p=12) and modified by [myself] (https://www.noobunbox.net) to make it work with the lastest [cloudflare API] (https://api.cloudflare.com) (v4) 32 | 33 | 34 | ## Licence 35 | MIT Licence 36 | -------------------------------------------------------------------------------- /cloudflare/tobackup.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | zone_identifier=zone_identifier 4 | record_identifier=record_identifier 5 | auth_key=api_key 6 | auth_email=your_cloudflare_email 7 | ip=backup_ip 8 | record_name=record_name 9 | 10 | 11 | wget -q --tries=1 --timeout=3 http://www.google.com -O /tmp/index.google >> /dev/null 12 | if [ ! -s /tmp/index.google ];then 13 | echo `date` No interwebz? Not switching over! >> /root/cloudflare/switch.log 14 | exit 0 15 | else 16 | #Start check if in backup mode 17 | if [ -f /root/cloudflare/backupactive ]; 18 | then 19 | echo "already in backup mode!" 20 | exit 0 21 | fi 22 | cd /root/cloudflare 23 | 24 | echo "Switching to backup..." 25 | touch /root/cloudflare/backupactive 26 | 27 | curl -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/$record_identifier" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" --data "{\"type\":\"A\",\"name\":\"$record_name\",\"content\":\"$ip\"}" 28 | 29 | 30 | echo `date` going down! >> /root/cloudflare/logs/switch.log 31 | fi 32 | exit 0 33 | -------------------------------------------------------------------------------- /cloudflare/tomain.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | zone_identifier=zone_identifier 4 | record_identifier=record_identifier 5 | auth_key=api_key 6 | auth_email=your_cloudflare_email 7 | ip=main_ip 8 | record_name=record_name 9 | 10 | if [ ! -f /root/cloudflare/backupactive ]; 11 | then 12 | echo "Server is not in backup mode, can't switch back to main" && exit 0 13 | fi 14 | cd /root/cloudflare 15 | 16 | curl -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_identifier/dns_records/$record_identifier" -H "X-Auth-Email: $auth_email" -H "X-Auth-Key: $auth_key" -H "Content-Type: application/json" --data "{\"type\":\"A\",\"name\":\"$record_name\",\"content\":\"$ip\"}" 17 | 18 | 19 | echo `date` going up! >> /root/cloudflare/logs/switch.log 20 | rm -f /root/cloudflare/backupactive 21 | -------------------------------------------------------------------------------- /monit_conf/cloudflare.conf: -------------------------------------------------------------------------------- 1 | check host yourwebsite.com with address yourwebsiteip 2 | alert your.email@domain.tld 3 | 4 | if failed icmp type echo count 3 with timeout 1 seconds for 4 cycles then exec "/bin/bash -c /root/cloudflare/tobackup.sh" 5 | else if succeeded for 20 cycles then exec "/bin/bash -c /root/cloudflare/tomain.sh" 6 | if failed url http://yourwebsite.com with timeout 2 seconds and retry 3 for 15 cycles then exec "/bin/bash -c /root/cloudflare/tobackup.sh" 7 | else if succeeded for 20 cycles then exec "/bin/bash -c /root/cloudflare/tomain.sh" 8 | --------------------------------------------------------------------------------