import datetime
import logging
from paho.mqtt import client as mqtt_client
import getopt
import json
import time
import subprocess
import sys
import os
pid = os.getpid()


cmnd = "ps -ef|grep omv_backups.py|grep -v grep |grep -v {}|wc -l".format(pid)
status, output = subprocess.getstatusoutput(cmnd)

print(output)
if int(output) > 0:
    print("Running already!")
    sys.exit()
broker = '192.168.77.106'
port = 1883
topic_sum = "sectorq/omv/backups"
mqtt_username = 'jaydee'
mqtt_password = 'jaydee1'

try:
    opts, args = getopt.getopt(sys.argv[1:], "am", ["command=", "help", "output="])
except getopt.GetoptError as err:
    #usage()
    sys.exit(2)
output = None
# QJ : getopts
_MODE = "manual"
for o, a in opts:
    if o == "-a":
        _MODE = "auto"
    elif o in ("-m", "--manual"):
       _MODE = "manual"       
        

client = mqtt_client.Client()
client.username_pw_set(mqtt_username, mqtt_password)
client.connect(broker,1883,60)
now = datetime.datetime.now()
STARTTIME = now.strftime("%Y-%m-%d_%H:%M:%S")
msg = {"mode":_MODE, "status":"started","bak_name":"complete","start_time":STARTTIME,"end_time":"in progress","progress":0}
client.publish(topic_sum, json.dumps(msg));
client.disconnect()

backups = {
    "github":
        {"source":"admin@192.168.77.106:/share/Data/__GITHUB",
         "exclude":"",
         "active": True
        },
    "photo": {
          "source":"admin@192.168.77.106:/share/Photo/Years",
          "exclude":"",
          "active":True
    },
    "docker_data":{
        "source":"admin@192.168.77.106:/share/docker_data/",
        "exclude":"",
        "active":True
    }
}
BACKUP_FS = "/srv/dev-disk-by-uuid-02fbe97a-cd9a-4511-8bd5-21f8516353ee"
for b in backups:
    topic = "sectorq/omv/backups/{}".format(b.lower())
    if not backups[b]["active"]:
        print("Backup {} is not active!".format(b))
        client.connect(broker,1883,60)
        msg = {"status":"inactive","bak_name":b,"start_time":"inactive","end_time":"inactive","progress":0}
        
        client.publish(topic, json.dumps(msg))
        client.disconnect()
        continue
    
    SOURCE_DIR = backups[b]["source"]
    now = datetime.datetime.now()
    BACKUP_DIR="/srv/dev-disk-by-uuid-02fbe97a-cd9a-4511-8bd5-21f8516353ee/{}".format(b)
    DATETIME = now.strftime("%Y-%m-%d_%H:%M:%S")
    BACKUP_PATH="{}/{}".format(BACKUP_DIR, DATETIME)
    LATEST_LINK="{}/latest".format(BACKUP_DIR)
    msg = {"status":"started","bak_name":b,"start_time":DATETIME,"end_time":"in progress", "progress":0}
    client.connect(broker,1883,60)
    client.publish(topic, json.dumps(msg));
    client.disconnect()

    cmnd = "mkdir -p {}".format(BACKUP_DIR)
    status, output = subprocess.getstatusoutput(cmnd)
    print(cmnd)


    #cmnd = "rsync -av --delete {}/ --link-dest {} --exclude=\".cache\" {}".format(SOURCE_DIR, LATEST_LINK, BACKUP_PATH)
    cmnd = "rsync --info=progress2 -avz --delete {} --link-dest {} --exclude=\".cache\" --exclude=\".git\" --exclude=\"var_lib_motioneye\" {}".format(SOURCE_DIR, LATEST_LINK, BACKUP_PATH)
    print(cmnd)
    status, output = subprocess.getstatusoutput(cmnd)
    cmnd = "rm -rf {}".format(LATEST_LINK)
    print(cmnd)
    status, output = subprocess.getstatusoutput(cmnd)
    
    cmnd = "ln -s {} {}".format(BACKUP_PATH, LATEST_LINK)
    print(cmnd)
    status, output = subprocess.getstatusoutput(cmnd)
    
    
    #Remove old 
    print("Removing old dirs")
    
    #cmnd = "find {} -maxdepth 1 -type d -mtime +30 -exec rm -rf {{}} \;".format(BACKUP_DIR)
    cmnd = "find {} -maxdepth 1 -type d -mmin +30 -exec rm -rf {{}} \;".format(BACKUP_DIR)
    print(cmnd)
    #status, output = subprocess.getstatusoutput(cmnd)
    now = datetime.datetime.now()
    ENDTIME = now.strftime("%Y-%m-%d_%H:%M:%S")
    msg = {"status":"finished","bak_name":b,"start_time":DATETIME,"end_time":ENDTIME,"progress":0}
    client.connect(broker,1883,10)
    client.publish(topic, json.dumps(msg))
    client.disconnect()

print("Getting size of FS")
#cmnd = "du -h --max-depth=0 {}".format(BACKUP_FS)
cmnd = "df -h /srv/dev-disk-by-uuid-02fbe97a-cd9a-4511-8bd5-21f8516353ee |awk '{ print $3 }'|tail -1"
status, output = subprocess.getstatusoutput(cmnd)
used_space = (output.split())[0]
now = datetime.datetime.now()
ENDJOB = now.strftime("%Y-%m-%d_%H:%M:%S")
print("Size : {}".format(used_space))
print("Sending finished status")
msg = {"mode":_MODE,"status":"finished","bak_name":"complete","start_time":STARTTIME,"end_time":ENDJOB,"progress":0,"used_space":used_space}
print(msg)
client.connect(broker,1883,10)
client.publish(topic_sum, json.dumps(msg))
client.disconnect()

if _MODE == "auto":
    cmnd = "systemctl suspend"
    status, output = subprocess.getstatusoutput(cmnd)