-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
120 lines (87 loc) · 2.91 KB
/
main.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
from dotenv import load_dotenv
from enum import Enum
import datetime
import shutil
import git
import sys
import os
import re
# config
load_dotenv(override=True)
class Config(Enum):
REPOSITORY = str(os.getenv('REPOSITORY_URL'))
ORIGINAL_DIR = str(os.getenv('ORIGINAL_DIR'))
BACKUP_DIR = str(os.getenv('BACKUP_DIR')) if os.getenv('BACKUP_DIR') is not None else 'backup'
BACKUP_USER = re.sub(r' ', '', str(os.getenv('BACKUP_USER'))).split(',') if os.getenv('BACKUP_USER') is not None else []
FLOW_DIR = BACKUP_DIR + '/flow_service'
# methods
def get_date():
t_delta = datetime.timedelta(hours=9)
jst = datetime.timezone(t_delta, 'JST')
now = datetime.datetime.now(jst)
return '%s %s' % (now.strftime('%Y-%m-%d'), now.strftime('%H:%M:%S'))
# init
def init():
# check dir
os.makedirs(Config.FLOW_DIR.value, exist_ok=True)
# git init and create remote
repo = git.Repo.init(Config.BACKUP_DIR.value)
if 'origin' not in map(lambda r: r.name, repo.remotes):
remote = repo.create_remote(name='origin', url=Config.REPOSITORY.value)
print(remote)
print('initialization ok!')
# backup
def backup_flow_service():
# remove folder
shutil.rmtree(Config.FLOW_DIR.value)
# backup flow service
user_list = os.listdir(f"{Config.ORIGINAL_DIR.value}/home")
print(f"backup user: {Config.BACKUP_USER.value}")
# flow file
for user in user_list:
if user in Config.BACKUP_USER.value:
shutil.copytree(f"{Config.ORIGINAL_DIR.value}/home/{user}", f"{Config.FLOW_DIR.value}/home/{user}")
# schedule data
shutil.copytree(f"{Config.ORIGINAL_DIR.value}/data", f"{Config.FLOW_DIR.value}/data")
def generate_readme_and_ignore_file():
date = get_date()
user_list = ''
for user in Config.BACKUP_USER.value:
user_list += f"- {user}\n"
readme = open('template/README.md', 'r', encoding="utf-8")
text = readme.read()
readme.close()
text = text.replace('{date}', date)
text = text.replace('{user_list}', user_list)
# create README.md
with open('%s/README.md' % (Config.BACKUP_DIR.value), 'w') as file:
file.write(text)
# create .gitignore
shutil.copy('template/.gitignore', f"{Config.BACKUP_DIR.value}/.gitignore")
def git_commit_and_push(mode):
message = 'backup: %s' % (get_date())
# git commit
repo = git.Repo(Config.BACKUP_DIR.value)
repo.git.add('.')
commit = repo.git.commit('.', message=message)
print(commit)
if mode != 'local':
push = repo.git.push('origin', 'master')
print(push)
print('commit and push ok!')
# main
def main():
# initializaion
init()
# backup
backup_flow_service()
# generate README.md
generate_readme_and_ignore_file()
# commit and push
try:
mode = sys.argv[1]
except IndexError as e:
mode = ""
git_commit_and_push(mode)
if __name__ == '__main__':
main()