---
title: "Thiết lập sao lưu tự động và khôi phục nhanh cho Prometheus Grafana"
description: "Hướng dẫn backup tự động và khôi phục nhanh dữ liệu TSDB của Prometheus cùng dashboard SQLite/PostgreSQL của Grafana trên Cloud VPS NVMe, không mất metric."
canonical: "https://thuecloud.vn/thiet-lap-sao-luu-tu-dong-va-khoi-phuc-nhanh-cho-prometheus-grafana"
markdown_url: "https://thuecloud.vn/thiet-lap-sao-luu-tu-dong-va-khoi-phuc-nhanh-cho-prometheus-grafana.md"
locale: "vi"
kind: "article"
published_at: "2026-11-22T16:18:16.630Z"
updated_at: "2026-11-22T16:18:15.649Z"
site: "ThueCloud.Vn"
estimated_tokens: 2096
---
# Thiết lập sao lưu tự động và khôi phục nhanh cho Prometheus Grafana

> Hướng dẫn backup tự động và khôi phục nhanh dữ liệu TSDB của Prometheus cùng dashboard SQLite/PostgreSQL của Grafana trên Cloud VPS NVMe, không mất metric.

- Canonical HTML: https://thuecloud.vn/thiet-lap-sao-luu-tu-dong-va-khoi-phuc-nhanh-cho-prometheus-grafana
- Markdown URL: https://thuecloud.vn/thiet-lap-sao-luu-tu-dong-va-khoi-phuc-nhanh-cho-prometheus-grafana.md
- Organization: ThueCloud.Vn
- Support Email: support@thuecloud.vn
- Hotline: 03333 499 48
- Published: 2026-11-22
- Updated: 2026-11-22

## Mục lục

- Chiến lược sao lưu đúng chuẩn cho Prometheus và Grafana
- Script sao lưu tự động Prometheus và Grafana
- Quy trình khôi phục nhanh khi gặp sự cố
- Câu hỏi thường gặp (FAQ)
  - Bật --web.enable-admin-api trên Prometheus có gây mất an toàn không?
  - Tại sao không dùng lệnh cp cho grafana.db khi Grafana đang chạy?
  - Nên giữ dữ liệu metric Prometheus trong bao lâu trên VPS?

Hệ thống giám sát Prometheus và Grafana là "đôi mắt" của đội ngũ vận hành trên [VPS Việt Nam](https://thuecloud.vn/vps-viet-nam). Tuy nhiên, nếu bạn sao chép trực tiếp thư mục dữ liệu TSDB của Prometheus bằng `cp` hoặc `tar` khi dịch vụ đang chạy, file block có thể bị hỏng (corrupt) do dữ liệu đang ghi dở trong WAL (Write-Ahead Log). Bài viết này hướng dẫn quy trình backup an toàn bằng Snapshot API và khôi phục nhanh trong vài phút.

> **Tóm tắt nhanh (TL;DR):**
> - Kích hoạt cờ `--web.enable-admin-api` cho Prometheus (chỉ bind `127.0.0.1`) để tạo snapshot nhất quán qua HTTP POST.
> - Dùng `sqlite3 .backup` cho `grafana.db` thay vì copy thô nhằm tránh lỗi khóa database.
> - Đồng bộ bản sao lưu ra nơi lưu trữ tách biệt bằng `rclone` hoặc `restic` và kiểm thử restore định kỳ.
> - *Cập nhật: 11/2026*

## Chiến lược sao lưu đúng chuẩn cho Prometheus và Grafana

Để đảm bảo tính toàn vẹn dữ liệu, mỗi thành phần cần cơ chế snapshot riêng. Quy trình này cũng tương đồng về mặt nguyên tắc nhất quán dữ liệu như khi bạn thực hiện [sao lưu tự động cho SQL Server 2022](https://thuecloud.vn/windows-server-sql-server-2022-cach-sao-luu-tu-dong-va-khoi-phuc-khi-loi). Đồng thời, hãy áp dụng [chính sách phân quyền Zero-Trust](https://thuecloud.vn/huong-dan-ra-soat-va-siet-chat-bao-mat-cho-he-thong-api-phuc-vu-ung-dung-mobile-chinh-sach-phan-quyen-zero-trust) để giới hạn quyền truy cập vào Admin API của Prometheus.

| Thành phần | Vị trí dữ liệu mặc định | Cách backup an toàn | Lưu ý quan trọng |
| :--- | :--- | :--- | :--- |
| Prometheus TSDB | `/var/lib/prometheus/data` | `POST /api/v1/admin/tsdb/snapshot` | Chỉ bind `127.0.0.1:9090` khi bật Admin API |
| Prometheus Config | `/etc/prometheus/*.yml` | `tar -czf` + Git versioning | Lưu cả `rules/*.yml` và `alertmanager.yml` |
| Grafana DB | `/var/lib/grafana/grafana.db` | `sqlite3 ... ".backup ..."` | Không copy trực tiếp khi Grafana đang ghi |
| Grafana Provisioning | `/etc/grafana/provisioning` | `tar -czf` | Chứa datasource và dashboard dạng code |

## Script sao lưu tự động Prometheus và Grafana

Trước hết, đảm bảo Prometheus được khởi chạy với cờ `--web.enable-admin-api` (và `--web.listen-address=127.0.0.1:9090` để không lộ Admin API ra ngoài). Sau đó tạo script `/usr/local/bin/backup-monitoring.sh`:

```bash
#!/usr/bin/env bash
set -euo pipefail

BACKUP_DIR="/var/backups/monitoring/$(date +%F_%H%M)"
PROM_DATA="/var/lib/prometheus/data"
mkdir -p "$BACKUP_DIR"

# 1. Tạo Prometheus TSDB Snapshot nhất quán
SNAP_JSON=$(curl -s -XPOST http://127.0.0.1:9090/api/v1/admin/tsdb/snapshot)
SNAP_NAME=$(echo "$SNAP_JSON" | jq -r '.data.name')
if [[ -z "$SNAP_NAME" || "$SNAP_NAME" == "null" ]]; then
  echo "Lỗi tạo snapshot Prometheus: $SNAP_JSON" >&2
  exit 1
fi
tar -czf "$BACKUP_DIR/prometheus-tsdb.tar.gz" -C "$PROM_DATA/snapshots" "$SNAP_NAME"
rm -rf "$PROM_DATA/snapshots/$SNAP_NAME"

# 2. Backup an toàn Grafana SQLite DB & cấu hình
sqlite3 /var/lib/grafana/grafana.db ".backup '$BACKUP_DIR/grafana.db'"
tar -czf "$BACKUP_DIR/configs.tar.gz" /etc/prometheus /etc/grafana

# 3. Dọn dẹp bản backup cũ hơn 7 ngày
find /var/backups/monitoring -maxdepth 1 -type d -mtime +7 -exec rm -rf {} +
echo "Backup hoàn tất tại $BACKUP_DIR"
```

Cấp quyền chạy và đặt lịch cron lúc 02:30 sáng mỗi ngày:

```bash
chmod +x /usr/local/bin/backup-monitoring.sh
(crontab -l 2>/dev/null; echo "30 2 * * * /usr/local/bin/backup-monitoring.sh >> /var/log/backup-mon.log 2>&1") | crontab -
```

## Quy trình khôi phục nhanh khi gặp sự cố

Khi chuyển sang VPS mới hoặc khôi phục sau lỗi ổ đĩa, dừng dịch vụ trước khi giải nén dữ liệu:

```bash
systemctl stop prometheus grafana-server
tar -xzf prometheus-tsdb.tar.gz --strip-components=1 -C /var/lib/prometheus/data/
cp grafana.db /var/lib/grafana/grafana.db
chown -R prometheus:prometheus /var/lib/prometheus/data
chown -R grafana:grafana /var/lib/grafana/grafana.db
systemctl start prometheus grafana-server
```

Nếu bạn đang vận hành cụm container, có thể xem thêm hướng dẫn [xử lý lỗi CrashLoopBackOff trên Kubernetes](https://thuecloud.vn/kubernetes-thuc-chien-xu-ly-loi-crashloopbackoff-tren-pod), tham khảo [bảng giá VPS](https://thuecloud.vn/pricing) và kiến trúc [giải pháp giám sát](https://thuecloud.vn/giai-phap).

> **Lưu trữ metric tốc độ cao trên KVM Enterprise NVMe tại ThueCloud.Vn:**
> Cụm server `vn-hn` sử dụng ổ cứng NVMe Enterprise giúp ghi/đọc chuỗi thời gian TSDB cực nhanh, **VAT 0%**, **SLA 99,9%** và **hoàn tiền 07 ngày**. Dễ dàng triển khai stack Prometheus + Grafana từ gói **VPS VN 2** (`78.000đ`/`90.000đ`) hoặc **VPS VN 3** (`139.000đ`/`170.000đ`/tháng).

## Câu hỏi thường gặp (FAQ)

### Bật `--web.enable-admin-api` trên Prometheus có gây mất an toàn không?
Có rủi ro nếu bạn mở port `9090` ra Internet vì Admin API cho phép xóa chuỗi thời gian hoặc tạo snapshot. Luôn bind `--web.listen-address=127.0.0.1:9090` và chỉ cho phép truy cập nội bộ hoặc qua Grafana.

### Tại sao không dùng lệnh `cp` cho `grafana.db` khi Grafana đang chạy?
Vì Grafana sử dụng SQLite ở chế độ ghi đồng thời; việc `cp` trực tiếp có thể sao chép đúng lúc transaction đang ghi dở, gây lỗi `database disk image is malformed` khi restore. Lệnh `sqlite3 .backup` sử dụng API khóa an toàn của SQLite.

### Nên giữ dữ liệu metric Prometheus trong bao lâu trên VPS?
Với VPS dung lượng NVMe vừa phải, bạn nên đặt `--storage.tsdb.retention.time=15d` hoặc `30d` để tối ưu dung lượng ổ đĩa, kết hợp backup snapshot hàng ngày ra object storage.

## References / Sources

- https://prometheus.io/docs/prometheus/latest/querying/api/#snapshot
- https://grafana.com/docs/grafana/latest/administration/back-up-grafana/

---

## Navigation & Machine-Readable Endpoints

- [Site Summary (llms.txt)](https://thuecloud.vn/llms.txt)
- [Full Technical Reference (llms-full.txt)](https://thuecloud.vn/llms-full.txt)
- [Pricing Catalog (Markdown)](https://thuecloud.vn/pricing.md)
- [Blog & Technical Guides (Markdown)](https://thuecloud.vn/blog.md)
- [XML Sitemap](https://thuecloud.vn/sitemap.xml)
