/
rsyncData.py
executable file
·173 lines (134 loc) · 5 KB
/
rsyncData.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Rsync local data (firefox and thunderbird) to another disk in order to be backuped.
"""
import os
import re
import subprocess
import sys
import socket
from optparse import OptionParser
sys.path.append('/home/greg/Tools/env/pythonCommon')
from basic import getConfigDir
from log import LogClass
from program import Program
from mail import SendMail
# from network import checkAddress, getIp
##############################################
# Global variables
##############################################
prog_name = 'rsyncData'
data_dir = "/media/perso/data/"
# flag
pc_name = None
##############################################
# Line Parsing ##
##############################################
parser = OptionParser()
parser.add_option(
"--debug",
action="store_true",
dest="debug",
default=False,
help="Display all debug information."
)
parser.add_option(
"--nogui",
action="store_true",
dest="nogui",
default=False,
help="Don't launch GUI."
)
parser.add_option(
"--dry_run",
action="store_true",
dest="dry_run",
default=False,
help="Just print what it does."
)
(parsed_args, args) = parser.parse_args()
##############################################
##############################################
# Class
##############################################
class Rsync(object):
def __init__(self, tool):
self.tool = tool
self.source = str()
self.destination = str()
def getDirName(self):
""" get the directories names of source and target"""
self.source = os.path.join(data_dir, self.tool, pc_name) + "/"
self.destination = str()
self.destination += os.path.join(getConfigDir(), self.tool, pc_name)
# check if source exists
source_wo_star = re.sub("/\*", "", self.source)
logger.debug("self.source = %s, source_wo_star = %s" % (self.source, source_wo_star))
if not os.path.isdir(source_wo_star):
logger.error("For tool %s, source doesn't exist : %s" % (self.tool, source_wo_star))
program.stopRunning()
sys.exit(1)
# create destination if necessary
if not os.path.isdir(self.destination):
os.makedirs(self.destination)
def rsyncData(self):
# construct command
cmd = ["/usr/bin/rsync", "-rtlpgovb", "--progress", "--delete",
self.source, self.destination]
if parsed_args.dry_run:
logger.info("Dry-run : rsync data with command = %s" % str(" ".join(cmd)))
else:
logger.info("Run synchronisation for tool %s with command = %s" % (self.tool, str(" ".join(cmd))))
# run synchronisation
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
msg, err = proc.communicate()
# analyse
if proc.returncode != 0:
logger.error("Synchronisation failed.\nError with command %s\n\nMessage :\n%s"
% (str(" ".join(cmd)), msg.decode('utf-8', errors="ignore")))
mail.send(subject="Synchronisation failed with command '%s'" % str(" ".join(cmd)),
message="See log file %s\n\nMessage :\n%s"
% (logC.getLogFile(), msg.decode('utf-8', errors="ignore")),
code=2)
program.stopRunning()
sys.exit(1)
else:
logger.debug("Program output : \n%s" % msg.decode('utf-8', errors="ignore"))
logger.info("Program error : \n%s" % err.decode('utf-8', errors="ignore"))
def run(self):
self.getDirName()
self.rsyncData()
def copyLocalData():
""" copy local data to home directory """
Rsync("thunderbird").run()
Rsync("firefox").run()
if not parsed_args.dry_run:
# Update config file
program.runToday()
def main():
global pc_name
pc_name = socket.gethostname()
# config file name
config_file = os.path.join(getConfigDir(), prog_name, prog_name + "_" + pc_name + ".cfg")
program.set_config_file(config_file)
# check if synchronisation is not currently running
if not program.isRunning():
program.startRunning()
# check if it has not already been launched in the last 7 days
if not program.isLaunchedLastDays(days=7):
# Rsync local data
copyLocalData()
program.stopRunning(stop_program=False)
# be sure that synchronisation is launched regularly
if not program.isLaunchedLastDays(days=14):
mail.send(subject="Not launched since more than 14 days",
message="See log file %s\nSee config file %s" % (logC.getLogFile(), config_file),
code=1)
program.stopLog()
if __name__ == '__main__':
logC = LogClass(prog_name, parsed_args.debug)
logger = logC.getLogger()
mail = SendMail(prog_name=prog_name)
program = Program(prog_name=prog_name)
main()