Skip to content

A python script allowing to do snapshots of multiple Digitalocean Droplets with doctl.

License

Notifications You must be signed in to change notification settings

drhdev/mdosnapshots

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

39 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mdosnapshots - multiple Digitalocean snapshots

License: GPL v3

mdosnapshots ("multiple Digitalocean snapshots") is a Python-based tool designed to manage snapshots for multiple DigitalOcean Droplets. It automates the creation, retention, and deletion of snapshots, ensuring your droplets are backed up efficiently. Additionally, it integrates with Telegram to notify you of the snapshot operations' status, providing real-time updates directly to your messaging app.

Table of Contents

Features

  • Automated Snapshot Management: Schedule regular snapshots for your DigitalOcean Droplets.
  • Retention Policy: Define how many recent snapshots to retain and automatically delete older ones.
  • Telegram Notifications: Receive real-time updates on snapshot operations via Telegram.
  • Flexible Configuration: Manage multiple droplets with individual settings using YAML configuration files.
  • Logging: Detailed logging with log rotation to monitor operations and troubleshoot issues.

Prerequisites

  • Python 3.7+
  • DigitalOcean Account with Droplets and API Tokens
  • doctl: DigitalOcean command-line tool installed and configured
  • Telegram Bot: A Telegram bot token and chat ID for sending notifications

Installation

  1. Clone the Repository

    git clone https://github.com/drhdev/mdosnapshots.git
    cd mdosnapshots
  2. Create a Virtual Environment (Optional but Recommended)

    python3 -m venv venv
    source venv/bin/activate
  3. Install Dependencies

    pip install -r requirements.txt
  4. Install doctl

    Follow the official doctl installation guide to install doctl on your system.

Configuration

  1. Environment Variables

    Create a .env file in the root directory of the repository based on the provided env.example:

    cp env.example .env

    Edit the .env file to include your Telegram Bot Token and Chat ID:

    TELEGRAM_BOT_TOKEN=your_bot_token_here
    TELEGRAM_CHAT_ID=your_chat_id_here
  2. Droplet Configuration

    Navigate to the configs directory and create or edit YAML configuration files for each droplet. Example configurations are provided as droplet1.yaml and droplet2.yaml.

    droplet:
      id: "your-digitalocean-droplet-id-1" # e.g. 33345678
      name: "your-digitalocean-droplet-name-1" # e.g. wordpress-ubuntu-s-1vcpu-1gb-nyc2-01
      api_token: "your_digitalocean_api_token_1"
      retain_last_snapshots: 3
      delete_retries: 5
    • id: The unique identifier of your droplet.
    • name: The name of your droplet.
    • api_token: Your DigitalOcean API token with appropriate permissions.
    • retain_last_snapshots: Number of recent snapshots to retain.
    • delete_retries: Number of retries for deleting snapshots in case of failure.

Usage

Managing Snapshots

To manually run the snapshot management script:

python mdosnapshots.py -v
  • Options:
    • -v, --verbose: Enable verbose logging to the console.
    • Specify specific configuration files by listing them as arguments.

Example:

python mdosnapshots.py droplet1.yaml droplet2.yaml -v

Sending Log Notifications to Telegram

After running the snapshot script, use the log notifier to send the latest status to Telegram:

python log2telegram.py -v
  • Options:
    • -v, --verbose: Enable verbose output to the console.
    • -d, --delay: Delay in seconds between sending multiple Telegram messages (default: 10 seconds).

Example:

python log2telegram.py --verbose --delay 15

Setting Up Cronjobs

To automate the snapshot management and notification process, set up cronjobs that run both scripts sequentially.

Example Cronjob

  1. Open the Crontab Editor

    crontab -e
  2. Add the Following Cronjob

    The following example schedules the snapshot process to run daily at 2:00 AM and immediately sends a Telegram notification after snapshots are managed.

    0 2 * * * PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin; cd /path/to/mdosnapshots && /path/to/mdosnapshots/venv/bin/python mdosnapshots.py >> mdosnapshots_cron.log 2>&1 && /path/to/mdosnapshots/venv/bin/python log2telegram.py >> log2telegram_cron.log 2>&1
    • Replace /path/to/mdosnapshots with the actual path to your mdosnapshots repository.
    • Replace /path/to/modsnapshots/venv/bin/python with the path to your Python executable, especially if using a virtual environment.
    • Logging: The output of each script is appended to separate cron logs for troubleshooting.
  3. Save and Exit

    Save the crontab file and exit the editor. The cronjob is now scheduled to run automatically.

Logging

  • Snapshot Manager Log: mdosnapshots.log – Logs detailed information about snapshot operations.
  • Telegram Notifier Log: log2telegram.log – Logs the activities of sending messages to Telegram.
  • Cronjob Logs: mdosnapshots_cron.log & log2telegram_cron.log – Capture the output of cron-executed scripts.

Logs are managed with log rotation to prevent uncontrolled growth.

Contributing

Contributions are welcome! Please fork the repository and submit a pull request for any enhancements or bug fixes.

License

This project is licensed under the GNU General Public License v3.0. See the LICENSE file for details.

About

A python script allowing to do snapshots of multiple Digitalocean Droplets with doctl.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages