drilix.com

Freedom, Community & Sustainability

4 - Automate your tasks (power on, backup, update)

October 27, 2014 -- William
Last modified on December 2014
Duration: +- 30 minutes

These configurations are optional too, you don't have to do this if you just want to tinker with a server.

Automatically reboot after power failure

Servers should usually stay turned on all the time. In the event of a power failure you can configure your server to turn on as soon as the power is back. Of course, if you have a no-break that can keep your server running it's even better but even no-breaks can run out of battery.

Usually you can set this option in your computer BIOS. We've used the BIOS already at the beginning to boot Debian from an external storage. As a reminder, when you turn your computer on, the first thing that appears on your screen is the boot and there should be a message saying the key you should press to enter the BIOS configuration (F10, F11, Esc, Del... it depends on the vendor and BIOS version). Then, again, this particular configuration will depend on the BIOS you have and I can't give step-by-step instructions.

On some older computers the procedure might be different. Try to execute this command:

cat /proc/pmu/options

If the file does not exist, this setting does not apply to your computer. If the output is "server_mode=1" then your server is configured correctly, if the output is "server_mode=0" then type:

echo 'server_mode=1' > /proc/pmu/options

Setup a backup mechanism (advanced)

Note: for this part of the tutorial you need to have two servers, one with your live information and another to keep the backups.

Backing up a server is somewhat more complex than backing up a regular computer for the good reason that usually you won't be sitting next to the server. So instead of plugging in a drive and pressing the "backup" button we are going to do automated backups through the network. On top of it, websites usually deal with databases and it can get quite complicated to backup this information. It might be harder to set up but once it's done you can just forget about it. The entire process consists of:

  1. Back up the database
  2. Set automatic login to the server from another computer
  3. Synchronize files and database with this other computer

1 - Backup your database localy with AutoMySQLBackUp

AutoMySQLBackUP is a super simple program that is quite awesome. To install it, type:

apt-get install automysqlbackup

That's it! You can enter now on the terminal "automysqlbackup" to do the first backup without having to wait for the next cron run.

2 - Set up automatic login for the backup

What we want is to be able to run a script that will log into the server, get the database dumps and your sites folders in order to copy them regularly on a remote backup drive.

In your server, you will create a pair of authentication keys (do not enter a pass phrase when prompted) :

ssh-keygen -t rsa

Two files are are generated by this command, an id and a public key (the file that ends with .pub). The next commands will add the public key from the server in the list of authorized keys of the backup. These commands should be typed from the server:

ssh root@[backupIP] mkdir -p ~/.ssh
cat ~/.ssh/id_rsa.pub | ssh root@[backupIP] 'cat >> ~./ssh/authorized_keys'

Now from the backup, try to connect to the server without entering your password:

ssh root@[serverIP]

If you logged in successfully, it's working!

3 - Use rsynch to synchronize your backups with the remote server

The last step consists of creating a routine that will do the synchronization between the server and the backup. So we will add a line to our cron, to run rsynch regularly and keep the backup up to date. Type "crontab -e" to enter cron then add the following lines:

0 4 * * * rsync -az -i /var/www user@host:/path/to/folder >> /dev/null 2>&1

Automatically update the server

Warning: computers are dumb, ehatever they do automatically can potentially go wrong. It's rather safe to update Debian stable updates but much more risky if using the unstable branch and/or with external applications in the sources.list.

You can setup automatic updates using cron-apt. To install it type:

apt-get install cron-apt

There are mainly three files to configure, the first one is the cron schedule (/etc/cron.d/cron-apt) that controls when cron-apt will run. The second one is the configuration of what cron-apt will do when it runs (/etc/cron-apt/config). The third file is the one that contains the commands that will be executed for each action (/etc/cron-apt/action.d/3-download).

1 - Cron-apt schedule

Run this command to set when cron-apt will run, it follows exactly the same rules as a regular cron file:

vim /etc/cron.d/cron-apt
minute hour day month weekday user * command
from 0 to 60 from 0 to 23 from 1 to 31 (independent of weekday) from 1 to 12 (independent of weekday) from 0 to 7 (0 or 7 for sunday, 1 for monday, 2 for tuesday..)

valid username (* system crontab only)

complete path to the comman

2 - Cron-apt configuration

The configuration file allows you to set things like sending e-mails when update runs or when update fails. To make things easier there's a file with all options commented out so you can start with that: http://www.debianadmin.com/manpages/defaultcron-aptfile.txt
To send e-mails edit MAILTO=”your@email.com” and MAILON=”always”.

3 - Set the actions to execute

There are by default two files in /etc/cron-apt/actions.d, the first one (0-update) updates package information, the second (3-download) will download new packages. Note that none actually installs anything. This is ok if you just want to be notified when updates are available. It's also the best practice on production servers.
On testing servers you can set up updates to be installed automatically. What you need to do in order to actually run the update is to rename '3-download' to something like '5-install' and then edit its content. Just remove the '-d' download flag so that your command looks like:

autoclean -y
dist-upgrade -y

Add new comment