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
sudo chown backup:backup /backup
1.4 add the user backup
sudo useradd backup
Create the private/public ssh keys:
ssh-keygen -t rsa
Copy the public key to all hosts you want to backup:
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