Home » Posts tagged "backups"

Using rsnapshot for daily PostgreSQL backups

Having done backups for MySQL using rsnapshot before I recently had to do the same thing for PostgreSQL. Turns out PostgreSQL has similar tools to do this and it’s actually quite easy to set it up.

First, assuming rsnapshot will run under the root user you’ll need to create a .pgpass file at /root/.pgpass that will hold the credentials that are required for setting up a connection with your database. The expected format is host:port:database:user:password, so for example:

localhost:*:mydbname:myusername:mypassword

With that in place create a shell script that will use this file and run the pg_dump program. Put it some place you can remember, e.g. /usr/local/bin/postgresql-backup.sh:

#!/bin/bash
export PGPASS=/root/.pgpass
pg_dump -w -h localhost -U myusername mydbname > postgresql-dump.sql
gzip postgresql-dump.sql

Again, just as with the using rsnapshot for daily MySQL backups article you need to configure rsnapshot.

Next is setting up /etc/rsnapshot.conf which is easy as well. At the end of this file you’ll find all the instructions of which directories to backup. Enter the next line. Do note that rsnapshot.conf uses a tab to separate values.

backup_script   /usr/local/bin/postgresql-backup.sh  postgresql/

To enable daily backups uncomment the next line:

#interval       hourly  6
interval        daily   7
#interval       weekly  4
#interval       monthly 3

To make rsnapshot run every day update /etc/cron.d/rsnapshot:

# 0 */4         * * *           root    /usr/bin/rsnapshot hourly
  30 3          * * *           root    /usr/bin/rsnapshot daily
# 0  3          * * 1           root    /usr/bin/rsnapshot weekly
# 30 2          1 * *           root    /usr/bin/rsnapshot monthly

Finally, try running rsnapshot. After it has run check your backup directory, which on Ubuntu 12.04 defaults to /.snapshots/. Be sure to mount /.snapshots/ on a separate drive. Because what use is it if your data drive contains your backup and decides to commit suicide?

$ sudo rsnapshot daily

Your PostgreSQL backup file should now be located at /.snapshots/daily.0/postgresql/postgresql-dump.sql. You can restore this file with the psql commandline utility.

Using rsnapshot for daily MySQL backups


Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /var/www/blog_htbaa_com/www/wp-content/plugins/source-code-syntax-highlighting-plugin-for-wordpress/geshi.php on line 2137

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /var/www/blog_htbaa_com/www/wp-content/plugins/source-code-syntax-highlighting-plugin-for-wordpress/geshi.php on line 2137

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /var/www/blog_htbaa_com/www/wp-content/plugins/source-code-syntax-highlighting-plugin-for-wordpress/geshi.php on line 2137

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /var/www/blog_htbaa_com/www/wp-content/plugins/source-code-syntax-highlighting-plugin-for-wordpress/geshi.php on line 2137

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /var/www/blog_htbaa_com/www/wp-content/plugins/source-code-syntax-highlighting-plugin-for-wordpress/geshi.php on line 2137

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /var/www/blog_htbaa_com/www/wp-content/plugins/source-code-syntax-highlighting-plugin-for-wordpress/geshi.php on line 2137

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /var/www/blog_htbaa_com/www/wp-content/plugins/source-code-syntax-highlighting-plugin-for-wordpress/geshi.php on line 2137

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /var/www/blog_htbaa_com/www/wp-content/plugins/source-code-syntax-highlighting-plugin-for-wordpress/geshi.php on line 2137

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /var/www/blog_htbaa_com/www/wp-content/plugins/source-code-syntax-highlighting-plugin-for-wordpress/geshi.php on line 2137

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in /var/www/blog_htbaa_com/www/wp-content/plugins/source-code-syntax-highlighting-plugin-for-wordpress/geshi.php on line 2137

Now that I’m using a CloudServer I figured I had to do something about backups as well. I’ve been using rsnapshot at work for a few years now and it’s an ideal backup solution. Because rsnapshot (which uses rsync as its base) makes use of hard links backups are very efficient. Basically it means that if a file isn’t changed it’s not duplicated, but a hard link is created instead. This way only changed and new files get copied. Deleted files will eventually be removed as well as soon as one of the backups expires.

Using rsnapshot for file backups is easy and the configuration explains this easy enough. Backing up MySQL databases however is a different story. When using MyISAM tables you might get away with copying and pasting the database files. With InnoDB not so much. Proper backups can be made with mysqldump. In my case I don’t have many databases so I want one big SQL backup file.

First, lets set up our backup script. I’ve stored it under /usr/local/bin/backup_mysql.sh with permissions set at 0700. The reason for these permissions is that I store my password inside the script and only want root to be able to open it. It also needs to be executable. I know it’s better to use a configuration file and use source to include the variables, but for my current use it’ll do (do note at work I’ve done this the proper way!). I also use the root user instead of a special read-only user. I know this is bad practice, but again I’ve done this the proper way at work. It’s an exercise for you to do it properly (hehe, that’s another way to put that I’m lazy). Anyway, here’s the script.

  1.  

Nothing exciting. Change the credentials as required. Again, use source if you want to store the credentials inside a proper configuration file. Now, calling this script ./backup_mysql.sh will backup all databases inside the file mysqldump.sql in the current work directory.

Next is setting up /etc/rsnapshot.conf which is easy as well. At the end of this file you’ll find all the instructions of which directories to backup. Enter the next line. Do note that rsnapshot.conf uses a tab to separate values.

  1. backup_script   /usr/local/bin/backup_mysql.sh  mysql/

To enable daily backups uncomment the next line:

  1. #interval       hourly  6
  2. interval        daily   7
  3. #interval       weekly  4
  4. #interval       monthly 3

To make rsnapshot run every day update /etc/cron.d/rsnapshot:

  1.  

Finally, try running rsnapshot. After it has run check your backup directory, which on Ubuntu 10.04 defaults to /.snapshots/. Be sure to mount /.snapshots/ on a separate drive. Because what use is it if your data drive contains your backup and decides to commit suicide?

$ sudo rsnapshot daily

Your MySQL backup file should now be located at /.snapshots/daily.0/mysql/mysqldump.sql. You can restore this file with the MySQL commandline client, or through MySQL Administrator from the MySQL GUI Tools.