Thursday, September 14th, 2006

Maintaining a regular backup of your blog / database

Today in this blog about Palm will focus more on the 'blog' in the 'Palm'. I think what comes next can be quite interesting to people, even those who do not have a blog but another type of service or website that uses databases, whether portals, forums, etc..

A few days ago as you know I upgraded the version of WordPress (software CMS that works with this blog) to version 2.0.4, which is proving stable and unproblematic. However, it has encountered a problem. First things first.

WordPress has by default a plugin to make regular backups of the database, from which you can retrieve all the posts, comments, and other information after a disaster, or clone the full blog on a new server. His name is wp-db-backup and does a great job with the ability to save the backup on our server, or send it by email, and most importantly, combined with the plugin wp-cron, we could schedule a backup of up daily, and we arrived by email.

The plugin is excellent, and in fact I've used without problems for months. But unfortunately, their development was stopped a year ago, and version 2.0.4 of WordPress is no longer functioning the ability to schedule backups. The same author informs us in this review that has stopped working on it, and has not tested this latest version. So unless another developer takes over and correct the plugin will not work in this and subsequent versions of WordPress.

We certainly do not want to have to run by hand every day the plugin, because I knew that sooner or later (or rather before) was to forget. So looking through the network, and testing various alternatives, I have finished writing a simple script in Bash that performs a daily backup of the database and saves it on the server, keeping the N latest versions and deleting the above. Also you could add the option of sending the file via email, but on my server I have no chance. It would only add one line of code:

/ bin / mail-s "subject" "address" <file

For this script to work, the server hosting the blog you have to use Linux, using MySQL as database software, and give them the ability to create regular jobs in Cron. The script is as follows:

mysqldump --add-drop-table --user "nombre_usuario" --password="contraseña" "base_de_datos" | gzip -9 > /home/"usuario_cpanel"/"directorio_backup"/"base_de_datos".`date --iso-8601`.gz
ls -t /home/"usuario_cpanel"/"directorio_backup"/"base_de_datos".* | tail +"número_copias_más_1" | xargs rm -f

Seen this way it looks horrible (they are really only two lines in this narrow column format are cut), but after a while give you a link to download it directly and configure it. All the quotation marks are data that you you will have to replace the corresponding values (without the quotes). Take his explanation one by one:

  • "Username": the name of the user accessing the database. It's you had to write to the wp-config.php file during installation.
  • "Password" the password to access the database. It's you had to write to the wp-config.php file during installation.
  • "Dbname", the name of the database. Also what you had to write to the wp-config.php file during installation.
  • "Usuario_cpanel" is your username on the server hosting. Surely is that you use to access the control panel.
  • "Directorio_backup" means the directory in which backups saved. You must create you for example by entering your FTP account. I recommend it in the root directory, which is nothing else to access. You'll have to give write permissions for that script can use it (this is done through the attributes of the directory you have just created, any decent FTP program will let you change them). For example try these 777 (read / write permission for all). If that directory is outside the directory where you save the contents of your WordPress installation (typically public_html), no one can access it via web.
  • "Número_copias_más_1" just that. If you want to save the last three copies, put a 4.

To better understand the script, as you would see specific data:

mysqldump --add-drop-table --user paco --password=paquito pacodb | gzip -9 > /home/paco_server/mysql_backups/pacodb.`date --iso-8601`.gz
ls -t /home/paco_server/mysql_backups/pacodb.* | tail +3 | xargs rm -f

You can download the code directly into a text file. I call db_backup.sh eg, you change the above variables by your example, and you go up by FTP to the root of your server. In the file attributes, you have to give execute permission for user and group, for example Put 754. You are ready to use.

Ejemplo de un panel cPanel, donde configurar el Cron
Example of a panel cPanel, which set the Cron

Now we have to create the cron job to execute it periodically. That is done from the control panel of your account on the server. For example, the server hosting this blog that you read, the software uses cPanel as control panel, and will surely be the case with many others, because of its popularity. In my case the option is called Cron jobs. We went and chose a new one. As command to execute, enter the path to our script, for example:

/home/pepe/backup.sh

And as frequency, say every six hours, all days, months and years. Unix Cron format line would look like to add:

0 */6 * * * /home/pepe/backup.sh

Save the changes, and it is scheduled to run our script every six hours. That is, four times a day will become a copy of our database in this directory. Shall accrue daily archives for the day you say, and when they are older than that, it erased. With this system, if some day we put our foot to change something on the blog, we can return to a previous state easily, by following these instructions.

I hope that the process has been fairly clear, even if you never tinkering with Cron jobs or databases, will be better than someone more used to working with servers you a hand. In return, you will have a backup of your blog, believe me, time is worth its weight in gold.


Deals in other equipment ...



Related articles

Share:



16 comments on "Maintaining a regular backup of your blog / database"


Pages: [1] 2 »

Leave a comment


Labels valid: <a href="" title=""> <abbr title=""> <acronym title=""> NEWS <blockquote cite=""> <cite> <code> <del datetime = "" > <em> <i> <q cite=""> <strike> <strong>

Akismet has protected and in this blog of thousands of fraudulent reviews. But if your comment filtered by mistake please let me know via the contact email on the right.