DEV Community

Alef Ojeda de Oliveira
Alef Ojeda de Oliveira

Posted on

How use the gem 'run_database_backup'

Introduction

Data is a critical aspect of any application, and it's essential to have a reliable backup strategy in place to protect your data in case of a disaster or data loss. Fortunately, the "run_database_backup" gem provides an easy way to create backups of your MongoDB, PostgreSQL, and MySQL databases from within your Rails application.

In this guide, we'll walk you through the basic steps for using the "run_database_backup" gem to create backups of your databases.

Usage

Add the "run_database_backup" gem to your Gemfile:

gem 'run_database_backup'
Enter fullscreen mode Exit fullscreen mode

Install the gem by running the following command:

bundle install
Enter fullscreen mode Exit fullscreen mode

This will add the three backup tasks to your application.

To create a backup of your MongoDB database, run the following command:

rails mongo:backup[uri,database_name,backup_directory]
Enter fullscreen mode Exit fullscreen mode

Replace "uri" with the URI for your MongoDB database (e.g. "mongodb://localhost:27017"), "database_name" with the name of the MongoDB database you want to back up, and "backup_directory" with the directory where you want to store the backup file.

For example, to create a backup of a MongoDB database called "app_database" running on the local machine and store the backup file in the "./tmp" directory, you would run:

rails mongo:backup['mongodb://localhost:27017','app_database','./tmp']
Enter fullscreen mode Exit fullscreen mode

To create a backup of your PostgreSQL database, run the following command:

rails postgresql:backup[uri,database_name,backup_directory]
Enter fullscreen mode Exit fullscreen mode

Replace "uri" with the URI for your PostgreSQL database (e.g. "postgresql://localhost"), "database_name" with the name of the PostgreSQL database you want to back up, and "backup_directory" with the directory where you want to store the backup file.

For example, to create a backup of a PostgreSQL database called "app_database" running on the local machine and store the backup file in the "./tmp" directory, you would run:

rails postgresql:backup['postgresql://localhost','app_database','./tmp']
Enter fullscreen mode Exit fullscreen mode

Note that you will need to have the "pg_dump" utility installed on your machine in order to create a backup of a PostgreSQL database.

To create a backup of your MySQL database, run the following command:

rails mysql:backup[uri,database_name,backup_directory]
Enter fullscreen mode Exit fullscreen mode

Replace "uri" with the URI for your MySQL database (e.g. "mysql2://localhost"), "database_name" with the name of the MySQL database you want to back up, and "backup_directory" with the directory where you want to store the backup file.

For example, to create a backup of a MySQL database called "app_database" running on the local machine and store the backup file in the "./tmp" directory, you would run:

rails mysql:backup['mysql2://localhost','app_database','./tmp']
Enter fullscreen mode Exit fullscreen mode

Note that you will need to have the "mysqldump" utility installed on your machine in order to create a backup of a MySQL database.

Conclusion

In this guide, we've walked you through the basic steps for using the "run_database_backup" gem to create backups of your MongoDB, PostgreSQL, and MySQL databases from within your Rails application. By following these steps, you can quickly and easily create backups of your databases to protect your data in case of a disaster or data loss.

Remember to store your backup files in a secure location and to regularly test your backup strategy to ensure that you can restore your data when you need it.

To learn more about the "run_database_backup" gem, visit the gem's repository on GitHub: https://github.com/nemuba/run_database_backup

Happy backing up!

Top comments (0)