Skip to content

Commit 7b4c449

Browse files
committed
Updated.
1 parent 95e2b15 commit 7b4c449

4 files changed

Lines changed: 104 additions & 39 deletions

File tree

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
__author__ = "ipetrash"
5+
6+
7+
import sqlite3
8+
9+
from datetime import date
10+
from pathlib import Path
11+
12+
from config import DIR_DB, DIR_DB_BACKUP
13+
from common import create_zip_for_file, _process_test
14+
15+
16+
FILE_NAME = Path(__file__).resolve()
17+
18+
19+
def backup(
20+
connect: sqlite3.Connection,
21+
file_name: Path,
22+
zip: bool = True,
23+
delete_file_name_after_zip: bool = True,
24+
) -> Path:
25+
dst = sqlite3.connect(file_name)
26+
connect.backup(dst)
27+
dst.close()
28+
29+
if zip:
30+
file_name_zip = Path(f"{file_name}.zip")
31+
create_zip_for_file(file_name_zip, file_name, delete_file_name=delete_file_name_after_zip)
32+
return file_name_zip
33+
34+
return file_name
35+
36+
37+
with sqlite3.connect(":memory:") as connect:
38+
_process_test(connect)
39+
40+
file_name_backup = DIR_DB_BACKUP / f"{FILE_NAME.stem}_memory_{date.today()}.db"
41+
file_name_backup_zip = backup(connect, file_name_backup, delete_file_name_after_zip=False)
42+
print(f"Создан бэкап базы данных в: {file_name_backup_zip}")
43+
44+
45+
with sqlite3.connect(
46+
str(DIR_DB / FILE_NAME.stem) + ".db",
47+
isolation_level=None,
48+
) as connect:
49+
connect.execute('pragma journal_mode=wal')
50+
51+
_process_test(connect)
52+
53+
file_name_backup = DIR_DB_BACKUP / f"{FILE_NAME.stem}_wal_{date.today()}.db"
54+
file_name_backup_zip = backup(connect, file_name_backup)
55+
print(f"Создан бэкап базы данных в: {file_name_backup_zip}")

sqlite3__examples/backup.py renamed to sqlite3__examples/backup__examples/backup_via_vacuum_into.py

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,17 @@
55

66

77
import sqlite3
8-
import zipfile
98

109
from datetime import date
1110
from pathlib import Path
1211

13-
from root_config import DIR_DB
12+
from config import DIR_DB, DIR_DB_BACKUP
13+
from common import create_zip_for_file, _process_test
1414

1515

16-
DIR_DB_BACKUP = DIR_DB / "backup"
17-
DIR_DB_BACKUP.mkdir(parents=True, exist_ok=True)
18-
1916
FILE_NAME = Path(__file__).resolve()
2017

2118

22-
def create_zip_for_file(
23-
file_name_zip: str | Path,
24-
file_name: Path,
25-
delete_file_name: bool = True,
26-
):
27-
with zipfile.ZipFile(file_name_zip, mode="w", compression=zipfile.ZIP_DEFLATED) as f:
28-
f.write(file_name, arcname=file_name.name)
29-
30-
if delete_file_name:
31-
file_name.unlink()
32-
33-
3419
def backup(
3520
connect: sqlite3.Connection,
3621
file_name: Path,
@@ -50,33 +35,14 @@ def backup(
5035
return file_name
5136

5237

53-
def _process_test(connect: sqlite3.Connection):
54-
connect.executescript(
55-
"""
56-
create table if not exists stocks (
57-
date text,
58-
trans text,
59-
symbol text,
60-
qty real,
61-
price real
62-
);
63-
64-
insert into stocks values ('2006-01-05', 'BUY', 'RHAT', 100, 35.14);
65-
insert into stocks values ('2006-05-01', 'BUY', 'TFAR', 40, 112.10);
66-
"""
67-
)
68-
69-
print()
70-
print(connect.execute("select * from stocks").fetchall())
71-
print()
72-
73-
7438
with sqlite3.connect(":memory:") as connect:
39+
print("MEMORY")
7540
_process_test(connect)
7641

7742
file_name_backup = DIR_DB_BACKUP / f"{FILE_NAME.stem}_memory_{date.today()}.db"
7843
file_name_backup_zip = backup(connect, file_name_backup)
7944
print(f"Создан бэкап базы данных в: {file_name_backup_zip}")
45+
print()
8046

8147

8248
with sqlite3.connect(
@@ -85,8 +51,10 @@ def _process_test(connect: sqlite3.Connection):
8551
) as connect:
8652
connect.execute('pragma journal_mode=wal')
8753

54+
print("FILE")
8855
_process_test(connect)
8956

9057
file_name_backup = DIR_DB_BACKUP / f"{FILE_NAME.stem}_wal_{date.today()}.db"
9158
file_name_backup_zip = backup(connect, file_name_backup)
9259
print(f"Создан бэкап базы данных в: {file_name_backup_zip}")
60+
print()
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
__author__ = "ipetrash"
5+
6+
import sqlite3
7+
import zipfile
8+
from pathlib import Path
9+
10+
11+
def create_zip_for_file(
12+
file_name_zip: str | Path,
13+
file_name: Path,
14+
delete_file_name: bool = True,
15+
):
16+
with zipfile.ZipFile(file_name_zip, mode="w", compression=zipfile.ZIP_DEFLATED) as f:
17+
f.write(file_name, arcname=file_name.name)
18+
19+
if delete_file_name:
20+
file_name.unlink()
21+
22+
23+
def _process_test(connect: sqlite3.Connection):
24+
connect.executescript(
25+
"""
26+
create table if not exists stocks (
27+
date text,
28+
trans text,
29+
symbol text,
30+
qty real,
31+
price real
32+
);
33+
34+
insert into stocks values ('2006-01-05', 'BUY', 'RHAT', 100, 35.14);
35+
insert into stocks values ('2006-05-01', 'BUY', 'TFAR', 40, 112.10);
36+
"""
37+
)
38+
39+
print(connect.execute("select * from stocks").fetchall())

sqlite3__examples/root_config.py renamed to sqlite3__examples/backup__examples/config.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
from pathlib import Path
88

99

10-
DIR = Path(__file__).resolve().parent
10+
DIR = Path(__file__).resolve().parent.parent
1111

1212
DIR_DB = DIR / "databases"
1313
DIR_DB.mkdir(parents=True, exist_ok=True)
14+
15+
DIR_DB_BACKUP = DIR_DB / "backup"
16+
DIR_DB_BACKUP.mkdir(parents=True, exist_ok=True)

0 commit comments

Comments
 (0)