Skip to content

Commit afee380

Browse files
Add partial backup option
1 parent 4a32851 commit afee380

File tree

2 files changed

+53
-5
lines changed

2 files changed

+53
-5
lines changed

installation_and_upgrade/part_truncate_archive.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ IF %errorlevel% neq 0 (
1515
REM Matches current MySQL version
1616
uv pip install mysql-connector-python==8.4.0
1717

18-
python -u part_truncate_archive.py %*
18+
python -u %~dp0part_truncate_archive.py %*
1919
IF %errorlevel% neq 0 goto ERROR
2020
call rmdir /s /q %UV_TEMP_VENV%
2121

installation_and_upgrade/part_truncate_archive.py

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import argparse
2+
import subprocess
23
import time
34
from contextlib import closing
45

@@ -51,21 +52,25 @@ def main() -> None:
5152
parser.add_argument(
5253
"--password", dest="password", action="store", help="mysql root password", default=""
5354
)
55+
parser.add_argument(
56+
"--backup", dest="backup", action="store", help="backup data before deleting to file"
57+
)
5458
parser.add_argument("--dry-run", dest="dry_run", action="store_true", help="dry run")
5559

5660
args = parser.parse_args()
61+
sample_id = None
62+
count_sample_id = None
5763

5864
# ignore pyright checking as oracle bug in type signature of close() method
5965
with closing(
6066
mysql.connector.connect(
61-
user="root", password=args.password, host=args.host, database="archive"
67+
user="report", password="$report", host=args.host, database="archive"
6268
) # pyright: ignore
6369
) as conn:
6470
# this is so we don't cache query results and keep getting the same answer
6571
conn.autocommit = True
6672

6773
with closing(conn.cursor(prepared=True)) as c:
68-
c.execute("SET SQL_LOG_BIN=0") # disable any binary logging for this session
6974
print(f"Looking for sample_id corresponding to {args.history} days ago")
7075
c.execute(
7176
"SELECT MAX(sample_id) FROM sample WHERE smpl_time < TIMESTAMPADD(DAY, -?, NOW())",
@@ -82,19 +87,62 @@ def main() -> None:
8287
f"ID of last row to delete is {sample_id} and there are {count_sample_id} rows "
8388
f"-> {int(1 + count_sample_id / args.limit)} delete operations"
8489
)
90+
91+
if args.backup:
92+
command = [
93+
r"C:\Instrument\Apps\MySQL\bin\mysqldump.exe",
94+
"--user=root",
95+
f"--password={args.password}",
96+
f"--host={args.host}",
97+
"--single-transaction",
98+
f"--result-file={args.backup}",
99+
"--no-create-db",
100+
"--no-create-info",
101+
"--skip-triggers",
102+
"--quick",
103+
f'--where="WHERE sample_id < {sample_id}"',
104+
"archive",
105+
"sample",
106+
]
107+
if args.dry_run:
108+
print(command)
109+
else:
110+
subprocess.run(command, check=True)
111+
112+
# ignore pyright checking as oracle bug in type signature of close() method
113+
with closing(
114+
mysql.connector.connect(
115+
user="root", password=args.password, host=args.host, database="archive"
116+
) # pyright: ignore
117+
) as conn:
118+
# this is so we don't cache query results and keep getting the same answer
119+
conn.autocommit = True
120+
121+
with closing(conn.cursor(prepared=True)) as c:
122+
c.execute("SET SQL_LOG_BIN=0") # disable any binary logging for this session
123+
delete_ops = int(1 + count_sample_id / args.limit)
124+
125+
print(
126+
f"ID of last row to delete is {sample_id} and there are {count_sample_id} rows "
127+
f"-> {delete_ops} delete operations"
128+
)
85129
print(
86-
f"This will take at least {args.sleep * count_sample_id / args.limit:.1f} "
130+
f"This will take at least {args.sleep * delete_ops:.1f} "
87131
"seconds based on sleep time alone"
88132
)
89133
if args.dry_run:
90134
print("Exiting as dry-run")
91135
return
92136
rowcount = 1
93137
it = 0
138+
progress = 0
94139
while rowcount > 0:
95140
c.execute(f"DELETE FROM sample WHERE sample_id < {sample_id} LIMIT {args.limit}")
96141
rowcount = c.rowcount
97-
print(f"{it % 10}", end="", flush=True)
142+
so_far = 100.0 * it / delete_ops
143+
if so_far > progress:
144+
print(f"{progress}% ", end="", flush=True)
145+
progress += 5
98146
it += 1
99147
time.sleep(args.sleep)
100148
print("")

0 commit comments

Comments
 (0)