Database Guardian
A powerful command-line utility for backing up and restoring multiple database types.
Features
- Database Connectivity: Connect to PostgreSQL (MySQL, MongoDB, and SQLite coming soon)
- Backup Operations: Full Backup with compression
- Storage Options: Local storage and AWS S3 (Google Cloud Storage and Azure Blob Storage coming soon)
- Restore Operations: Restore databases from backup files
- Automatic Scheduling: Schedule backups using cron jobs
- Logging: Track all backup activities
Installation
Prerequisites
- Go (1.18 or later)
- Database client tools (e.g., pg_dump for PostgreSQL)
- Cloud SDKs for cloud storage integrations
Steps
$ git clone https://github.com/Annany2002/Database-Guardian.git
$ cd data_guardian
$ go mod tidy
$ go build .Docker Setup
Run the Database Guardian CLI tool using Docker without needing to install Go or any dependencies locally.
Prerequisites
- Docker installed on your machine
Run Database Guardian CLI
$ docker pull annany/guard:latest
$ docker run --rm -it annany/guard:latest /bin/bash
$ docker run -dit --name guard-container annany/guard:latest /bin/bashPersist Data Using Volumes
$ docker run -dit --name guard-container -v guard-data:/app/data annany/guard:latest /bin/bash
$ docker run -dit --name guard-container -v $(pwd)/data:/app/data annany/guard:latest /bin/bashCommand Reference
Backup Command
$ guard backup --dbms mysql --host localhost --port 3306 --username root --password secret --dbname mydbOptions
--dbms: Type of the database (mysql, postgres, mongodb, sqlite), default is postgres--host: Database host--port: Database port--username: Username for database access--password: Password for database access--dbname: Name of the database to back up--output: (Optional) Directory to save the backup file
Backup Operations
Database Guardian provides robust backup capabilities for your database. Currently, the tool supports full backups for PostgreSQL databases, with incremental and differential backup support coming in future releases.
Backup Types
- Full Backup: Creates a complete copy of your database
- Incremental Backup (Upcoming): Only backs up data that changed since the last backup
- Differential Backup (Upcoming): Backs up all changes since the last full backup
Compression
All backups are automatically compressed to save storage space. This is particularly useful when backing up large databases or when storing backups in cloud storage with limited capacity.
Example Backup Command
$ guard backup --dbms postgres --host localhost --port 5432 --username postgres --password securepass --dbname my_database --output /path/to/backupsRestore Operations
Database Guardian makes it easy to restore your databases from previously created backups. This is crucial for disaster recovery or when migrating to a new server.
Basic Restore
To restore a database from a backup file, use the restore command with the path to your backup file:
$ guard restore --dbms postgres --host localhost --port 5432 --username postgres --password securepass --dbname my_database --file /path/to/backup_file.sqlSelective Restore
In upcoming versions, Database Guardian will support selectively restoring specific tables or collections, allowing for more granular control over the restore process.
Scheduling Backups
Automation is key to a reliable backup strategy. Database Guardian allows you to schedule backups using cron jobs, ensuring your data is backed up regularly without manual intervention.
Creating a Scheduled Backup
To schedule a backup, use the schedule command with a cron expression:
$ guard schedule --cron "0 2 * * *" --dbms postgres --host localhost --port 5432 --username postgres --password securepass --dbname my_databaseThis example schedules a backup every day at 2:00 AM.
Common Cron Expressions
0 2 * * *: Every day at 2:00 AM0 */6 * * *: Every 6 hours0 0 * * 0: Every Sunday at midnight0 0 1 * *: First day of every month at midnight
Managing Scheduled Backups
To view all scheduled backups:
$ guard listTo remove a scheduled backup:
$ guard unschedule --j job_idStorage Options
Database Guardian provides multiple options for storing your backup files, allowing for flexibility and redundancy in your backup strategy.
Local Storage
By default, backups are stored locally in the system. You can specify the output directory using the --output flag:
$ guard backup --dbms postgres --host localhost --port 5432 --username postgres --password securepass --dbname my_database --output /path/to/backupsCloud Storage
Database Guardian supports storing backups in cloud storage providers for added security and accessibility:
- AWS S3
Currently supported for storing backups in Amazon S3 buckets.
Coming Soon
- - Google Cloud Storage
- - Azure Blob Storage
Configuration
Database Guardian will support configuration through a YAML file in future releases, allowing you to set default values for common settings.
Example Configuration (Upcoming)
mysql:
host: localhost
port: 3306
user: root
password: secret
db_name: mydb
storage:
local: /backups
cloud: aws
aws:
bucket: your-bucket-name
region: us-west-1Logging
Database Guardian provides comprehensive logging to help you monitor and troubleshoot backup and restore operations.
Log Location
Logs are stored in logs/guard.log by default. The log path will be configurable in future releases.
Log Information
The logs include the following information:
- Start time of operations
- End time of operations
- Operation status (success or failure)
- Error messages (if any)
- Database connection details