|
18 | 18 |
|
19 | 19 | d = datetime.datetime.now() |
20 | 20 | date_format = "%Y_%m_%d_%H_%M" |
21 | | -log_file = "/tmp/backup.log" |
| 21 | +log_file = "/tmp/freenas-backup.log" |
22 | 22 | db_file = "/data/freenas-v1.db" |
23 | 23 | bk_path = "/tmp/" |
24 | 24 | backup_name = "backup" |
25 | | -latest_name = "current" |
| 25 | +archive_name = "archive" |
26 | 26 | ext = ".db" |
27 | 27 |
|
28 | 28 | matched = None |
29 | 29 | fname = None |
30 | | -latest_len = len(latest_name) |
| 30 | +backup_len = len(backup_name) |
31 | 31 |
|
32 | 32 | console = logging.StreamHandler() |
33 | 33 | console.setLevel(logging.INFO) |
|
39 | 39 | datefmt='%m/%d/%Y %H:%M:%S') |
40 | 40 | logging.getLogger('').addHandler(console) |
41 | 41 |
|
42 | | -logging.info(latest_len) |
43 | | - |
44 | 42 | if os.stat(log_file)[6]==0: |
45 | | - logging.debug('Version 1.0 build 15') |
| 43 | + logging.debug('Version 1.0 build 16') |
46 | 44 | logging.debug('Automatic Backup Script for FreeNAS Configuration File') |
47 | 45 | logging.debug('Script Created By: Eric Bright Copyright (C) 2013') |
48 | 46 | logging.debug('https://github.com/ebright/FreeNas_Config/') |
|
51 | 49 | os.chdir(bk_path) |
52 | 50 | logging.debug('Searching for previous backup in ' + bk_path ) |
53 | 51 | for files in os.listdir("."): |
54 | | - if files.startswith(latest_name): |
| 52 | + if files.startswith(backup_name): |
55 | 53 | fname = files |
56 | 54 | bk_file = bk_path + files |
57 | 55 | matched = True |
|
64 | 62 | logging.info('Configuration has not changed. Aborting backup') |
65 | 63 | sys.exit() |
66 | 64 | else: |
67 | | - logging.debug('Configuration changed. renaming previous backup') |
68 | | - logging.debug(bk_file + ' >> ' + bk_path + backup_name + fname[latest_len:]) |
69 | | - shutil.move(bk_file, bk_path + backup_name + fname[latest_len:]) |
70 | | - logging.info('Creating backup ' + bk_path + latest_name + '_' + d.strftime(date_format) + ext) |
71 | | - shutil.copy2(db_file, bk_path + latest_name + '_' + d.strftime(date_format) + ext) |
| 65 | + logging.debug('Configuration changed. Archiving previous backup') |
| 66 | + logging.debug(bk_file + ' >> ' + bk_path + archive_name + fname[backup_len:]) |
| 67 | + shutil.move(bk_file, bk_path + archive_name + fname[backup_len:]) |
| 68 | + logging.info('Creating backup ' + bk_path + backup_name + '_' + d.strftime(date_format) + ext) |
| 69 | + shutil.copy2(db_file, bk_path + backup_name + '_' + d.strftime(date_format) + ext) |
72 | 70 |
|
73 | 71 | else: |
74 | | - bk_file = bk_path + latest_name + '_' + d.strftime(date_format) + ext |
| 72 | + bk_file = bk_path + backup_name + '_' + d.strftime(date_format) + ext |
75 | 73 | logging.info('No previous backup found. Create new backup: ' + bk_file) |
76 | 74 | shutil.copy2(db_file, bk_file) |
77 | 75 | sys.exit() |
0 commit comments