Personal website of Martin Halford

Automated backup using rtcwake and crontab on Ubuntu

This article describes using rtcwake and crontab to wake up a backup computer on a daily basis, perform the backup from a remote server using rsync, then suspend ready for the next time around. The rtcwake process is also re-initiated if the backup computer is woken from suspend in the interim

Firstly, the backup computer must be able to access the server as root using a key:

ssh-keygen -f "/root/.ssh/known_hosts" -R "xxx.xxx.xxx.xxx"
ssh-copy-id username@xxx.xxx.xxx.xxx

and a cron job is needed to start the backup once the computer wakes up, in this cases at 20:32, daily

run crontab -e as root

# m h dom mon dow command
32 20 * * * /backup/backupscript

and the backup script should look something like:



echo "Running Server Backup: $(date)" >> $log_file echo " " >> $log_file
echo "Backup of directory_name" >> $log_file
rsync -avh -e ssh username@xxx.xxx.xxx.xxx:"/directory_name/" /backup/ 2>&1 >> $log_file
echo "\nEnding Backup: $(date)" >> $log_file
echo " " >> $log_file
/usr/sbin/rtcwake -m mem -l -t $(date +%s -d 'tomorrow 20:30') 2>&1 >> $log_file 

The final line causes the computer to suspend with a wake up time of 20:30

rtcwake also needs to be setup on suspend should the computer be used in the interim:

As root, create/edit the file........


adding the following

if [ "${1}" == "pre" ]; then
/usr/sbin/rtcwake -m no -l -t $(date +%s -d 'tomorrow 20:30')

Maybe set the computer to suspend automatically after 10-30mins depending on how long the backup is likely to take.