|
1 | | -import re |
| 1 | +import logging |
2 | 2 | import subprocess |
3 | 3 | import os |
4 | 4 | import datetime |
|
11 | 11 | from src import settings |
12 | 12 | from src import docker |
13 | 13 |
|
| 14 | +# Read config and setup logging |
14 | 15 | config, global_labels = settings.read() |
| 16 | +logging.basicConfig( |
| 17 | + level=config.loglevel, |
| 18 | + format='%(asctime)s %(levelname)s %(message)s' |
| 19 | +) |
| 20 | +logging.info("Starting backup service") |
| 21 | + |
| 22 | +# Connecting to docker API |
15 | 23 | docker_client = docker.get_client() |
16 | 24 | own_container = docker.get_own_container(docker_client) |
17 | | - |
18 | | -if config.verbose: |
19 | | - print(f"VERBOSE: Own Container ID: {own_container.id}") |
| 25 | +logging.debug(f"Own Container ID: {own_container.id}") |
20 | 26 |
|
21 | 27 | while True: |
22 | 28 | containers = docker_client.containers.list( |
|
27 | 33 |
|
28 | 34 | if len(containers): |
29 | 35 | successful_containers = 0 |
30 | | - print(f"Starting backup cycle with {len(containers)} container(s)..") |
| 36 | + logging.info(f"Starting backup cycle with {len(containers)} container(s)..") |
31 | 37 |
|
32 | 38 | network = docker_client.networks.create("docker-database-backup") |
33 | 39 | network.connect(own_container.id) |
34 | 40 |
|
35 | 41 | for i, container in enumerate(containers): |
36 | 42 | database = Database(container, global_labels) |
37 | 43 |
|
38 | | - print("[{}/{}] Processing container {} {} ({})".format( |
| 44 | + logging.info("[{}/{}] Processing container {} {} ({})".format( |
39 | 45 | i + 1, |
40 | 46 | len(containers), |
41 | 47 | container.short_id, |
42 | 48 | container.name, |
43 | 49 | database.type.name |
44 | 50 | )) |
45 | 51 |
|
46 | | - if config.verbose: |
47 | | - print("VERBOSE: Login {}@host:{} using Password: {}".format(database.username, database.port, "YES" if len(database.password) > 0 else "NO")) |
48 | | - if database.compress: |
49 | | - print("VERBOSE: Compressing backup") |
| 52 | + logging.debug("Login {}@host:{} using Password: {}".format(database.username, database.port, "YES" if len(database.password) > 0 else "NO")) |
| 53 | + if database.compress: |
| 54 | + logging.debug("Compressing backup") |
50 | 55 |
|
51 | 56 | if database.type == DatabaseType.unknown: |
52 | | - print("Cannot read database type. Please specify via label.") |
| 57 | + logging.info("Cannot read database type. Please specify via label.") |
53 | 58 |
|
54 | 59 | network.connect(container, aliases = ["database-backup-target"]) |
55 | 60 | outFile = "/dump/{}.sql".format(container.name) |
|
94 | 99 | network.disconnect(container) |
95 | 100 |
|
96 | 101 | if error_code > 0: |
97 | | - print(f"FAILED. Return Code: {error_code}; Error Output:") |
98 | | - print(f"{error_text}") |
| 102 | + logging.error(f"FAILED. Return Code: {error_code}; Error Output:") |
| 103 | + logging.error(f"{error_text}") |
99 | 104 | else: |
100 | 105 | if (os.path.exists(outFile)): |
101 | 106 | uncompressed_size = os.path.getsize(outFile) |
|
111 | 116 | os.chown(outFile, config.dump_uid, config.dump_gid) # pylint: disable=maybe-no-member |
112 | 117 |
|
113 | 118 | successful_containers += 1 |
114 | | - print("SUCCESS. Size: {}{}".format(humanize.naturalsize(uncompressed_size), " (" + humanize.naturalsize(compressed_size) + " compressed)" if database.compress else "")) |
| 119 | + logging.info("SUCCESS. Size: {}{}".format(humanize.naturalsize(uncompressed_size), " (" + humanize.naturalsize(compressed_size) + " compressed)" if database.compress else "")) |
115 | 120 |
|
116 | 121 | network.disconnect(own_container.id) |
117 | 122 | network.remove() |
118 | | - print(f"Finished backup cycle. {successful_containers}/{len(containers)} successful.") |
| 123 | + logging.info(f"Finished backup cycle. {successful_containers}/{len(containers)} successful.") |
119 | 124 | else: |
120 | | - print("No databases to backup") |
| 125 | + logging.info("No databases to backup") |
121 | 126 |
|
122 | 127 | if config.interval > 0: |
123 | 128 | nextRun = datetime.datetime.now() + datetime.timedelta(seconds=config.interval) |
124 | | - print("Scheduled next run at {}..".format(nextRun.strftime("%Y-%m-%d %H:%M:%S"))) |
| 129 | + logging.info("Scheduled next run at {}..".format(nextRun.strftime("%Y-%m-%d %H:%M:%S"))) |
125 | 130 |
|
126 | 131 | time.sleep(config.interval) |
127 | 132 | else: |
|
0 commit comments