-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup-app.py
96 lines (69 loc) · 2.24 KB
/
backup-app.py
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
96
#! /usr/bin/env python
#
# For the full copyright and license information, please view the LICENSE
# file that was distributed with this source code.
#
# Copyright 2010 (c) Augusto Morais
# All rights reserved.
__author__="Madkourone at gmail.com"
__date__ ="$Jan 11, 2010 6:14:02 PM$"
class Compress:
def doCompress(self, files, path = "/home/"):
import tarfile
import os
import datetime
from datetime import datetime
#try:
dirformat = datetime.strftime(datetime.today(), "%Y%m%d-%H%M")
os.makedirs("/home/aflavio/backups/applications/" + dirformat)
self.dirsSuccess = []
print ("\n\nIniciando backup dos Aplicativos...")
for file in files:
compressedFile = tarfile.open("/home/aflavio/backups/applications/" + dirformat + "/" + file + ".tar.gz", "w:gz")
compressedFile.add(path + file)
compressedFile.close()
self.dirsSuccess += ["Backup com Sucesso: " + file]
print ("OK - " + file)
return self.dirsSuccess
class SysUtils:
def listDir(self, path = "/home"):
import os
dirList=os.listdir(path)
files=[]
for fname in dirList:
if fname is not None:
files+=[fname]
#excluding homes
files.remove("aflavio")
return files
def createFile(self):
import os
import datetime
from datetime import datetime
try:
os.makedirs("/home/aflavio/backups/log/")
except:
pass
fileformat = datetime.strftime(datetime.today(), "%Y%m%d-%H%M") + ".log"
file = open("/home/aflavio/backups/log/" + fileformat, "w")
file.write ("#### BACKUP LOG ####\n")
file.write ("#### CREATED AT: "+datetime.strftime(datetime.today(), "%d/%m/%Y %H:%M:%S")+" ####\n\n")
self.fileLogName = fileformat
self.fileHandle = file
return self.fileHandle
def insertMsg(self, message):
self.fileHandle.write (message + "\n")
def closeLog(self):
self.fileHandle.close()
SU = SysUtils()
files = SU.listDir("/home")
SU.createFile()
CompactFiles = Compress()
abc = CompactFiles.doCompress(files)
#Changing permission to AFLAVIO USER
import os
os.system('chown aflavio:aflavio -R /home/aflavio/backups')
for retorno in abc:
SU.insertMsg(retorno)
SU.insertMsg("#### Fim do log #####")
SU.closeLog()