-
Notifications
You must be signed in to change notification settings - Fork 0
/
zfs-backup.py
executable file
·77 lines (69 loc) · 2.82 KB
/
zfs-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
#!/usr/bin/python3
import coloredlogs,logging
coloredlogs.install(level='DEBUG')
import sys
import configparser
import datetime
import zfs
# Read configuration files
cfg = configparser.ConfigParser()
cfg.read("test.cfg")
configLength = len(cfg.sections())
numDatasets = configLength - 1
logging.debug("configLengthlength is " + str(configLength))
logging.info("datasets to process: " + str(numDatasets))
# Process config file
datasetsToProcess = []
for x in cfg.sections():
if x == "ZFSBackupGlobal":
logging.debug("not processing global config as dataset")
else:
if zfs.checkExists(x):
logging.debug(x + " exists!")
datasetsToProcess.append(x)
else:
logging.error(x + " does not exist. Ignoring.")
logging.info("Datasets to Process")
for x in datasetsToProcess:
logging.info("Pool Name: " + x)
logging.info(" Backup Pool: " + cfg[x]["backuppool"])
logging.info(" Encrypted Backup: " + cfg[x]["encrypted"])
logging.info(" Backup Frequency: " +cfg[x]["frequency"])
# Process datasets
for x in datasetsToProcess:
logging.info("Processsing dataset " + x)
dsBackupPool = cfg[x]['backuppool']
dsBackupSet = dsBackupPool + "/" + x
logging.debug("dsBackupPool is " + dsBackupPool)
dsEncryptBackup = cfg[x]['encrypted']
dsBackupFreq = cfg[x]['frequency']
dsFirstRun = False
# Check if backup dataset exists
if zfs.checkExists(dsBackupSet):
logging.debug(dsBackupSet + " exists!")
else:
logging.warning(dsBackupSet + " does not exist!")
if not zfs.checkExists(dsBackupPool):
logging.fatal(dsBackupPool + " does not exist. Please create " + dsBackupPool + " zpool on your chosen backup device.")
sys.exit(1)
dsFirstRun = True
if dsFirstRun:
logging.warning("This appears to be the first run of zfs-backup for " + x +". Is this correct? [y/N] ")
newAnswer = input()
if newAnswer == "y" or newAnswer == "Y":
logging.debug("proceeding as first run")
elif newAnswer == "n" or newAnswer == "N" or newAnswer == "":
logging.fatal("Please check your storage devices and run zfs-backup again! Exiting.")
sys.exit(1)
else:
logging.fatal("Unknown input! Exiting.")
sys.exit(1)
snapshotName = "zback-" + datetime.datetime.now().strftime("%b%d%Y-%H%M%S")
#snapshotName = "borktest"
logging.debug("snapshot name will be: " + x + "@" + snapshotName)
logging.info("creating initial snapshot...")
zfs.snapshot(x, snapshotName)
logging.info("snapshot created!")
logging.info("sending snapshot to " + dsBackupSet)
zfs.sendLocal(x, snapshotName, dsBackupSet)
logging.info("initial snapshot of " + x + "@" + snapshotName +" created!")