Skip to content

Commit b4b8ab9

Browse files
committed
Uploading code again for v1.0
1 parent fe7359c commit b4b8ab9

2 files changed

Lines changed: 63 additions & 183 deletions

File tree

rsync.py

Lines changed: 18 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
#!/usr/bin/env python3
2+
# Version 1.0
23
# Author: chomes@github
3-
# Current version: 1.0
4-
# Version 1.0 basic rsync with copying locally and remotely.
5-
# Version 1.1 config addition and automation of script.
6-
# Future versions:
7-
# Version 1.2 lock file to stop same backup running, along with a way of telling if the backup is still running.
8-
# Version 1.3 email notification
9-
# Importing functions
4+
# importing functions
105
import syncfuncs
116
from shutil import which
12-
from pathlib import Path
13-
import configparser
7+
148

159
# Checking if user has rsync installed
1610
if which("rsync") is not None:
@@ -27,30 +21,19 @@
2721
exit()
2822

2923

30-
# If statement to determine manual or automated backup
31-
config_file = Path("config.ini")
32-
if config_file.is_file():
33-
config = configparser.ConfigParser()
34-
config.read("config.ini")
35-
if 'Manual' in config:
36-
syncfuncs.lsyauto()
37-
elif 'LoRem' in config:
38-
syncfuncs.loreauto()
39-
elif 'RemLo' in config:
40-
syncfuncs.reloauto()
41-
else:
42-
print("Config not found, manual backup is starting")
43-
# Welcome message
44-
print("Welcome to the Backup script, this will allow you to choose whether you want to back up locally or remotely")
45-
print("You can choose to either back up from remote to local or local to remote it's entirely your choice")
4624

47-
# remote or local backup options
48-
remlo = input("Is this a back up locally from one place to another or a remote server backup? (local OR remote)")
49-
if remlo == 'local':
50-
syncfuncs.localsyn()
51-
elif remlo == 'remote':
52-
resync = input("Are you copying from local to remote server or remote server to local? (local OR remote)")
53-
if resync == 'remote':
54-
syncfuncs.remlocal()
55-
elif resync == 'local':
56-
syncfuncs.localrem()
25+
# Welcome message
26+
print("Welcome to the Backup script, this will allow you to choose whether you want to back up locally or remotely")
27+
print("\n")
28+
print("You can choose to either back up from remote to local or local to remote it's entirely your choice")
29+
print("\n")
30+
# remote or local backup options
31+
remlo = input("Is this a back up local backup or are you backing up to a remote server? (local OR remote) ").lower()
32+
if remlo == 'local':
33+
syncfuncs.localsyn()
34+
elif remlo == 'remote':
35+
resync = input("Is the copy from local or is it from a remote server? (local OR remote) ").lower()
36+
if resync == 'remote':
37+
syncfuncs.remlocal()
38+
elif resync == 'local':
39+
syncfuncs.localrem()

syncfuncs.py

Lines changed: 45 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,26 @@
44
import time
55
import subprocess
66
from subprocess import call
7-
import configparser
87
import getpass
98
import apt
109

1110

12-
# Function for apt install of package
11+
# Package for installing apt
1312
def install_apt(pkg_name):
1413
cache = apt.Cache()
1514
cache.update()
1615
pkg = cache[pkg_name]
1716
pkg.mark_install()
1817

1918

20-
# Function for yum install of package
2119
def install_yum(pkg_name):
22-
rsync_install = ["yum", "install", pkg_name, "-y"]
20+
rsync_install = ['yum install %s -y' % (pkg_name)]
2321
call(rsync_install)
2422

2523

2624
# Local sync function
2725
def sync_call_manual(startbk, sor, des, logloc):
2826
if startbk == 1:
29-
timestamp = time.strftime("%Y%m%d-%H%M")
30-
logloc = logloc + timestamp
31-
logloc = logloc + "-logfile"
3227
print("Starting backup, rsync -avv {} {} --log-file={}".format(sor, des, logloc))
3328
lclresyn = ["rsync", "-avv", sor, des, "--log-file", logloc]
3429
call(lclresyn)
@@ -116,35 +111,20 @@ def localsyn():
116111
logging = input("Do you want a log of the backup? (y or n)").lower()
117112
if logging == "n":
118113
logloc = ""
114+
print("No logs required running backup!")
119115
startbk = 2
120-
print("Saving to config for automation later")
121-
config = configparser.ConfigParser()
122-
config['Manual'] = {'bkoption': startbk,
123-
'source': sor,
124-
'destination': des,
125-
'log_location': logloc}
126-
with open('config.ini', 'w') as configfile:
127-
config.write(configfile)
128-
configfile.close()
129-
print("Config file saved. No logs required running backup!")
130116
sync_call_manual(startbk, sor, des, logloc)
131117
break
132118
elif logging == "y":
133119
logloc = input("Please type the destination of the log: ")
134120
if logloc.endswith("/"):
135121
conf_log = input("Are you happy with the destination {} ? y or n ".format(logloc)).lower()
136122
if conf_log == "y":
123+
timestamp = time.strftime("%Y%m%d-%H%M")
124+
logloc = logloc + timestamp
125+
logloc = logloc + "-logfile"
137126
startbk = 1
138-
print("Saving to config for automation later")
139-
config = configparser.ConfigParser()
140-
config['Manual'] = {'bkoption': startbk,
141-
'source': sor,
142-
'destination': des,
143-
'log_location': logloc}
144-
with open('config.ini', 'w') as configfile:
145-
config.write(configfile)
146-
configfile.close()
147-
print("Config saved. Ok, lets run the backup!")
127+
print("Ok lets run the backup!")
148128
sync_call_manual(startbk, sor, des, logloc)
149129
break
150130
else:
@@ -158,10 +138,20 @@ def localsyn():
158138
# local to remote server backup
159139
def localrem():
160140
print("Now we will choose the source and destination")
161-
sor = input("What is the FULL PATH to the directory you want to backup? ")
162-
des = input("What is the FULL PATH to the directory you want to copy to? ")
163-
usn = input('''Please state the user for the backup,
164-
if you don't state one we'll use the default one {} '''.format(getpass.getuser()))
141+
# Loop to make sure directory ends with /
142+
while True:
143+
sor = input("What is the FULL PATH to the directory you want to backup? ")
144+
if sor.endswith("/"):
145+
break
146+
else:
147+
print("Directory must have a / at the end, please add one")
148+
while True:
149+
des = input("What is the FULL PATH to the directory you want to copy to? ")
150+
if des.endswith("/"):
151+
break
152+
else:
153+
print("Directory must have a / at the end, please add one")
154+
usn = input("What's the username? If none given we'll use the current {}".format(getpass.getuser()))
165155
if not usn:
166156
usn = getpass.getuser()
167157
remserv = input("What is the server you want to connect to? ")
@@ -174,21 +164,26 @@ def localrem():
174164
change = input("Did you want to make any changes? (source, destination, port, username, server, no)").lower()
175165
if change == 'source':
176166
sor = input("What is the FULL PATH to the directory you want to backup? ")
177-
print("We will copy from {} to {}".format(sor, des))
178-
continue
167+
if sor.endswith("/"):
168+
print("We will copy from {} to {}".format(sor, des))
169+
continue
170+
else:
171+
print("The directory must have a / please add one")
179172
elif change == 'destination':
180173
des = input("What is the FULL PATH to the dorectory you want to copy to? ")
181-
print("We will copy from {} to {}".format(sor, des))
182-
continue
174+
if sor.endswith("/"):
175+
print("We will copy from {} to {}".format(sor, des))
176+
continue
177+
else:
178+
print("The directory must have a / please add one")
183179
elif change == 'port':
184180
servport = input("What is the server port? If you don't choose one we will default to normal ssh ")
185181
if not servport:
186182
servport = 22
187183
print("The port you have chosen is {}".format(servport))
188184
continue
189185
elif change == 'username':
190-
usn = input('''Please state the user for the backup,
191-
if you don't state one we'll use the default one {} '''.format(getpass.getuser()))
186+
usn = input("What's the username? If none given we'll use the current {}".format(getpass.getuser()))
192187
print("The username you have chosen is {}".format(usn))
193188
continue
194189
elif change == 'server':
@@ -203,41 +198,20 @@ def localrem():
203198
logging = input("Do you want a log of the backup? (y or n)").lower()
204199
if logging == "n":
205200
logloc = ""
201+
print("No logs required running backup!")
206202
startbk = 2
207-
print("Saving to config for automation later")
208-
config = configparser.ConfigParser()
209-
config['LoRem'] = {'bkoption': startbk,
210-
'source': sor,
211-
'destination': des,
212-
'log_location': logloc,
213-
'username': usn,
214-
'remote_server': remserv,
215-
'server_port': servport}
216-
with open('config.ini', 'w') as configfile:
217-
config.write(configfile)
218-
configfile.close()
219-
print("Config saved. No logs required running backup!")
220203
sync_call_lorem(startbk, sor, des, usn, remserv, servport, logloc)
221204
break
222205
elif logging == "y":
223206
logloc = input("Please type the destination of the log: ")
224207
if logloc.endswith("/"):
225208
conf_log = input("Are you happy with the destination {} ? y or n ".format(logloc)).lower()
226209
if conf_log == "y":
210+
timestamp = time.strftime("%Y%m%d-%H%M")
211+
logloc = logloc + timestamp
212+
logloc = logloc + "-logfile"
227213
startbk = 1
228-
print("Saving to config for automation later")
229-
config = configparser.ConfigParser()
230-
config['LoRem'] = {'bkoption': startbk,
231-
'source': sor,
232-
'destination': des,
233-
'log_location': logloc,
234-
'username': usn,
235-
'remote_server': remserv,
236-
'server_port': servport}
237-
with open('config.ini', 'w') as configfile:
238-
config.write(configfile)
239-
configfile.close()
240-
print("Config saved. Ok lets run the backup!")
214+
print("Ok lets run the backup!")
241215
sync_call_lorem(startbk, sor, des, usn, remserv, servport, logloc)
242216
break
243217
else:
@@ -253,8 +227,7 @@ def remlocal():
253227
print("Now we will choose the source and destination")
254228
sor = input("What is the FULL PATH to the directory you want to copy from? ")
255229
des = input("What is the FULL PATH to the directory you want to copy to? ")
256-
usn = input('''Please state the user for the backup,
257-
if you don't state one we'll use the default one {} '''.format(getpass.getuser()))
230+
usn = input("What's the username? If none given we'll use the current {}".format(getpass.getuser()))
258231
if not usn:
259232
usn = getpass.getuser()
260233
remserv = input("What is the server you want to connect to? ")
@@ -280,8 +253,7 @@ def remlocal():
280253
print("The port you have chosen is {}".format(servport))
281254
continue
282255
elif change == 'username':
283-
usn = input('''Please state the user for the backup,
284-
if you don't state one we'll use the default one {} '''.format(getpass.getuser()))
256+
usn = input("What's the username? If none given we'll use the current {}".format(getpass.getuser()))
285257
print("The username you have chosen is {}".format(usn))
286258
continue
287259
elif change == 'server':
@@ -296,100 +268,25 @@ def remlocal():
296268
logging = input("Do you want a log of the backup? (y or n)").lower()
297269
if logging == "n":
298270
logloc = ""
271+
print("No logs required running backup!")
299272
startbk = 3
300-
print("Saving to config for automation later")
301-
config = configparser.ConfigParser()
302-
config['RemLo'] = {'bkoption': startbk,
303-
'source': sor,
304-
'destination': des,
305-
'log_location': logloc,
306-
'username': usn,
307-
'remote_server': remserv,
308-
'server_port': servport}
309-
with open('config.ini', 'w') as configfile:
310-
config.write(configfile)
311-
configfile.close()
312-
print("Config saved. Ok lets run the backup!")
313273
sync_call_lorem(startbk, sor, des, usn, remserv, servport, logloc)
314274
break
315275
elif logging == "y":
316276
logloc = input("Please type the destination of the log: ")
317277
if logloc.endswith("/"):
318278
conf_log = input("Are you happy with the destination {} ? y or n ".format(logloc)).lower()
319279
if conf_log == "y":
280+
timestamp = time.strftime("%Y%m%d-%H%M")
281+
logloc = logloc + timestamp
282+
logloc = logloc + "-logfile"
320283
startbk = 4
321-
print("Saving to config for automation later")
322-
config = configparser.ConfigParser()
323-
config['RemLo'] = {'bkoption': startbk,
324-
'source': sor,
325-
'destination': des,
326-
'log_location': logloc,
327-
'username': usn,
328-
'remote_server': remserv,
329-
'server_port': servport}
330-
with open('config.ini', 'w') as configfile:
331-
config.write(configfile)
332-
configfile.close()
333-
print("Config saved. Ok lets run the backup!")
284+
print("Ok lets run the backup!")
334285
sync_call_lorem(startbk, sor, des, usn, remserv, servport, logloc)
335286
break
336287
else:
337288
print("Ok, lets change the destination")
338289
continue
339290
else:
340291
print("Destination must end with a / please try again")
341-
continue
342-
343-
344-
# Automated backup for local
345-
def lsyauto():
346-
print("Reading config")
347-
config = configparser.ConfigParser()
348-
config.read("config.ini")
349-
if config.get("Manual", "bkoption") == '1':
350-
startbk = 1
351-
else:
352-
startbk = 2
353-
sor = config.get("Manual", "source")
354-
des = config.get("Manual", "destination")
355-
logloc = config.get("Manual", "log_location")
356-
print("Running backup from {} to {}".format(sor, des))
357-
sync_call_manual(startbk, sor, des, logloc)
358-
359-
360-
# Automated backup for local to remote
361-
def loreauto():
362-
print("Reading config")
363-
config = configparser.ConfigParser()
364-
config.read("config.ini")
365-
if config.get("LoRem", "bkoption") == '1':
366-
startbk = 1
367-
else:
368-
startbk = 2
369-
sor = config.get("LoRem", "source")
370-
des = config.get("LoRem", "destination")
371-
logloc = config.get("LoRem", "log_location")
372-
usn = config.get("LoRem", "username")
373-
remserv = config.get("LoRem", "remote_server")
374-
servport = config.get("LoRem", "server_port")
375-
print("Running backup")
376-
sync_call_lorem(startbk, sor, des, usn, remserv, servport, logloc)
377-
378-
379-
# Automated backup for remote to local
380-
def reloauto():
381-
print("Reading config")
382-
config = configparser.ConfigParser()
383-
config.read("config.ini")
384-
if config.get("RemLo", "bkoption") == '3':
385-
startbk = 3
386-
else:
387-
startbk = 4
388-
sor = config.get("RemLo", "source")
389-
des = config.get("RemLo", "destination")
390-
logloc = config.get("RemLo", "log_location")
391-
usn = config.get("RemLo", "username")
392-
remserv = config.get("RemLo", "remote_server")
393-
servport = config.get("RemLo", "server_port")
394-
print("Running backup")
395-
sync_call_lorem(startbk, sor, des, usn, remserv, servport, logloc)
292+
continue

0 commit comments

Comments
 (0)