forked from ebright/Freenas-bk-cfg
-
Notifications
You must be signed in to change notification settings - Fork 1
/
backup.py
144 lines (132 loc) · 6.6 KB
/
backup.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
#!/usr/local/bin/python
#
#FreeNAS Configuration Backup Script (version 1.0_15)
#Created By: Eric Bright Copyright (C) 2013
#
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License version 3 as
#published by the Free Software Foundation.
#
#You should have received a copy of the GNU General Public License
#along with this program. If not, see <http://www.gnu.org/licenses/>.
#
"""
-\-
\-- \-
\ - -\
\ \\
\ \
\ \\
\ \\
\ \\
\ \\\
\ \\
\ \\
\. . \\
\ . \\
\ . \\
\ . \\
\ . \\
\ <=)
\ <==)
\ <=)
\ .\\ _-
\ . \\ _-//
\ . \\ _-_/ /
\ . . . \\ _--_/ _/
\ \\ _- _/ _/
\ \\ ___-(O) _/ _/
\ \ __-- __ /_ / ***********************************
\ \\ ____--__---- / \_ I AM A MOTHERFUCKING PTERODACTYL
\ \\ ------- / \_ \_ HERE TO PTERO-YOU A NEW ASSHOLE
\ \ // // \__ \_ **********************************
\ \\ // // \_ \_
\ \\ /// // \__-
\ - \\///////// //
\ - \_ //
/ - //
/ - ///
/ - //
__--/ ///
__________/ // |
//-_________ ___ //// |
____\__--/ ///// |
-----______ -/---________//// |
_______/ --/ \ |
/_________-/ \ |
// \ /
\. /
\ . /
\ . /
\\ . /
\ /
\ __|
\ ==/
/ //
/ . //
/ . . //
/. /
/ //
/ /
/ //
/ //
--/ /
/ //
//// //
///_________////
"""
import os
import shutil
import datetime
import sys
import logging
d = datetime.datetime.now()
date_format = "%Y_%m_%d_%H_%M"
log_file = "/tmp/freenas-backup.log"
db_file = "/data/freenas-v1.db"
bk_path = "/tmp/"
backup_name = "backup"
archive_name = "archive"
ext = ".db"
matched = None
fname = None
backup_len = len(backup_name)
console = logging.StreamHandler()
console.setLevel(logging.INFO)
logging.basicConfig(filename=log_file,
level=logging.DEBUG,
format='%(asctime)s.%(msecs).03d %(message)s',
datefmt='%m/%d/%Y %H:%M:%S')
logging.getLogger('').addHandler(console)
if os.stat(log_file)[6]==0:
logging.debug('Version 1.0 build 16')
logging.debug('Automatic Backup Script for FreeNAS Configuration File')
logging.debug('Script Created By: Eric Bright Copyright (C) 2013')
logging.debug('https://github.com/ebright/FreeNas_Config/')
logging.debug('---------------------------')
os.chdir(bk_path)
logging.debug('Searching for previous backup in ' + bk_path )
for files in os.listdir("."):
if files.startswith(backup_name):
fname = files
bk_file = bk_path + files
matched = True
logging.debug('Found file ' + bk_file)
break
if matched is True:
read1 = file(db_file).read().split('\n')
read2 = file(bk_file).read().split('\n')
if read1 == read2:
logging.info('Configuration has not changed. Aborting backup')
sys.exit()
else:
logging.debug('Configuration changed. Archiving previous backup')
logging.debug(bk_file + ' >> ' + bk_path + archive_name + fname[backup_len:])
shutil.move(bk_file, bk_path + archive_name + fname[backup_len:])
logging.info('Creating backup ' + bk_path + backup_name + '_' + d.strftime(date_format) + ext)
shutil.copy2(db_file, bk_path + backup_name + '_' + d.strftime(date_format) + ext)
else:
bk_file = bk_path + backup_name + '_' + d.strftime(date_format) + ext
logging.info('No previous backup found. Create new backup: ' + bk_file)
shutil.copy2(db_file, bk_file)
sys.exit()