Navigation
ARCHIVE: January, 2016

Effective Nightly Backups with crontab

Cron is a way of scheduling tasks on linux based machines; most often used to run nightly, weekly or monthly batch processes to move data or restart services. Crontab is the tool on ubuntu to interact with the cron daemon and really simplifies the process. To start, we need to start editing our crontab file. If this is your first time using crontab, it should prompt you that it’s creating a new file and ask you for an editor. I typically use nano on Ubuntu and vi/vim on other distros. To do this, run the following command.

user@host> crontab -e

The format of the tasks is as follows. Keep in mind that for any of the time properties, an asterisk (*) may be used to signify “all.”

minute hour day_of_month month day_of_week command

Our command is pretty straight forward. We want to run a shell script and store the output in a log file (for debugging). The path is not specific to crontab or cron jobs in general and may be changed to suit your needs. I generally keep my work in /var to align with the general use of the server (/var/www/*)

0 23 * * * /var/scripts/nightly.sh > /var/scripts/cron.log

In /var, create your scripts directory and start editing nightly.sh. We’ll break the file into three sections: MySQL backups, File backups, and Service restarts.

#!/bin/sh
echo "Making backups of MySQL databases"
mysqldump -u USERNAME -pPASSWORD DATABASE_NAME | gzip > "/var/backups/SITE_NAME.sql.gz"

echo "Making file backups of the websites"
tar -czf /var/backups/SITE_NAME.files.tar.gz /var/www/SITE_NAME

echo "Restarting services"
sudo service MySQL restart
sudo service apache2 restart

echo "Finished!"

Keep in mind that the specifics of each line are relative to your setup. In my case, I keep all my backups in /var and all my sites organized by SITE_NAME in /var/www.

To test this script, all you need to do is manually run it.

user@host> ./nightly.sh

If all is well, you’ll be set! Happy cronning!

Adding a New User to MySQL

Whenever you boot up a service like MySQL which has a root user, it’s best practice to create users for your specific needs that are limited by security and scope. In the case of running a WordPress or similar MySQL based application, it’s best to limit the user to a specific database.

We begin by loading the MySQL CLI.

mysql -u root -p

We then create the new user. If you haven’t created the database yet, now would be an excellent time.

CREATE USER 'wordpress_admin'@'localhost' IDENTIFIED BY 'someCOMPLEXpassword';

Note that we want to limit the user’s scope to just localhost such that even if we were to open the server’s ports, the user would not be able to logon remotely.

We then grant privileges to the user to access our specific database.

GRANT ALL PRIVILEGES ON wordpress_database.* TO 'wordpress_admin'@'localhost';

If you ever need to remove the user, you may do so with the following DROP command.

DROP USER 'wordpress_admin'@'localhost';

Enable URL Rewriting on Apache

After installing apache, you can enable mod_rewrite by running the following commands.

sudo a2enmod rewrite
sudo service apache2 restart

You will probably want to use the .htaccess file to configure your mod_rewrite. In this case, you should make sure to allow overrides by modifying your site’s configuration file. If you haven’t already, I suggest creating/copying the default config in /etc/apache2/sites-enabled. To enable the .htaccess file, add the following.


    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
    allow from all

Pay attention to the Directory element and the path that follows. For me, this path is different and for you it may be different as well. And as usual, restart the apache service.

sudo service apache2 restart
There are no more results.