forked from vabene1111/DockerPostgresBackups
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackups.py
255 lines (171 loc) · 6.72 KB
/
backups.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#!/usr/bin/env python
"""Docker Compose Postgres dump and backup script
This script can be used to easily create dumps of postgres databases in docker containers,
upload them to rclone targets and restore them if needed.
It can also simply create archives of any folder (for example a postgres volume mapping) and restore/sync them.
Requires the docker container to be setup with docker compose.
"""
import os
import glob
import argparse
import datetime
import shutil
import configparser
import tarfile
import time
storage_dir = ''
filename_prefix = ''
timestamp = True
timestamp_format = ''
docker_compose_path = ''
pg_data_dir = ''
pg_docker_container = ''
pg_docker_user = ''
rclone_target = ''
rclone_path = ''
delete_days = 7
DUMP_EXTENSION = '.sql'
DEBUG = False
def debug(msg):
if DEBUG:
print(msg)
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('config', help="config from config.ini that should be used", nargs='?', default='default')
parser.add_argument("-d", '--dump', help="dump postgres database", action="store_true")
parser.add_argument("-l", '--load', help="load latest dump", action="store_true")
parser.add_argument("-L", '--load-specific', help="load specified dump")
parser.add_argument("-s", '--sync', help="sync backups with rclone target", action="store_true")
parser.add_argument("-v", '--verbose', help="enables debugging output", action="store_true")
return parser.parse_args()
def make_storage_dir():
if not os.path.exists(storage_dir):
print('Created storage directory!')
os.makedirs(storage_dir)
def file_timestamp(file):
if timestamp:
now = datetime.datetime.now()
ts = now.strftime(timestamp_format)
file = file + ts
return file
def get_dump_path():
return file_timestamp(storage_dir + filename_prefix) + DUMP_EXTENSION
def create_pg_dump():
print("Creating DB dump ... ")
os.chdir(docker_compose_path)
cmd_container_up = '/usr/local/bin/docker-compose up -d ' + pg_docker_container
debug('starting container: ' + cmd_container_up)
out = os.popen(cmd_container_up).read()
cmd_container_dump = '/usr/local/bin/docker-compose exec -T ' + pg_docker_container + ' pg_dumpall -U ' + pg_docker_user + ' -f /var/lib/postgresql/data/dump' + DUMP_EXTENSION
debug('creating dump: ' + cmd_container_dump)
os.system(cmd_container_dump)
mv = 'mv ' + pg_data_dir + 'dump' + DUMP_EXTENSION + ' ' + get_dump_path()
os.system(mv)
debug('moving backup with: ' + mv)
if 'is up-to-date' in out:
os.system('/usr/local/bin/docker-compose stop ' + pg_docker_container)
print("DB dump created in " + get_dump_path())
def load_pg_dump(file):
print("Loading DB dump ... ")
os.chdir(docker_compose_path)
os.system('/usr/local/bin/docker-compose stop ' + pg_docker_container)
if os.path.exists(pg_data_dir):
shutil.rmtree(pg_data_dir, ignore_errors=False, onerror=None)
os.system('/usr/local/bin/docker-compose up -d ' + pg_docker_container)
time.sleep(2)
os.system('/usr/local/bin/docker-compose exec -T ' + pg_docker_container + ' psql -U ' + pg_docker_user + ' postgres < ' + file)
print("DB dump loaded!")
def choose_file(extension):
dir_list = os.listdir(storage_dir)
print(storage_dir)
if len(dir_list) == 0:
print('There where no ' + extension + ' files found that could be restored!')
return
print("Please choose the file you want to restore:")
file_chooser = ""
i = 0
for file in dir_list:
if file.endswith(extension):
file_chooser += '[' + str(i) + '] ' + file + ' '
i += 1
if i % 3 == 0:
file_chooser += '\n'
print(file_chooser)
file_index = int(input())
filename = storage_dir + dir_list[file_index]
load_pg_dump(filename)
def get_latest():
print('Searching latest ' + DUMP_EXTENSION)
list_of_files = glob.glob(storage_dir + '*' + DUMP_EXTENSION)
if len(list_of_files) == 0:
print('There where no ' + DUMP_EXTENSION + ' files found that could be restored!')
return
filename = max(list_of_files, key=os.path.getctime)
print("Found " + filename)
load_pg_dump(filename)
def sync_storage():
if rclone_target == '':
return
print('Starting backup sync ...')
cmd = 'rclone sync ' + storage_dir + ' ' + rclone_target + ':' + rclone_path
if DEBUG:
print('rclone command: ' + cmd)
os.system(cmd)
print('Storage directory synced!')
def delete_old():
if delete_days == -1:
return
print("Deleting old backups ...")
cutoff = time.time() - (delete_days * 86400)
files = os.listdir(storage_dir)
for f in files:
if os.path.isfile(storage_dir + f):
t = os.stat(storage_dir + f)
c = t.st_ctime
if c < cutoff:
os.remove(storage_dir + f)
print('Finished deleting old backups.')
def load_config(config_name):
# config parser infos https://docs.python.org/3/library/configparser.html
# config.sections() all sections for all parameter
global storage_dir, filename_prefix, timestamp, timestamp_format, pg_data_dir, pg_docker_container, pg_docker_user, rclone_target, rclone_path, delete_days, docker_compose_path
config = configparser.ConfigParser()
config.read("config.ini")
storage_dir = config.get(config_name, "storage_dir")
filename_prefix = config.get(config_name, "filename_prefix")
timestamp = config.get(config_name, "timestamp")
timestamp_format = config.get(config_name, "timestamp_format")
docker_compose_path = config.get(config_name, "docker_compose_path")
pg_data_dir = config.get(config_name, "pg_data_dir")
pg_docker_container = config.get(config_name, "pg_docker_container")
pg_docker_user = config.get(config_name, "pg_docker_user")
rclone_target = config.get(config_name, "rclone_target")
rclone_path = config.get(config_name, "rclone_path")
delete_days = int(config.get(config_name, "delete_days"))
if not storage_dir.endswith("/"):
storage_dir += "/"
if not docker_compose_path.endswith("/"):
docker_compose_path += "/"
if not pg_data_dir.endswith("/"):
pg_data_dir += "/"
def main():
global DEBUG
args = parse_args()
if args.verbose:
DEBUG = True
if DEBUG:
print('Loading Config:' + args.config)
load_config(args.config)
make_storage_dir()
if args.dump:
create_pg_dump()
if args.load_specific:
choose_file(DUMP_EXTENSION)
if args.load:
get_latest()
if args.dump:
delete_old()
if args.dump or args.sync:
sync_storage()
if __name__ == "__main__":
main()