Files
Sheep-Service/backup.py
2025-09-09 00:10:53 +03:00

37 lines
1.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import os
import requests
from datetime import datetime
from zipfile import ZipFile, ZIP_DEFLATED
from dotenv import load_dotenv
# Загрузка переменных из .env
load_dotenv()
TELEGRAM_TOKEN = os.getenv('TELEGRAM_TOKEN')
CHAT_ID = os.getenv('CHAT_ID')
DB_PATH = os.path.join(os.getenv('DB_PATH'), 'database.sqlite')
def send_document(filename, caption):
url = f'https://api.telegram.org/bot{TELEGRAM_TOKEN}/sendDocument'
with open(filename, 'rb') as f:
response = requests.post(
url,
data={'chat_id': CHAT_ID, 'caption': caption},
files={'document': f}
)
print(response.json())
def main():
if not TELEGRAM_TOKEN or not CHAT_ID or not DB_PATH:
print("Помилка: TELEGRAM_TOKEN, CHAT_ID або DB_PATH не задано в .env.")
return
if os.path.exists(DB_PATH):
timestamp = datetime.now().strftime("%d.%m.%Y %H:%M")
caption = f"Backup Sheep Service DB - {timestamp}"
send_document(DB_PATH, caption)
else:
print("ZIP file not created")
if __name__ == "__main__":
main()