File: ZBackupExecutor.py

package info (click to toggle)
zbackup 1.5-4
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 868 kB
  • sloc: cpp: 6,957; ansic: 468; python: 207; makefile: 10
file content (95 lines) | stat: -rw-r--r-- 2,866 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env python3

import sys, argparse, os, time, datetime, subprocess, io, re, argparse, random
import Logger


EMPTY_BYTES = bytearray ()


class ZBackupExecutor:
    def __init__(self, directory, zbackup, encryptionKey):    
        self.directory = directory
        self.zbackup = zbackup

        if encryptionKey is None:
            self.encryptionArgument = "--non-encrypted" 
        else:
            self.encryptionArgument = "--password-file "  + encryptionKey
            

    def __str__(self):
        return "ZBackupExecutor[" + self.zbackup + " " + self.encryptionArgument + " " + self.directory + "]";


    def executeZBackup (self, arguments, bytesToSTDIN):
        cCmd = self.zbackup + " " + self.encryptionArgument + " --silent " + arguments
        Logger.log (3, "        executeZBackup:" + cCmd)

        proc = subprocess.Popen(cCmd, shell = True, stdin = subprocess.PIPE, stdout = subprocess.PIPE)
        (stdout_data, stderr_data) = proc.communicate(input = bytesToSTDIN)

        proc.wait()           

        return stdout_data


    def getSizeInBytes (self):
        cmd="du -s %s" % self.directory
        size=subprocess.Popen([cmd], stdout=subprocess.PIPE, shell=True).communicate()[0].split()[0]

        return int(size) * 1024


    def getFileExists (self, fileName):
        fullFileName = self.directory + "/backups/" + fileName

        return os.path.exists(fullFileName)


    def getFilePath (self, fileName):
        fullFileName = self.directory + "/backups/" + fileName
        dirName = os.path.dirname(fullFileName)

        if not os.path.exists(dirName):
            Logger.log (3, "mkdir " + dirName)
            os.makedirs(dirName, exist_ok=True)

        return fullFileName     


    def writeZBackupFile (self, bytesource, fileName):
        theBytes = bytesource.getBytes ()
        
        self.executeZBackup ("backup '" + self.getFilePath (fileName) + "'", theBytes)



    def readZBackupFile (self, bytesource, fileName):
        restoredBytes = self.executeZBackup ("restore '" + self.getFilePath (fileName) + "'", EMPTY_BYTES)

        if not restoredBytes == bytesource.getBytes ():
            raise Exception("Bytes don't match")   


    def initZBackup (self):
        ignoreBytes = self.executeZBackup ("init " + self.directory, EMPTY_BYTES)


    def compactZBackupIndex (self):
        Logger.log (1, "Compact the zbackup index")
        ignoreBytes = self.executeZBackup (" gc -O gc.concat " + self.directory, EMPTY_BYTES)


    def garbageCollectZBackup (self):
        Logger.log (1, "Garbage collect zbackup")
        ignoreBytes = self.executeZBackup ("gc " + self.directory, EMPTY_BYTES)


    def removeFile (self, fileName):
        Logger.log (1, "Remove file " + fileName)
        
        fullFileName = self.getFilePath (fileName)
        os.remove (fullFileName)