Automated Backups Using dhcpd On Ubuntu

This tutorial shows how to set up automated backups of Linux hosts through dhcp using Ubuntu.

I will use following software:

  • dhcp3
  • custom scripts

 

1 On The Server

1.1 Configure dhcp3

In /etc/dhcp3/dhcps.conf add:

on commit {
  execute (
    "/etc/dhcp3/startbackup.sh",
    "start",
    binary-to-ascii(10,8,".",leased-address)
  );
}

 

1.2 Create startbackup.sh

sudo vi /etc/dhcp3/startbackup.sh
#!/bin/bash
case "$1" in
start)
  sudo -u backup /etc/dhcp3/rsync.sh $2
;;
*)
;;
esac
exit 0

 

1.3 Create rsync.sh

sudo vi /etc/dhcp3/rsync.sh
#!/bin/bash
nohup rsync -azuvb backup@$1:/home /backup/$1 &

and add the /backup folder:

sudo mkdir /backup

 

1.4 Add The User backup

sudo useradd backup
sudo chown backup:backup /backup

Create the private/public ssh keys:

ssh-keygen -t rsa

Copy the public key to all hosts you want to back up:

ssh-copy-id -i ~/.ssh/id_rsa backup@[host]

Replace [host] with your hostname(s) or ip address(es).

 

1.5 Configure sudo

sudo visudo

and add a line like this:

dhcpd ALL=(backup)NOPASSWD: /etc/dhcp3/startbackup.sh, /etc/dhcp3/rsync.sh

 

1.6 Configure AppArmor

sudo vi /etc/apparmor.d/usr.sbin.dhcpd3

Change the part that looks like:

/etc/dhcp3/ r,
/etc/dhcp3/** r,
/etc/dhcpd.conf r,
/etc/dhcpd_ldap.conf r,

to:

/etc/dhcp3/ r,
/etc/dhcp3/** r,
/etc/dhcp3/startbackup.sh Uxr,
/etc/dhcpd.conf r,
/etc/dhcpd_ldap.conf r,

 

1.7 Restart Services

sudo /etc/init.d/apparmor restart
sudo /etc/init.d/dhcp3-server restart

 

On The Client

2.1 Add The User backup

sudo useradd backup
sudo usermod -G root

 

2.2 Test The Setup

sudo dhclient [interface]

where interface is the one you want to renew

You should now have a backup of your clients on the server.

Share this page:

1 Comment(s)