-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup.py
71 lines (63 loc) · 2.48 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
import os
import azure.storage.blob
import re
import yaml
from datetime import datetime, timedelta
#Get ENV secret variables
account_name = os.environ['ACCOUNT_NAME']
account_key = os.environ['ACCOUNT_KEY']
container_name = os.environ['CONTAINER_NAME']
#Import ConfigMap
with open('/config/blob-config.yml') as f:
config = yaml.load(f)
config_folder = config['k8s-backup-config']['Backup_Dir']
config_filter = config['k8s-backup-config']['Filter']
config_blob_folder = config['k8s-backup-config']['Blob_Folder']
retention_days = config['k8s-backup-config']['retention_days']
#Get File list by Filter
def getfiles(folder, filter):
files = list()
pattern = re.compile(filter)
for file in os.listdir(folder):
if pattern.match(file):
files.append(file)
return files
#Get blobs on container
def getblobs(connection, container):
blobs = connection.list_blobs(container)
return blobs
#Upload Files
def uploadFiles(folder, file, connection, container, prefix, blobs, existendblobs):
blobpath = prefix + '/' + file
source_file = os.path.join(folder, file)
# Test if there is no File in blobs
if len(blobs.items) == 0:
#Upload File
connection.create_blob_from_path(container, blobpath, source_file)
print("Will now upload blob " + blobpath)
else:
#Test if file is already there
if blobpath in existendblobs:
print(blobpath + " is already uploaded")
else:
#Upload File
connection.create_blob_from_path(container, blobpath, source_file)
print("Will now upload blob " + blobpath)
#Erase Files older than specified in config
def deleteFiles(connection, blobs, container, retention):
for blob in blobs:
time_diff = datetime.now() - blob.properties.last_modified
if time_diff>retention:
connection.delete_blob(container, blob.name)
print("Deleted Blob " + blob.name + " because of retention time")
#init Blob Connection
block_blob_service = azure.storage.blob.BlockBlobService(account_name=account_name, account_key=account_key)
# Filter Files
filteredfiles = getfiles(config_folder, config_filter)
blobs = getblobs(block_blob_service, container_name)
existendblobs = []
for blob in blobs:
existendblobs.append(blob.name)
for file in filteredfiles:
uploadFiles(config_folder, file, block_blob_service, container_name, config_blob_folder, blobs, existendblobs)
#deleteFiles(block_blob_service, blobs, container_name, retention_days)