55 lines
1.9 KiB
Python
55 lines
1.9 KiB
Python
from apscheduler.schedulers.background import BlockingScheduler
|
|
from datetime import datetime
|
|
import subprocess
|
|
from app.models import *
|
|
from django.db import close_old_connections
|
|
|
|
#=====================================================================
|
|
def sync_files():
|
|
# close old connections
|
|
close_old_connections()
|
|
|
|
print("===Start sync files===", datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
|
|
password = 'qabeNMHRXhiHAiq'
|
|
source = "bigdata@5.223.62.175:/home/bigdata/filestore/y99/"
|
|
target = "./static/backup/y99prod"
|
|
ssh_port = "234"
|
|
|
|
cmd = [
|
|
"sshpass", "-p", password,
|
|
"rsync", "-avz", "--delete",
|
|
"-e", f"ssh -p {ssh_port}",
|
|
source, target
|
|
]
|
|
# start time
|
|
start_time = datetime.now()
|
|
try:
|
|
subprocess.run(cmd, check=True)
|
|
print("✅ Sync thành công")
|
|
print("===End sync files===", datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
|
|
backup = Backup(code="FILE{}".format(datetime.now().strftime('%Y%m%d%H%M')), name="file-backup",
|
|
status=Task_Status.objects.get(pk=4), start_time=start_time, end_time=datetime.now())
|
|
backup.save()
|
|
|
|
except subprocess.CalledProcessError as e:
|
|
print("❌ Lỗi khi sync:", e)
|
|
backup = Backup(code="FILE{}".format(datetime.now().strftime('%Y%m%d%H%M')), name="file-backup",
|
|
status=Task_Status.objects.get(pk=3), start_time=start_time, end_time=datetime.now())
|
|
backup.save()
|
|
|
|
#=====================================================================
|
|
# scheduler
|
|
scheduler = BlockingScheduler()
|
|
|
|
# backup files
|
|
scheduler.add_job(sync_files,
|
|
'cron',
|
|
hour='7,12,20', # chạy lúc 7h,12h,20h
|
|
minute=0,
|
|
timezone='Asia/Ho_Chi_Minh'
|
|
)
|
|
|
|
# scheduler.add_job(sync_files, name="sync-files", timezone='Asia/Ho_Chi_Minh', next_run_time=datetime.now())
|
|
|
|
#=====================================================================
|
|
scheduler.start() |