Skip to content

Latest commit

Β 

History

8 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

go-devops-cutter

License Go Version Version CI PR Tests

πŸ“ Description

Go-Devops-Cutter is a lightweight CLI tool built in Go for backing up PostgreSQL and MySQL databases to your local machine. It uses Docker-based database clients, so you don't need to install database tools locally.

Perfect for DevOps engineers who need quick, reliable database backups without installing database clients or managing complex tooling.

✨ Features

  • πŸ—„οΈ Database Backup - Backup PostgreSQL and MySQL databases to local machine
  • 🐳 Docker-based Clients - No need to install pg_dump or mysqldump locally
  • πŸ”’ SSH Jump Host Support - Secure access to databases behind firewalls via SSH tunneling
  • πŸ“¦ Auto Compression - Built-in gzip compression for backups
  • πŸ“‹ Backup Listing - List backup files in current directory
  • ⚑ Fast & Lightweight - Single binary, minimal dependencies
  • πŸ› οΈ Simple CLI - Easy to use command-line interface

πŸ› οΈ Tech Stack

  • Language: Go 1.24
  • CLI Framework: Cobra
  • Containerization: Docker (for database clients)

πŸ“¦ Key Dependencies

github.com/spf13/cobra            v1.10.1    // CLI framework
github.com/gin-gonic/gin          v1.11.0    // HTTP framework (for health API)

πŸš€ Quick Start

Prerequisites

  • Go 1.24 or higher
  • Docker (for running database clients)
  • Make
  • SSH client (for SSH jump host feature)

Installation

# Clone the repository
git clone https://github.com/PandhuWibowo/go-devops-cutter.git
cd go-devops-cutter

# Install dependencies
make deps

# Install git hooks (recommended for development)
./scripts/install-hooks.sh

# Build the CLI
make build-cli

# Install CLI system-wide (optional)
make install-cli

πŸ’» CLI Commands

Database Operations

cutter db backup - Backup database to local machine

Required Flags:

  • --database - Database name
  • --username - Database username

Optional Flags:

  • --type - Database type: postgres or mysql (default: postgres)
  • --host - Database host (default: localhost)
  • --port - Database port (default: 5432 for postgres, 3306 for mysql)
  • --password - Database password (prompted if not provided)
  • --output - Output file path (auto-generated if not specified)
  • --compress - Compress with gzip (default: true)
  • --ssh-jump - SSH jump host for accessing databases behind firewalls (format: user@host)

cutter db list - List backup files (.sql) in current directory

Usage Examples

# PostgreSQL backup (direct connection)
cutter db backup \
  --type postgres \
  --host localhost \
  --port 5432 \
  --username dbuser \
  --password dbpass \
  --database myapp

# MySQL backup with custom output
cutter db backup \
  --type mysql \
  --host 192.168.1.100 \
  --port 3306 \
  --username root \
  --password secret \
  --database production \
  --output ~/backups/prod_backup.sql.gz

# Backup via SSH jump host (for databases behind firewalls)
cutter db backup \
  --type postgres \
  --host 10.0.1.50 \
  --port 5432 \
  --username appuser \
  --password dbpass \
  --database internal_db \
  --ssh-jump devops@jumphost.company.com

# List all backup files
cutter db list

SSH Jump Host

The --ssh-jump flag enables access to databases behind firewalls or in private networks through an SSH bastion/jump host.

How it works:

  1. Creates an SSH tunnel from your local machine to the database through the jump host
  2. Automatically selects an available local port for the tunnel
  3. Runs the database backup through the tunnel
  4. Cleans up the SSH connection after backup completes

Requirements:

  • SSH access to the jump host must be configured (SSH key-based authentication recommended)
  • Jump host must have network access to the target database
  • Format: user@jumphost or user@jumphost:port

Example:

# Ensure your SSH key is loaded
ssh-add ~/.ssh/id_rsa

# Backup database behind firewall
cutter db backup \
  --type postgres \
  --host 10.0.1.50 \
  --port 5432 \
  --username dbuser \
  --password dbpass \
  --database production \
  --ssh-jump devops@jumphost.company.com

🌐 Health Check API

The project includes a minimal health check API server for monitoring.

Running the API Server

# Build and run
make build-api
./build/devops-cutter-api

# Or run directly
make run-api

# Custom port (default: 8080)
PORT=3000 ./build/devops-cutter-api

API Endpoint

GET /health - Health check endpoint

Response:

{
  "status": "ok",
  "version": "0.1.0"
}

Example:

curl http://localhost:8080/health

πŸ“ Project Structure

.
β”œβ”€β”€ cmd/
β”‚   β”œβ”€β”€ api/
β”‚   β”‚   β”œβ”€β”€ main.go              # Health check API server
β”‚   β”‚   └── main_test.go         # API tests
β”‚   └── cutter/
β”‚       └── main.go              # CLI entry point
β”œβ”€β”€ internal/
β”‚   └── cli/
β”‚       └── commands/
β”‚           β”œβ”€β”€ db.go            # Database backup commands
β”‚           └── db_test.go       # Command tests
β”œβ”€β”€ pkg/
β”‚   └── client/
β”‚       β”œβ”€β”€ client.go            # HTTP client utilities
β”‚       └── client_test.go       # Client tests
β”œβ”€β”€ build/                       # Build artifacts (generated)
β”‚   β”œβ”€β”€ cutter                   # CLI binary
β”‚   └── devops-cutter-api        # API binary
β”œβ”€β”€ Makefile
β”œβ”€β”€ go.mod
β”œβ”€β”€ go.sum
β”œβ”€β”€ LICENSE
└── README.md

πŸ”§ Development

Git Hooks

Install git hooks to automatically run tests before commits and pushes:

# Install hooks (run once after cloning)
./scripts/install-hooks.sh

Pre-commit hook:

  • Checks code formatting
  • Runs go vet
  • Verifies go.mod is tidy
  • Runs all tests
  • Builds project

Pre-push hook:

  • Runs tests with coverage
  • Builds all binaries
  • Warns about TODO/FIXME comments
  • Confirms push to main branch

Skip hooks when needed:

git commit --no-verify  # Skip pre-commit
git push --no-verify    # Skip pre-push

See scripts/README.md for more details.

Make Commands

# Development
make deps          # Download and tidy dependencies
make build         # Build all binaries (API + CLI)
make build-cli     # Build CLI only
make build-api     # Build API server only
make run-api       # Run API server locally
make test          # Run tests
make clean         # Clean build artifacts

# Installation
make install-cli   # Install CLI to /usr/local/bin

Development Setup

  1. Install Go 1.24+

    go version
  2. Install Docker

    docker --version
  3. Install Dependencies

    make deps
  4. Build and Test

    make build-cli
    ./build/cutter --help
    ./build/cutter db --help

πŸ§ͺ Testing

# Run all tests
make test

# Run tests with coverage
go test -v -cover ./...

# Run specific package tests
go test -v ./internal/cli/commands
go test -v ./cmd/api
go test -v ./pkg/client

πŸš€ Deployment

CLI Deployment

# Build for production
make build-cli

# Deploy to remote server
scp build/cutter user@server:/usr/local/bin/

# Or install locally
sudo cp build/cutter /usr/local/bin/

API Server Deployment

# Build for production
make build-api

# Deploy to server
scp build/devops-cutter-api user@server:/opt/devops-cutter/

# Run with systemd (example)
# /etc/systemd/system/devops-cutter-api.service:
# [Unit]
# Description=DevOps Cutter Health Check API
#
# [Service]
# ExecStart=/opt/devops-cutter/devops-cutter-api
# Environment=PORT=8080
# Restart=always
#
# [Install]
# WantedBy=multi-user.target

🀝 Contributing

Contributions are welcome! Here's how you can help:

  1. Fork the repository
  2. Create a new branch: git checkout -b feature/your-feature
  3. Make your changes and add tests
  4. Run tests: make test
  5. Commit your changes: git commit -am 'Add feature'
  6. Push to your branch: git push origin feature/your-feature
  7. Open a pull request

Please ensure your code:

  • Follows Go best practices
  • Includes appropriate tests
  • Has clear commit messages
  • Updates documentation as needed

πŸ“œ License

This project is licensed under the MIT License. See LICENSE file for details.

πŸ™ Acknowledgments

  • Cobra - CLI framework for Go
  • Gin - HTTP web framework
  • Docker - Container platform for database clients

Made with ❀️ by Pandhu Wibowo

About

No description, website, or topics provided.

Resources

Stars

5 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages