You are not logged in.
do with this as you will, I just like it
#Imports
import schedule
from datetime import datetime
import time
import os
from pathlib import Path
import subprocess
#Options
#--Global
ErrorLog = "/vmdisk/log/error"
#--Global
#--OSUpdate
OSUpdateLog = "/vmdisk/log/osupdate"
OSUpdateCommand = 'pacman -Syu --noconfirm'
#--OSUpdate
#--Backup
BackupSource = "/vmdisk"
BackupTarget = "/backup"
BackupSnapTMP = BackupSource + "/snapshot"
BackupSnapCMD = "btrfs subvolume snapshot " + BackupSource + " " + BackupSnapTMP
BackupLog = "/vmdisk/log/backup"
BackupCleanCMD = "rm -rf " + BackupSnapTMP
#--Backup
#--ConfigBackup
ConfigPath = BackupSnapTMP + "/xml"
ConfigCMD = "virsh dumpxml "
#--ConfigBackup
#--Heartbeat
HeartbeatLog = "/vmdisk/log/heartbeat"
#--Heartbeat
#--WeeklyBackup
WeeklyBackupTarget = "/weekly"
WeeklyBackupCMD = "cp "
WeeklyBackupLog = "/vmdisk/log/weekly"
#--WeeklyBackup
#Functions
def Log(Message, LogLocation):
logfile = open(LogLocation, "a")
logfile.write(Message + "\n")
logfile.close()
def OSUpdate():
Log("Tick " + datetime.today().strftime("%d-%m-%Y %H:%M:%S"), OSUpdateLog)
Log(os.popen(OSUpdateCommand).read().rstrip(), OSUpdateLog)
Log("Tock " + datetime.today().strftime("%d-%m-%Y %H:%M:%S"), OSUpdateLog)
def DiskChecks(CheckTarget):
CheckDiskMounted = os.path.ismount(CheckTarget)
if CheckDiskMounted:
os.chdir(CheckTarget)
DiskCheck = os.statvfs(CheckTarget)
CheckFiles = sorted(os.listdir(os.getcwd()), key=os.path.getmtime)
NewestOutFile = CheckFiles[-1]
MinOutFree = os.path.getsize(NewestOutFile) * 4
CheckDiskFree = (DiskCheck.f_frsize * DiskCheck.f_bfree)
if CheckDiskFree > MinOutFree:
return True
else:
return False
Log("CheckDisk:" + CheckTarget + " Too Full" + datetime.today().strftime("%d-%m-%Y %H:%M:%S"), ErrorLog)
else:
Log("CheckDisk: " + CheckTarget + " Not Mounted" + datetime.today().strftime("%d-%m-%Y %H:%M:%S"), ErrorLog)
return False
def BackupConfig():
os.mkdir(ConfigPath)
ConfigVMS = os.popen("virsh list --all | awk '$1 ~ /^[0-9][0-9]*$/ { print $2}'").read().rstrip().split('\n')
for ConfigVM in ConfigVMS:
ConfigOutVMConf = os.popen(ConfigCMD + ConfigVM).read()
ConfigOutFile = ConfigPath + "/" + ConfigVM + ".xml"
if os.path.exists(ConfigOutFile):
os.remove(ConfigOutFile)
ConfigOutFileWrite = open(ConfigOutFile, "w")
ConfigOutFileWrite.write(ConfigOutVMConf)
ConfigOutFileWrite.close()
def Backup():
Log("Tick " + datetime.today().strftime("%d-%m-%Y %H:%M:%S"), BackupLog)
if DiskChecks(BackupTarget):
BackupOutPath = BackupTarget + "/" + datetime.today().strftime("%d-%m-%Y") + ".tar.bzip2"
BackupOutCMD = "tar -cf " + BackupOutPath + " " + BackupSnapTMP + "/* --use-compress-program=lbzip2"
BackupOutExist = os.path.exists(BackupOutPath)
if not BackupOutExist:
Log(" Snapshot " + BackupSnapCMD, BackupLog)
os.popen(BackupSnapCMD).read()
Log(" VMConfig", BackupLog)
BackupConfig()
Log(" Compress " + BackupOutCMD, BackupLog)
os.popen(BackupOutCMD).read()
Log(" Remove Snapshot", BackupLog)
os.popen(BackupCleanCMD).read()
Log("Tock " + datetime.today().strftime("%d-%m-%Y %H:%M:%S"), BackupLog)
else:
Log(" Checks Failed, Throwing error", BackupLog)
Log("Tock " + datetime.today().strftime("%d-%m-%Y %H:%M:%S"), BackupLog)
def Heartbeat():
Log("Tick\nTock " + datetime.today().strftime("%d-%m-%Y %H:%M:%S"), HeartbeatLog)
def WeeklyBackup():
Log("Tick " + datetime.today().strftime("%d-%m-%Y %H:%M:%S"), WeeklyBackupLog)
if DiskChecks(WeeklyBackupTarget):
Log(" Run", WeeklyBackupLog)
os.chdir(BackupTarget)
CheckFiles = sorted(os.listdir(os.getcwd()), key=os.path.getmtime)
NewestBackupFile = CheckFiles[-1]
WeeklyOut = WeeklyBackupTarget + "/Weekly" + datetime.today().strftime("%d-%m-%Y") + ".tar.bzip2"
os.popen(WeeklyBackupCMD + BackupTarget + "/" + NewestBackupFile + " " + WeeklyOut).read()
Log("Tock " + datetime.today().strftime("%d-%m-%Y %H:%M:%S"), WeeklyBackupLog)
#Schedules
#--Updates
schedule.every().friday.at("20:00").do(OSUpdate)
#--Updates
#--Backups
schedule.every().day.at("20:00").do(Backup)
#--Backups
#--Heartbeat
schedule.every().hour.do(Heartbeat)
#--Heartbeat
#--WeeklyBackup
schedule.every().friday.at("22:00").do(WeeklyBackup)
#--WeeklyBackup
#GoFastBit
while True:
schedule.run_pending()
time.sleep(1)
Offline