Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
Owo Sugiana
/
maintenance
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 68afdec7
authored
Jul 26, 2024
by
Owo Sugiana
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
File *.signal sebagai tanda db mirror
1 parent
6314a6b9
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
32 additions
and
32 deletions
CHANGES.txt
maintenance/__init__.py
CHANGES.txt
View file @
68afdec
0.9.1 26-7-2024
---------------
- Status database mirror kini berdasarkan keberadaan file *.signal.
0.9 7-7-2024
0.9 7-7-2024
------------
------------
- Status database mirror kini menggunakan query ketimbang baca log file.
- Status database mirror kini menggunakan query ketimbang baca log file.
...
...
maintenance/__init__.py
View file @
68afdec
...
@@ -11,6 +11,7 @@ from subprocess import (
...
@@ -11,6 +11,7 @@ from subprocess import (
)
)
from
configparser
import
ConfigParser
from
configparser
import
ConfigParser
from
argparse
import
ArgumentParser
from
argparse
import
ArgumentParser
from
glob
import
glob
from
.pg_conf
import
Reader
from
.pg_conf
import
Reader
from
.file_rotate
import
FileRotate
from
.file_rotate
import
FileRotate
from
.logger
import
setup_logging
from
.logger
import
setup_logging
...
@@ -60,14 +61,32 @@ def is_systemd():
...
@@ -60,14 +61,32 @@ def is_systemd():
return
os
.
path
.
exists
(
script
)
return
os
.
path
.
exists
(
script
)
def
is_pg_active
(
version
,
cluster
):
service
=
f
'postgresql@{version}-{cluster}.service'
cmd1
=
[
'systemctl'
,
'status'
,
service
]
cmd2
=
[
'grep'
,
'Active'
]
p1
=
Popen
(
cmd1
,
stdout
=
PIPE
)
p2
=
Popen
(
cmd2
,
stdin
=
p1
.
stdout
,
stdout
=
PIPE
)
out
,
err
=
p2
.
communicate
()
if
not
err
:
s
=
out
.
decode
(
'utf-8'
)
if
s
:
t
=
s
.
split
()
return
t
[
1
]
==
'active'
def
systemd_pg_service
(
todo
):
def
systemd_pg_service
(
todo
):
pg_conf_dir
=
conf
.
get
(
'main'
,
'pg_conf_dir'
)
pg_conf_dir
=
conf
.
get
(
'main'
,
'pg_conf_dir'
)
for
version
in
os
.
listdir
(
pg_conf_dir
):
for
version
in
os
.
listdir
(
pg_conf_dir
):
conf_dir
=
os
.
path
.
join
(
pg_conf_dir
,
version
)
conf_dir
=
os
.
path
.
join
(
pg_conf_dir
,
version
)
for
cluster
in
os
.
listdir
(
conf_dir
):
for
cluster
in
os
.
listdir
(
conf_dir
):
cluster_dir
=
os
.
path
.
join
(
conf_dir
,
cluster
)
if
is_mirror
(
version
,
cluster
):
pg_conf
=
os
.
path
.
join
(
cluster_dir
,
'postgresql.conf'
)
continue
if
is_mirror
(
pg_conf
):
is_active
=
is_pg_active
(
version
,
cluster
)
if
todo
==
'start'
:
if
is_active
:
continue
elif
not
is_active
:
continue
continue
cmd
=
f
'systemctl {todo} postgresql@{version}-{cluster}.service'
cmd
=
f
'systemctl {todo} postgresql@{version}-{cluster}.service'
run
(
cmd
)
run
(
cmd
)
...
@@ -110,32 +129,8 @@ def pg_port(conf_file):
...
@@ -110,32 +129,8 @@ def pg_port(conf_file):
return
value
return
value
def
is_mirror
(
pg_conf
):
def
is_mirror
(
version
,
cluster
):
r
=
Reader
(
pg_conf
)
return
glob
(
f
'/var/lib/postgresql/{version}/{cluster}/*.signal'
)
if
'include_dir'
in
r
:
# Versi 12 - 15
conf_dir
=
os
.
path
.
split
(
pg_conf
)[
0
]
mirror_conf
=
os
.
path
.
join
(
conf_dir
,
r
[
'include_dir'
],
'recovery.conf'
)
if
os
.
path
.
exists
(
mirror_conf
):
return
True
# Versi 9.1 - 11
mirror_conf
=
os
.
path
.
join
(
r
[
'data_directory'
],
'recovery.conf'
)
if
os
.
path
.
exists
(
mirror_conf
):
return
True
if
'primary_conninfo'
in
r
:
return
True
port
=
r
[
'port'
]
# Versi 11 - 15
cmd_psql
=
f
'psql -p {port} -c "SHOW primary_conninfo"'
cmd1
=
[
'su'
,
'-'
,
'postgres'
,
'-c'
,
cmd_psql
]
cmd2
=
[
'grep'
,
'user'
]
p1
=
Popen
(
cmd1
,
stdout
=
PIPE
)
p2
=
Popen
(
cmd2
,
stdin
=
p1
.
stdout
,
stdout
=
PIPE
)
out
,
err
=
p2
.
communicate
()
if
not
err
:
s
=
out
.
decode
(
'utf-8'
)
if
s
:
return
True
def
is_db_exists
(
version
,
port
,
db_name
):
def
is_db_exists
(
version
,
port
,
db_name
):
...
@@ -162,7 +157,7 @@ def pg_local_conf():
...
@@ -162,7 +157,7 @@ def pg_local_conf():
for
cluster
in
os
.
listdir
(
conf_dir
):
for
cluster
in
os
.
listdir
(
conf_dir
):
cluster_dir
=
os
.
path
.
join
(
conf_dir
,
cluster
)
cluster_dir
=
os
.
path
.
join
(
conf_dir
,
cluster
)
pg_conf
=
os
.
path
.
join
(
cluster_dir
,
'postgresql.conf'
)
pg_conf
=
os
.
path
.
join
(
cluster_dir
,
'postgresql.conf'
)
if
is_mirror
(
pg_conf
):
if
is_mirror
(
version
,
cluster
):
continue
continue
pg_hba_file
=
os
.
path
.
join
(
cluster_dir
,
'pg_hba.conf'
)
pg_hba_file
=
os
.
path
.
join
(
cluster_dir
,
'pg_hba.conf'
)
pg_hba_bak_file
=
pg_hba_file
+
'.orig'
pg_hba_bak_file
=
pg_hba_file
+
'.orig'
...
@@ -243,6 +238,8 @@ def pg_backup():
...
@@ -243,6 +238,8 @@ def pg_backup():
version_backup_dir
=
os
.
path
.
join
(
base_backup_dir
,
version
)
version_backup_dir
=
os
.
path
.
join
(
base_backup_dir
,
version
)
mkdir
(
version_backup_dir
)
mkdir
(
version_backup_dir
)
for
cluster
in
os
.
listdir
(
version_conf_dir
):
for
cluster
in
os
.
listdir
(
version_conf_dir
):
if
is_mirror
(
version
,
cluster
):
continue
cluster_conf_dir
=
os
.
path
.
join
(
version_conf_dir
,
cluster
)
cluster_conf_dir
=
os
.
path
.
join
(
version_conf_dir
,
cluster
)
cluster_backup_dir
=
os
.
path
.
join
(
version_backup_dir
,
cluster
)
cluster_backup_dir
=
os
.
path
.
join
(
version_backup_dir
,
cluster
)
mkdir
(
cluster_backup_dir
)
mkdir
(
cluster_backup_dir
)
...
@@ -251,8 +248,7 @@ def pg_backup():
...
@@ -251,8 +248,7 @@ def pg_backup():
for
db_name
in
db_list
:
for
db_name
in
db_list
:
if
not
is_db_exists
(
version
,
port
,
db_name
):
if
not
is_db_exists
(
version
,
port
,
db_name
):
continue
continue
if
not
is_mirror
(
pg_conf
):
vacuum
(
version
,
port
,
db_name
)
vacuum
(
version
,
port
,
db_name
)
pg_dump
(
version
,
port
,
db_name
,
cluster_backup_dir
)
pg_dump
(
version
,
port
,
db_name
,
cluster_backup_dir
)
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment