Delete older backups of config files

This commit is contained in:
Andy Teijelo Pérez 2015-11-08 12:27:37 -05:00
parent 319c578ba9
commit cf61ba3500
3 changed files with 20 additions and 8 deletions

View file

@ -96,10 +96,13 @@ ff:ff:ff:ff:ff:fe,10.6.122.94
ff:ff:ff:ff:ff:fe,10.6.122.95 ff:ff:ff:ff:ff:fe,10.6.122.95
# {"reserved-by": "Andy"} # {"reserved-by": "Andy"}
00:23:54:8c:65:13,10.6.122.96,set:client0023548c6513 00:23:54:8c:65:13,10.6.122.96,set:client0023548c6513
# {"color": "red", "reserved-by": "Andy"}
ff:ff:ff:ff:ff:fe,10.6.122.97 ff:ff:ff:ff:ff:fe,10.6.122.97
# {"color": "gold", "reserved-by": "Andy"}
ff:ff:ff:ff:ff:fe,10.6.122.98 ff:ff:ff:ff:ff:fe,10.6.122.98
# {"color": "red", "reserved-by": "Zelda"} # {"color": "green", "reserved-by": "Zelda"}
ff:ff:ff:ff:ff:fe,10.6.122.99 ff:ff:ff:ff:ff:fe,10.6.122.99
# {"color": "blue", "reserved-by": "Andy"}
ff:ff:ff:ff:ff:fe,10.6.122.100 ff:ff:ff:ff:ff:fe,10.6.122.100
# {"reserved-by": "Andy"} # {"reserved-by": "Andy"}
12:24:42:23:24:45,10.6.122.101,set:client122442232445 12:24:42:23:24:45,10.6.122.101,set:client122442232445

View file

@ -1,2 +1,2 @@
tag:client122442232445,option:router,10.6.122.2
tag:client0023548c6513,option:router,10.6.122.96 tag:client0023548c6513,option:router,10.6.122.96
tag:client122442232445,option:router,10.6.122.2

21
main.py
View file

@ -11,6 +11,7 @@ import hashlib
import random import random
import shutil import shutil
import time import time
import glob
import sys import sys
import re import re
import os import os
@ -63,7 +64,7 @@ def delete_user(deluser):
continue continue
f.write(line) f.write(line)
f.close() f.close()
shutil.move(tempfile, users_file) rename(tempfile, users_file)
reload_freeradius() reload_freeradius()
def nthash(password): def nthash(password):
@ -107,7 +108,6 @@ def login():
session['logged_in'] = True session['logged_in'] = True
session['username'] = username session['username'] = username
print("Redirecting to", request.args.get('redirect_to'))
return redirect(request.args.get('redirect_to','admin')) return redirect(request.args.get('redirect_to','admin'))
@app.route("/") @app.route("/")
@ -314,6 +314,17 @@ def parse_macs(macs):
r.append(m.group(0)) r.append(m.group(0))
return r return r
def rename(src, dst):
shutil.move(src, dst)
shutil.copy(dst, src)
copies = glob.glob(dst + ".[0-9]*")
# sort from most recent to oldest
copies = sorted(copies, key=lambda x: -float(x[len(dst)+1:]))
# keep only the most recent 5
for c in copies[5:]:
print("Deleting", c)
os.remove(c)
def write_dhcp_files(ipmap): def write_dhcp_files(ipmap):
timestamp = time.time() timestamp = time.time()
tempfile = "{}.{}".format(dhcp_hosts_file, timestamp) tempfile = "{}.{}".format(dhcp_hosts_file, timestamp)
@ -343,8 +354,7 @@ def write_dhcp_files(ipmap):
settag = ",set:" + tag settag = ",set:" + tag
gws[tag] = ip.gw gws[tag] = ip.gw
print("{},{}.{}{}".format(macs,prefix,i,settag), file=f) print("{},{}.{}{}".format(macs,prefix,i,settag), file=f)
shutil.move(tempfile, dhcp_hosts_file) rename(tempfile, dhcp_hosts_file)
shutil.copy(dhcp_hosts_file, tempfile)
# Write custom gateways file # Write custom gateways file
tempfile = "{}.{}".format(dhcp_opts_file, timestamp) tempfile = "{}.{}".format(dhcp_opts_file, timestamp)
@ -354,8 +364,7 @@ def write_dhcp_files(ipmap):
"tag:{},option:router,{}".format(tag,gws[tag]), "tag:{},option:router,{}".format(tag,gws[tag]),
file=f, file=f,
) )
shutil.move(tempfile, dhcp_opts_file) rename(tempfile, dhcp_opts_file)
shutil.copy(dhcp_opts_file, tempfile)
@app.route("/ip/<int:last_byte>", methods=['GET', 'POST']) @app.route("/ip/<int:last_byte>", methods=['GET', 'POST'])
@login_required @login_required