Tự động dọn dẹp backup SQL Server local & OneDrive với thông báo Telegram
| Tính năng | Mô tả |
|---|---|
| Local Cleanup | Xóa file backup cũ (>.5h) trên ổ cứng |
| Cloud Cleanup | Dọn Recycle Bin OneDrive qua Graph API |
| Storage Monitor | Kiểm tra dung lượng OneDrive (Active + Recycle Bin) |
| Telegram Alert | Gửi thông báo tóm tắt sau mỗi lần chạy |
- PowerShell 7.0+ - Download
- Azure AD App với quyền
Sites.FullControl.All - Telegram Bot (tùy chọn)
# 1. Clone repo
git clone https://github.com/iatoi/auto-backup-sql-cleanup.git
cd auto-backup-sql-cleanup
# 2. Tạo config
cp config/config.template.json config/config.json
# 3. Điền thông tin vào config.json
# 4. Chạy
pwsh -ExecutionPolicy Bypass -File src/AutoClean_SQL.ps1{
"AzureAD": {
"TenantId": "xxx-xxx-xxx",
"ClientId": "xxx-xxx-xxx",
"ClientSecret": "your-secret"
},
"OneDrive": {
"SiteUrl": "https://tenant-my.sharepoint.com/personal/user_domain",
"BackupFolderPath": "Backup.SQL"
},
"LocalBackup": {
"Path": "D:\\BFC\\Backup\\",
"RetentionMinutes": 300,
"Extensions": [".bak", ".zip", ".7z"]
},
"CloudRecycleBin": {
"FirstStageRetentionDays": 3,
"RowLimit": 5000
},
"Telegram": {
"BotToken": "123456:ABC-xyz",
"ChatId": "987654321"
},
"StorageAlert": {
"WarningThresholdPercent": 70
}
}- Mở Telegram, tìm @BotFather
- Gửi
/newbot→ nhận Bot Token - Gửi tin nhắn cho bot của bạn
- Truy cập:
https://api.telegram.org/bot<TOKEN>/getUpdates - Tìm
"chat":{"id":XXXXXX}→ đó là Chat ID
Thông báo mẫu:
✅ BFC SQL BACKUP
🟢 1015.53GB con (0.8% used)
📂 10 files: 20260104, 20260103
🗑️ 134 items (3-day safe)
| Emoji | Ý nghĩa |
|---|---|
| ✅/ |
Trạng thái: OK/Warning/Failed |
| 🟢/🟡/🔴 | Dung lượng: <50%/<80%/>80% |
| 📂 | Số file backup trên OneDrive |
| 🗑️ | Số items trong Recycle Bin |
# Program: pwsh.exe
# Arguments: -ExecutionPolicy Bypass -File "D:\Scripts\AutoClean_SQL\src\AutoClean_SQL.ps1"
# Start in: D:\Scripts\AutoClean_SQL\- ☑️ Run whether user is logged on or not
- ☑️ Run with highest privileges
auto-backup-sql-cleanup/
├── src/AutoClean_SQL.ps1 # Script chính
├── config/
│ ├── config.template.json # Template (commit)
│ └── config.json # Config thực (gitignore)
├── logs/ # Log files
└── README.md
Để tránh lưu mật khẩu dạng plaintext trong config.json, hãy sử dụng chế độ setup bảo mật:
# 1. Chạy setup (chỉ cần làm 1 lần)
pwsh -ExecutionPolicy Bypass -File src/AutoClean_SQL.ps1 -SetupSecurity
# 2. Nhập Client Secret và Bot Token khi được hỏi
# Script sẽ tự động mã hóa và lưu vào file .encrypted
# File này chỉ có thể được giải mã trên Máy tính này bởi User này.Sau khi setup, bạn có thể xóa ClientSecret và BotToken trong config.json. Script sẽ tự động ưu tiên đọc file encrypted.
⚠️ KHÔNG commit config.json - chứa thông tin nhạy cảm!
# Nếu lỡ commit
git rm --cached config/config.json
git commit -m "Remove config"- 🐛 FIX: OneDrive Storage Quota tính thiếu dung lượng Recycle Bin
- 🔧 UPDATE: Sử dụng endpoint
/users/{email}/driveđể lấy tổng dung lượng chính xác (khớp Web UI)
- 🔒 Thêm chế độ bảo mật
-SetupSecurity(Windows DPAPI) - ✨ Hỗ trợ mã hóa Client Secret & Bot Token
- ✨ Format Telegram message đẹp hơn với emoji
- ✨ Hiển thị files backup trên OneDrive folder
- ✨ Cấu hình BackupFolderPath
- ✨ Thêm Telegram notification
- ✨ Thêm storage monitoring
- 🔄 Chuyển từ PnP.PowerShell sang Microsoft Graph API
Developed by BFC DevOps Team | 2026