33from src .helpers .kubernetes import Kubernetes
44
55
6- def main ():
7- # # If an argument is provided = we're using from CLI
8- # try:
9- parser = argparse .ArgumentParser (description = "Backup Kubernetes volumes" )
10- parser .add_index = False
11- parser .add_argument ("-c" , "--context" , type = str , default = None , help = "Local kubectl context" )
12- parser .add_argument ("-n" , "--namespace" , type = str , default = None , help = "Kubernetes namespace" )
13- args = parser .parse_args ()
14-
15- kubernetes = Kubernetes (context = args .context , given_namespace = args .namespace )
16- print (f"context: { args .context } " )
17-
18- # # If no argument is provided = we're in Kubernetes
19- # except:
20- # kubernetes = Kubernetes()
21- # print(f"context: local")
22-
23- current_ns = kubernetes .get_current_namespace ()
6+ # Get a YAML map of what will be backuped
7+ def get_backup_map (kubernetes : Kubernetes ):
8+ current_ns = kubernetes .get_namespace ()
249
10+ print (f"context: { kubernetes .get_context ()} " )
2511 print (f"namespaces:" )
2612 print (f"- name: { current_ns } " )
2713
@@ -30,6 +16,7 @@ def main():
3016 print ("error: no PVC found or unauthorized access." )
3117 return 0
3218
19+ # Loop to display the backup map
3320 print (f" claims:" )
3421 for pvc in kubernetes .list_pvc ():
3522 print (f" - name: { pvc ['name' ]} " )
@@ -44,13 +31,42 @@ def main():
4431 print (f" path: { m ['mount_path' ]} " )
4532 print (f" read_only: { m ['read_only' ]} " )
4633 print (f" resource: { m ['resource' ]} " )
47-
48- # volume_name_clean = print(f"{m['resource'].split("/")[1]}-{m['container_name']}-{m['mount_path'].replace('/','')}")
49- # kubernetes.create_backup_job(pvc=pvc['name'], mounted_path=m['mount_path'], volume_name=m['volume_name_clean'])
50-
51- # kubernetes.create_backup_job(pvc=pvc['name'], mounted_path=m['mount_path'])
5234 else :
5335 print (f" mounts: { mounts } " )
5436
37+
38+ # Run a backup job
39+ def run_backup_job (kubernetes : Kubernetes ):
40+ # Display the map before running the job
41+ get_backup_map (kubernetes )
42+
43+ for pvc in kubernetes .list_pvc ():
44+ mounts = kubernetes .get_pvc (pvc ['name' ])
45+ if mounts :
46+ for m in mounts :
47+ kubernetes .create_backup_job (pvc = pvc ['name' ], mounted_path = m ['mount_path' ])
48+
49+
50+ def main ():
51+ parser = argparse .ArgumentParser (description = "Backup Kubernetes volumes" )
52+ parser .add_index = False
53+ parser .add_argument ("-c" , "--context" , type = str , default = None , help = "Local kubectl context" )
54+ parser .add_argument ("-n" , "--namespace" , type = str , default = None , help = "Kubernetes namespace" )
55+ parser .add_argument ("-j" , "--job" , action = "store_true" , help = "Run a backup job" )
56+ parser .add_argument ("-m" , "--map" , action = "store_true" , help = "Get the backup map" )
57+ args = parser .parse_args ()
58+
59+ kubernetes = Kubernetes (context = args .context , namespace = args .namespace )
60+
61+ if args .job :
62+ run_backup_job (kubernetes )
63+
64+ elif args .map :
65+ get_backup_map (kubernetes )
66+
67+ else :
68+ print ('please run with --job or --map (--help for more info)' )
69+
70+
5571if __name__ == "__main__" :
5672 sys .exit (main ())
0 commit comments