-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackshield.py
More file actions
executable file
·143 lines (117 loc) · 5.03 KB
/
backshield.py
File metadata and controls
executable file
·143 lines (117 loc) · 5.03 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env python3
import argparse
import os
import sys
import shutil
import subprocess
from distutils.dir_util import copy_tree
from datetime import datetime
def run():
# Parse arguments
parser = argparse.ArgumentParser(description="Backshield is simple backup tool for server configurations with git.")
subparsers = parser.add_subparsers()
parser_init = subparsers.add_parser("init", help="Initialize with git repository url")
parser_init.add_argument('repository', type=str, metavar="REPOSITORY", help="git repository for backup")
parser_init.set_defaults(handler=command_init)
parser_add = subparsers.add_parser("add", help="Add file to backup targets")
parser_add.add_argument('file', type=str, metavar="FILE", help="filename")
parser_add.set_defaults(handler=command_add)
parser_remove = subparsers.add_parser("remove", help="Remove file in backup targets")
parser_remove.add_argument('file', type=str, metavar="FILE", help="filename")
parser_remove.set_defaults(handler=command_remove)
parser_list = subparsers.add_parser("list", help="Print backup targets")
parser_list.set_defaults(handler=command_list)
parser_backup = subparsers.add_parser("backup", help="Backup now")
parser_backup.add_argument('message', type=str, nargs="?", default="-", metavar="COMMIT_MESSAGE", help="commit message")
parser_backup.set_defaults(handler=command_backup)
# Run the command
args = parser.parse_args()
if hasattr(args, 'handler'):
args.handler(args)
else:
parser.print_help()
def command_init(args):
# Git clone
print("Run git clone: %s" % args.repository)
os.chdir(os.path.dirname(os.path.realpath(__file__)))
p = subprocess.Popen("git clone %s data" % args.repository, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
if p.returncode != 0:
print(err.decode("utf-8"))
sys.exit(1)
# Create config files if not exist
hostname = os.uname()[1]
print("Create config file and directory")
os.makedirs("data/%s" % hostname, exist_ok=True)
os.makedirs("data/_backshield", exist_ok=True)
open("data/%s/.gitkeep" % hostname, "a").close()
open("data/_backshield/%s.conf" % hostname, "a").close()
print("Initialized.")
def command_add(args):
# Get fullpath
abspath = os.path.abspath(args.file)
if os.path.exists(abspath) == False:
print("File not found: %s" % abspath)
sys.exit(1)
# Add file to config
hostname = os.uname()[1]
os.chdir(os.path.dirname(os.path.realpath(__file__)))
with open("data/_backshield/%s.conf" % hostname, "r+") as f:
watches = f.readlines()
if abspath in watches:
print("Already exist in configuration")
sys.exit(0)
f.write("%s\n" % abspath)
print("Added %s" % abspath)
def command_remove(args):
# Get fullpath
abspath = os.path.abspath(args.file)
# Remove file to config
hostname = os.uname()[1]
os.chdir(os.path.dirname(os.path.realpath(__file__)))
with open("data/_backshield/%s.conf" % hostname, "r+") as f:
watches = f.readlines()
f.seek(0)
f.truncate()
for watch in watches:
if watch.rstrip() == abspath:
if os.path.exists(watch):
os.remove("data/%s%s" % (hostname, abspath))
print("Removed %s" % abspath)
continue
f.write("%s\n" % watch.rstrip())
def command_list(args):
hostname = os.uname()[1]
os.chdir(os.path.dirname(os.path.realpath(__file__)))
with open("data/_backshield/%s.conf" % hostname, "r") as f:
watches = f.readlines()
watches.sort()
for watch in watches:
print(watch.rstrip())
def command_backup(args):
hostname = os.uname()[1]
os.chdir(os.path.dirname(os.path.realpath(__file__)))
with open("data/_backshield/%s.conf" % hostname, "r+") as f:
watches = f.readlines()
for watch in watches:
watch = watch.rstrip()
# Check exist file
if os.path.exists(watch) == False:
print("Warning: %s is not found." % watch)
continue
# Create Directory
os.makedirs("data/%s%s" % (hostname, os.path.split(watch)[0]), exist_ok=True)
# Copy directory/file
if os.path.isdir(watch):
copy_tree(watch, "data/%s%s" % (hostname, watch))
else:
shutil.copy(watch, "data/%s%s" % (hostname, watch))
# Git commit
timestr = datetime.now().strftime("%Y/%m/%d %H:%M:%S")
p = subprocess.Popen("cd data && git add -A; git diff-index --quiet HEAD || (git commit -m \"[%s] %s: %s\" ;git push origin master) && cd .." % (timestr, hostname, args.message), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
if p.returncode != 0:
print("git add/commit/push failed: ", err.decode("utf-8"), out.decode("utf-8"))
sys.exit(1)
print("Completed.")
run()