Preserve restic env for backup commands
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
import json
|
import json
|
||||||
|
import os
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from aiogram import F
|
from aiogram import F
|
||||||
from aiogram.types import Message, InlineKeyboardMarkup, InlineKeyboardButton, CallbackQuery
|
from aiogram.types import Message, InlineKeyboardMarkup, InlineKeyboardButton, CallbackQuery
|
||||||
@@ -30,6 +31,12 @@ async def _unit_status(unit: str, props: list[str]) -> dict[str, str]:
|
|||||||
return _parse_systemctl_kv(out)
|
return _parse_systemctl_kv(out)
|
||||||
|
|
||||||
|
|
||||||
|
def _sudo_cmd(cmd: list[str]) -> list[str]:
|
||||||
|
if os.geteuid() == 0:
|
||||||
|
return cmd
|
||||||
|
return ["sudo", "-E"] + cmd
|
||||||
|
|
||||||
|
|
||||||
def _load_json(raw: str, label: str) -> tuple[bool, object | None, str]:
|
def _load_json(raw: str, label: str) -> tuple[bool, object | None, str]:
|
||||||
if not raw or not raw.strip():
|
if not raw or not raw.strip():
|
||||||
return False, None, f"? {label} returned empty output"
|
return False, None, f"? {label} returned empty output"
|
||||||
@@ -200,7 +207,11 @@ async def cmd_backup_now(msg: Message):
|
|||||||
await msg.answer("▶️ Backup запущен", reply_markup=backup_kb)
|
await msg.answer("▶️ Backup запущен", reply_markup=backup_kb)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
rc, out = await run_cmd(["sudo", "/usr/local/bin/backup.py", "restic-backup"], timeout=6 * 3600)
|
rc, out = await run_cmd(
|
||||||
|
_sudo_cmd(["/usr/local/bin/backup.py", "restic-backup"]),
|
||||||
|
use_restic_env=True,
|
||||||
|
timeout=6 * 3600,
|
||||||
|
)
|
||||||
await msg.answer(("✅ OK\n" if rc == 0 else "❌ FAIL\n") + out, reply_markup=backup_kb)
|
await msg.answer(("✅ OK\n" if rc == 0 else "❌ FAIL\n") + out, reply_markup=backup_kb)
|
||||||
finally:
|
finally:
|
||||||
release_lock("backup")
|
release_lock("backup")
|
||||||
@@ -311,7 +322,11 @@ async def rc(msg: Message):
|
|||||||
|
|
||||||
async def job():
|
async def job():
|
||||||
await msg.answer("🧪 Restic check запущен", reply_markup=backup_kb)
|
await msg.answer("🧪 Restic check запущен", reply_markup=backup_kb)
|
||||||
rc2, out = await run_cmd(["sudo", "/usr/local/bin/restic-check.sh"], timeout=6 * 3600)
|
rc2, out = await run_cmd(
|
||||||
|
_sudo_cmd(["/usr/local/bin/restic-check.sh"]),
|
||||||
|
use_restic_env=True,
|
||||||
|
timeout=6 * 3600,
|
||||||
|
)
|
||||||
await msg.answer(("✅ OK\n" if rc2 == 0 else "❌ FAIL\n") + out, reply_markup=backup_kb)
|
await msg.answer(("✅ OK\n" if rc2 == 0 else "❌ FAIL\n") + out, reply_markup=backup_kb)
|
||||||
|
|
||||||
pos = await enqueue("restic-check", job)
|
pos = await enqueue("restic-check", job)
|
||||||
@@ -325,7 +340,11 @@ async def wr(msg: Message):
|
|||||||
|
|
||||||
async def job():
|
async def job():
|
||||||
await msg.answer("📬 Weekly report запущен", reply_markup=backup_kb)
|
await msg.answer("📬 Weekly report запущен", reply_markup=backup_kb)
|
||||||
rc2, out = await run_cmd(["sudo", "/usr/local/bin/weekly-report.sh"], timeout=3600)
|
rc2, out = await run_cmd(
|
||||||
|
_sudo_cmd(["/usr/local/bin/weekly-report.sh"]),
|
||||||
|
use_restic_env=True,
|
||||||
|
timeout=3600,
|
||||||
|
)
|
||||||
await msg.answer(("✅ OK\n" if rc2 == 0 else "❌ FAIL\n") + out, reply_markup=backup_kb)
|
await msg.answer(("✅ OK\n" if rc2 == 0 else "❌ FAIL\n") + out, reply_markup=backup_kb)
|
||||||
|
|
||||||
pos = await enqueue("weekly-report", job)
|
pos = await enqueue("weekly-report", job)
|
||||||
|
|||||||
Reference in New Issue
Block a user