Compare commits

..

13 Commits

Author SHA1 Message Date
charlesgauthereau 20e3facb58 chore(release): 1.0.0-rc.1 2026-01-09 19:05:01 +01:00
charlesgauthereau 09991d3c48 fix: refactoring mysql side and tested the method. Add logging with config from env 2026-01-09 19:04:37 +01:00
Charles GTE 8a2eb4638a Delete .DS_Store 2026-01-08 23:23:27 +01:00
charlesgauthereau f7af1d5db3 fix: gitignore 2026-01-08 23:23:03 +01:00
charlesgauthereau 8973e3a17e fix: Postgres system: add a lock system to avoid concurrency problems. 2026-01-08 23:19:58 +01:00
charlesgauthereau c69ab2a266 fix: Cargo.lock 2026-01-08 09:52:43 +01:00
charlesgauthereau 7fc4e59a4c chore(release): 0.1.2-rc.1 2026-01-08 09:52:24 +01:00
charlesgauthereau 766abe697a fix: postgres.rs methods and add tokio-postgres 2026-01-08 09:52:05 +01:00
charlesgauthereau a279993fa5 chore: Cargo.lock 2026-01-07 22:31:49 +01:00
charlesgauthereau bb3f254e6c chore(release): 0.1.1 2026-01-07 22:31:20 +01:00
charlesgauthereau 5d1841cdfe fix: psql error for ping method in postgres 2026-01-07 22:30:46 +01:00
charlesgauthereau 942b0e421e chore(release): 0.1.1-rc.8 2026-01-07 22:22:55 +01:00
charlesgauthereau 5daa635c8c fix: psql error for ping method in postgres 2026-01-07 22:22:45 +01:00
39 changed files with 1521 additions and 739 deletions
+3
View File
@@ -1,3 +1,6 @@
/target
.idea
/src/data/
.DS_Store
+2 -2
View File
@@ -22,5 +22,5 @@ keywords:
- self-hosted
- portabase
license: Apache-2.0
version: 0.1.1-rc.7
date-released: "2026-01-07"
version: 1.0.0-rc.1
date-released: "2026-01-09"
Generated
+456 -2
View File
@@ -8,6 +8,15 @@ version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa"
[[package]]
name = "aho-corasick"
version = "1.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ddd31a130427c27518df266943a5308ed92d4b226cc639f5a8f1002816174301"
dependencies = [
"memchr",
]
[[package]]
name = "android_system_properties"
version = "0.1.5"
@@ -86,12 +95,27 @@ version = "2.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "812e12b5285cc515a9c72a5c1d3b6d46a19dac5acfef5265968c166106e31dd3"
[[package]]
name = "block-buffer"
version = "0.10.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71"
dependencies = [
"generic-array",
]
[[package]]
name = "bumpalo"
version = "3.19.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5dd9dc738b7a8311c7ade152424974d8115f2cdad61e8dab8dac9f2362298510"
[[package]]
name = "byteorder"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
[[package]]
name = "bytes"
version = "1.11.0"
@@ -191,6 +215,15 @@ version = "0.8.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b"
[[package]]
name = "cpufeatures"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "59ed5838eebb26a2bb2e58f6d5b5316989ae9d08bab10e0e6d103e656d1b0280"
dependencies = [
"libc",
]
[[package]]
name = "crc32fast"
version = "1.5.0"
@@ -211,6 +244,51 @@ dependencies = [
"winnow 0.6.26",
]
[[package]]
name = "crossbeam-channel"
version = "0.5.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "82b8f8f868b36967f9606790d1903570de9ceaf870a7bf9fbbd3016d636a2cb2"
dependencies = [
"crossbeam-utils",
]
[[package]]
name = "crossbeam-utils"
version = "0.8.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28"
[[package]]
name = "crypto-common"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78c8292055d1c1df0cce5d180393dc8cce0abec0a7102adb6c7b1eef6016d60a"
dependencies = [
"generic-array",
"typenum",
]
[[package]]
name = "deranged"
version = "0.5.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ececcb659e7ba858fb4f10388c250a7252eb0a27373f1a72b8748afdd248e587"
dependencies = [
"powerfmt",
]
[[package]]
name = "digest"
version = "0.10.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292"
dependencies = [
"block-buffer",
"crypto-common",
"subtle",
]
[[package]]
name = "displaydoc"
version = "0.2.5"
@@ -259,6 +337,12 @@ dependencies = [
"windows-sys 0.61.2",
]
[[package]]
name = "fallible-iterator"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4443176a9f2c162692bd3d352d745ef9413eec5782a80d8fd6f8a1ac692a07f7"
[[package]]
name = "fastrand"
version = "2.3.0"
@@ -329,6 +413,21 @@ version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42703706b716c37f96a77aea830392ad231f44c9e9a67872fa5548707e11b11c"
[[package]]
name = "futures"
version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "65bc07b1a8bc7c85c5f2e110c476c7389b4554ba72af57d8445ea63a576b0876"
dependencies = [
"futures-channel",
"futures-core",
"futures-executor",
"futures-io",
"futures-sink",
"futures-task",
"futures-util",
]
[[package]]
name = "futures-channel"
version = "0.3.31"
@@ -345,12 +444,34 @@ version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e"
[[package]]
name = "futures-executor"
version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e28d1d997f585e54aebc3f97d39e72338912123a67330d723fdbb564d646c9f"
dependencies = [
"futures-core",
"futures-task",
"futures-util",
]
[[package]]
name = "futures-io"
version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9e5c1b78ca4aae1ac06c48a526a655760685149f0d465d21f37abfe57ce075c6"
[[package]]
name = "futures-macro"
version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "162ee34ebcb7c64a8abebc059ce0fee27c2262618d7b60ed8faf72fef13c3650"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "futures-sink"
version = "0.3.31"
@@ -369,8 +490,10 @@ version = "0.3.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81"
dependencies = [
"futures-channel",
"futures-core",
"futures-io",
"futures-macro",
"futures-sink",
"futures-task",
"memchr",
@@ -379,6 +502,16 @@ dependencies = [
"slab",
]
[[package]]
name = "generic-array"
version = "0.14.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a"
dependencies = [
"typenum",
"version_check",
]
[[package]]
name = "getrandom"
version = "0.2.16"
@@ -437,6 +570,15 @@ version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
[[package]]
name = "hmac"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c49c37c09c17a53d937dfbb742eb3a961d65a994e6bcdcf37e7399d0cc8ab5e"
dependencies = [
"digest",
]
[[package]]
name = "http"
version = "1.4.0"
@@ -760,7 +902,7 @@ checksum = "3d0b95e02c851351f877147b7deea7b1afb1df71b63aa5f8270716e0c5720616"
dependencies = [
"bitflags",
"libc",
"redox_syscall",
"redox_syscall 0.7.0",
]
[[package]]
@@ -775,6 +917,15 @@ version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6373607a59f0be73a39b6fe456b8192fcc3585f602af20751600e974dd455e77"
[[package]]
name = "lock_api"
version = "0.4.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "224399e74b87b5f3557511d98dff8b14089b3dadafcab6bb93eab67d3aace965"
dependencies = [
"scopeguard",
]
[[package]]
name = "log"
version = "0.4.29"
@@ -787,6 +938,25 @@ version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "112b39cec0b298b6c1999fee3e31427f74f676e4cb9879ed1a121b43661a4154"
[[package]]
name = "matchers"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d1525a2a28c7f4fa0fc98bb91ae755d1e2d1505079e05539e35bc876b5d65ae9"
dependencies = [
"regex-automata",
]
[[package]]
name = "md-5"
version = "0.10.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d89e7ee0cfbedfc4da3340218492196241d89eefb6dab27de5df917a6d2e78cf"
dependencies = [
"cfg-if",
"digest",
]
[[package]]
name = "memchr"
version = "2.7.6"
@@ -849,6 +1019,12 @@ dependencies = [
"num-traits",
]
[[package]]
name = "num-conv"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9"
[[package]]
name = "num-integer"
version = "0.1.46"
@@ -867,6 +1043,15 @@ dependencies = [
"autocfg",
]
[[package]]
name = "num_threads"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c7398b9c8b70908f6371f47ed36737907c87c52af34c268fed0bf0ceb92ead9"
dependencies = [
"libc",
]
[[package]]
name = "once_cell"
version = "1.21.3"
@@ -917,12 +1102,54 @@ dependencies = [
"vcpkg",
]
[[package]]
name = "parking_lot"
version = "0.12.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "93857453250e3077bd71ff98b6a65ea6621a19bb0f559a85248955ac12c45a1a"
dependencies = [
"lock_api",
"parking_lot_core",
]
[[package]]
name = "parking_lot_core"
version = "0.9.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2621685985a2ebf1c516881c026032ac7deafcda1a2c9b7850dc81e3dfcb64c1"
dependencies = [
"cfg-if",
"libc",
"redox_syscall 0.5.18",
"smallvec",
"windows-link",
]
[[package]]
name = "percent-encoding"
version = "2.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220"
[[package]]
name = "phf"
version = "0.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c1562dc717473dbaa4c1f85a36410e03c047b2e7df7f45ee938fbef64ae7fadf"
dependencies = [
"phf_shared",
"serde",
]
[[package]]
name = "phf_shared"
version = "0.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e57fef6bc5981e38c2ce2d63bfa546861309f875b8a75f092d1d54ae2d64f266"
dependencies = [
"siphasher",
]
[[package]]
name = "pin-project-lite"
version = "0.2.16"
@@ -943,7 +1170,7 @@ checksum = "7edddbd0b52d732b21ad9a5fab5c704c14cd949e5e9a1ec5929a24fded1b904c"
[[package]]
name = "portabase-agent"
version = "0.1.1-rc.6"
version = "0.1.2-rc.1"
dependencies = [
"anyhow",
"async-trait",
@@ -952,6 +1179,7 @@ dependencies = [
"cron",
"dotenvy",
"flate2",
"futures",
"hex",
"log",
"once_cell",
@@ -963,12 +1191,45 @@ dependencies = [
"tar",
"tempfile",
"thiserror 2.0.17",
"time",
"tokio",
"tokio-postgres",
"toml",
"tracing",
"tracing-appender",
"tracing-log",
"tracing-subscriber",
]
[[package]]
name = "postgres-protocol"
version = "0.6.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fbef655056b916eb868048276cfd5d6a7dea4f81560dfd047f97c8c6fe3fcfd4"
dependencies = [
"base64",
"byteorder",
"bytes",
"fallible-iterator",
"hmac",
"md-5",
"memchr",
"rand",
"sha2",
"stringprep",
]
[[package]]
name = "postgres-types"
version = "0.2.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ef4605b7c057056dd35baeb6ac0c0338e4975b1f2bef0f65da953285eb007095"
dependencies = [
"bytes",
"fallible-iterator",
"postgres-protocol",
]
[[package]]
name = "potential_utf"
version = "0.1.4"
@@ -978,6 +1239,12 @@ dependencies = [
"zerovec",
]
[[package]]
name = "powerfmt"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391"
[[package]]
name = "ppv-lite86"
version = "0.2.21"
@@ -1120,6 +1387,15 @@ dependencies = [
"xxhash-rust",
]
[[package]]
name = "redox_syscall"
version = "0.5.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed2bf2547551a7053d6fdfafda3f938979645c44812fbfcda098faae3f1a362d"
dependencies = [
"bitflags",
]
[[package]]
name = "redox_syscall"
version = "0.7.0"
@@ -1129,6 +1405,23 @@ dependencies = [
"bitflags",
]
[[package]]
name = "regex-automata"
version = "0.4.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5276caf25ac86c8d810222b3dbb938e512c55c6831a10f3e6ed1c93b84041f1c"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.8.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a2d987857b319362043e95f5353c0535c1f58eec5336fdfcf626430af7def58"
[[package]]
name = "reqwest"
version = "0.13.1"
@@ -1310,6 +1603,12 @@ dependencies = [
"windows-sys 0.61.2",
]
[[package]]
name = "scopeguard"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
[[package]]
name = "security-framework"
version = "3.5.1"
@@ -1391,6 +1690,17 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbfa15b3dddfee50a0fff136974b3e1bde555604ba463834a7eb7deb6417705d"
[[package]]
name = "sha2"
version = "0.10.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a7507d819769d01a365ab707794a4084392c824f54a7a6a7862f8c3d0892b283"
dependencies = [
"cfg-if",
"cpufeatures",
"digest",
]
[[package]]
name = "sharded-slab"
version = "0.1.7"
@@ -1412,6 +1722,12 @@ version = "0.3.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e320a6c5ad31d271ad523dcf3ad13e2767ad8b1cb8f047f75a8aeaf8da139da2"
[[package]]
name = "siphasher"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56199f7ddabf13fe5074ce809e7d3f42b42ae711800501b5b16ea82ad029c39d"
[[package]]
name = "slab"
version = "0.4.11"
@@ -1440,6 +1756,17 @@ version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596"
[[package]]
name = "stringprep"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b4df3d392d81bd458a8a621b8bffbd2302a12ffe288a9d931670948749463b1"
dependencies = [
"unicode-bidi",
"unicode-normalization",
"unicode-properties",
]
[[package]]
name = "subtle"
version = "2.6.1"
@@ -1571,6 +1898,39 @@ dependencies = [
"cfg-if",
]
[[package]]
name = "time"
version = "0.3.44"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "91e7d9e3bb61134e77bde20dd4825b97c010155709965fedf0f49bb138e52a9d"
dependencies = [
"deranged",
"itoa",
"libc",
"num-conv",
"num_threads",
"powerfmt",
"serde",
"time-core",
"time-macros",
]
[[package]]
name = "time-core"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "40868e7c1d2f0b8d73e4a8c7f0ff63af4f6d19be117e90bd73eb1d62cf831c6b"
[[package]]
name = "time-macros"
version = "0.2.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "30cfb0125f12d9c277f35663a0a33f8c30190f4e4574868a330595412d34ebf3"
dependencies = [
"num-conv",
"time-core",
]
[[package]]
name = "tinystr"
version = "0.8.2"
@@ -1622,6 +1982,32 @@ dependencies = [
"syn",
]
[[package]]
name = "tokio-postgres"
version = "0.7.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b40d66d9b2cfe04b628173409368e58247e8eddbbd3b0e6c6ba1d09f20f6c9e"
dependencies = [
"async-trait",
"byteorder",
"bytes",
"fallible-iterator",
"futures-channel",
"futures-util",
"log",
"parking_lot",
"percent-encoding",
"phf",
"pin-project-lite",
"postgres-protocol",
"postgres-types",
"rand",
"socket2",
"tokio",
"tokio-util",
"whoami",
]
[[package]]
name = "tokio-rustls"
version = "0.26.4"
@@ -1740,6 +2126,18 @@ dependencies = [
"tracing-core",
]
[[package]]
name = "tracing-appender"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "786d480bce6247ab75f005b14ae1624ad978d3029d9113f0a22fa1ac773faeaf"
dependencies = [
"crossbeam-channel",
"thiserror 2.0.17",
"time",
"tracing-subscriber",
]
[[package]]
name = "tracing-attributes"
version = "0.1.31"
@@ -1778,10 +2176,16 @@ version = "0.3.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2f30143827ddab0d256fd843b7a66d164e9f271cfa0dde49142c5ca0ca291f1e"
dependencies = [
"chrono",
"matchers",
"nu-ansi-term",
"once_cell",
"regex-automata",
"sharded-slab",
"smallvec",
"thread_local",
"time",
"tracing",
"tracing-core",
"tracing-log",
]
@@ -1792,18 +2196,45 @@ version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b"
[[package]]
name = "typenum"
version = "1.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "562d481066bde0658276a35467c4af00bdc6ee726305698a55b86e61d7ad82bb"
[[package]]
name = "unicase"
version = "2.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "75b844d17643ee918803943289730bec8aac480150456169e647ed0b576ba539"
[[package]]
name = "unicode-bidi"
version = "0.3.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c1cb5db39152898a79168971543b1cb5020dff7fe43c8dc468b0885f5e29df5"
[[package]]
name = "unicode-ident"
version = "1.0.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5"
[[package]]
name = "unicode-normalization"
version = "0.1.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5fd4f6878c9cb28d874b009da9e8d183b5abc80117c40bbd187a1fde336be6e8"
dependencies = [
"tinyvec",
]
[[package]]
name = "unicode-properties"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7df058c713841ad818f1dc5d3fd88063241cc61f49f5fbea4b951e8cf5a8d71d"
[[package]]
name = "untrusted"
version = "0.9.0"
@@ -1840,6 +2271,12 @@ version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
[[package]]
name = "version_check"
version = "0.9.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
[[package]]
name = "walkdir"
version = "2.5.0"
@@ -1874,6 +2311,12 @@ dependencies = [
"wit-bindgen",
]
[[package]]
name = "wasite"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8dad83b4f25e74f184f64c43b150b91efe7647395b42289f38e50566d82855b"
[[package]]
name = "wasm-bindgen"
version = "0.2.106"
@@ -1961,6 +2404,17 @@ dependencies = [
"rustls-pki-types",
]
[[package]]
name = "whoami"
version = "1.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5d4a4db5077702ca3015d3d02d74974948aba2ad9e12ab7df718ee64ccd7e97d"
dependencies = [
"libredox",
"wasite",
"web-sys",
]
[[package]]
name = "winapi-util"
version = "0.1.11"
+7 -2
View File
@@ -1,6 +1,6 @@
[package]
name = "portabase-agent"
version = "0.1.1-rc.7"
version = "1.0.0-rc.1"
edition = "2024"
[dependencies]
@@ -10,7 +10,7 @@ chrono = { version = "0.4", features = ["serde"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
tracing = "0.1"
tracing-subscriber = "0.3"
tracing-subscriber = { version = "0.3", features = ["env-filter", "fmt", "local-time", "chrono"] }
dotenvy = "0.15"
once_cell = "1.17"
base64 = "0.22.1"
@@ -26,6 +26,11 @@ openssl = "0.10.75"
hex = "0.4.3"
flate2 = "1.1.5"
tar = "0.4.44"
tokio-postgres = "0.7.15"
futures = "0.3.31"
tracing-log = "0.2.0"
tracing-appender = "0.2.4"
time = { version = "0.3.44", features = ["macros"] }
[[bin]]
name = "app"
BIN
View File
Binary file not shown.
+34
View File
@@ -0,0 +1,34 @@
{
"databases": [
{
"name": "Test database 1 - rust",
"database": "devdb",
"type": "postgresql",
"username": "devuser",
"password": "changeme",
"port": 5432,
"host": "db",
"generated_id": "16678159-ff7e-4c97-8c83-0adeff214681"
},
{
"name": "Test database 2 - rust",
"database": "devdb",
"type": "postgresql",
"username": "devuser",
"password": "changeme",
"port": 5432,
"host": "db",
"generated_id": "16678156-ff7e-4c97-8c83-0adeff214681"
},
{
"name": "Test database 3 - rust",
"database": "mariadb",
"type": "mysql",
"username": "mariadb",
"password": "changeme",
"port": 3306,
"host": "db2",
"generated_id": "16678124-ff7e-4c97-8c83-0adeff214681"
}
]
}
+3 -3
View File
@@ -8,11 +8,11 @@ services:
volumes:
- ./databases.toml:/config/config.toml
environment:
APP_ENV: production
RUST_LOG: info
# APP_ENV: production
LOG: info
TZ: "Europe/Paris"
DATABASES_CONFIG_FILE: "config.toml"
EDGE_KEY: "eyJzZXJ2ZXJVcmwiOiJodHRwOi8vbG9jYWxob3N0Ojg4ODciLCJhZ2VudElkIjoiNzIxZTE0YTQtZDFmZi00OTM4LWFjYzgtNGY1ZWRmMjllODkzIiwicHVibGljS2V5IjoiLS0tLS1CRUdJTiBSU0EgUFVCTElDIEtFWS0tLS0tXG5NSUlCQ2dLQ0FRRUFuYUVKdnVBSExlNGF5d0RmNGplelVobm45VXRkdksyZ3pEMEg2cERJYXczYkJKRkpwVnVDXG5uVFV3MXA3Q2RnOXBzdjZhRnpyOXZPd0J2MjMzckxpdVpCT2lCb2p2Q0QrSlZid3hyTzBRRW5hN2dmaHV1ZGYwXG5VVlJOMkxmK1g1aTkvZzJTNm5xcExoTm1DaGFJNk8ybktYZUNlRmtubEErRUJrNnFoV1FCVGozb05TYTFTOFY1XG40UFRTT2I4NUo3a2k5YllEbXRiNWxrU3dCNXdXOTdtQjg0ZzI2WHAvU3FFcmhKc0NGK3YrN09vTWYzTzJqTTNoXG5XMUQ0MzBPRitWaklwUGdoV09rZy96NXZQUWFHRzhqQ0h4VDlJR0Q0bjhyS05LQ3FTOGNyN2diTGU0cWpNdmhvXG5BQVVvaHpHR2FRNkhlWlJ4S0UvM3J1a2JldnY5dnJ2TTNRSURBUUFCXG4tLS0tLUVORCBSU0EgUFVCTElDIEtFWS0tLS0tXG4ifQ"
EDGE_KEY: "eyJzZXJ2ZXJVcmwiOiJodHRwOi8vbG9jYWxob3N0Ojg4ODciLCJhZ2VudElkIjoiOThlZmY1YjEtMDQyZC00MDkxLWJkYjctZTgzYmM0YjczNjAxIiwicHVibGljS2V5IjoiLS0tLS1CRUdJTiBSU0EgUFVCTElDIEtFWS0tLS0tXG5NSUlCQ2dLQ0FRRUFuYUVKdnVBSExlNGF5d0RmNGplelVobm45VXRkdksyZ3pEMEg2cERJYXczYkJKRkpwVnVDXG5uVFV3MXA3Q2RnOXBzdjZhRnpyOXZPd0J2MjMzckxpdVpCT2lCb2p2Q0QrSlZid3hyTzBRRW5hN2dmaHV1ZGYwXG5VVlJOMkxmK1g1aTkvZzJTNm5xcExoTm1DaGFJNk8ybktYZUNlRmtubEErRUJrNnFoV1FCVGozb05TYTFTOFY1XG40UFRTT2I4NUo3a2k5YllEbXRiNWxrU3dCNXdXOTdtQjg0ZzI2WHAvU3FFcmhKc0NGK3YrN09vTWYzTzJqTTNoXG5XMUQ0MzBPRitWaklwUGdoV09rZy96NXZQUWFHRzhqQ0h4VDlJR0Q0bjhyS05LQ3FTOGNyN2diTGU0cWpNdmhvXG5BQVVvaHpHR2FRNkhlWlJ4S0UvM3J1a2JldnY5dnJ2TTNRSURBUUFCXG4tLS0tLUVORCBSU0EgUFVCTElDIEtFWS0tLS0tXG4ifQ"
extra_hosts:
- "localhost:host-gateway"
networks:
+5 -3
View File
@@ -7,14 +7,16 @@ services:
container_name: rust-dev
volumes:
- .:/app
- ./databases.toml:/config/config.toml
- ./databases.json:/config/config.json
# - ./databases.toml:/config/config.toml
- cargo-registry:/usr/local/cargo/registry
- cargo-git:/usr/local/cargo/git
environment:
APP_ENV: development
RUST_LOG: debug
LOG: debug
# POOLING: 1
TZ: "Europe/Paris"
DATABASES_CONFIG_FILE: "config.toml"
# DATABASES_CONFIG_FILE: "config.toml"
EDGE_KEY: "eyJzZXJ2ZXJVcmwiOiJodHRwOi8vbG9jYWxob3N0Ojg4ODciLCJhZ2VudElkIjoiOThlZmY1YjEtMDQyZC00MDkxLWJkYjctZTgzYmM0YjczNjAxIiwicHVibGljS2V5IjoiLS0tLS1CRUdJTiBSU0EgUFVCTElDIEtFWS0tLS0tXG5NSUlCQ2dLQ0FRRUFuYUVKdnVBSExlNGF5d0RmNGplelVobm45VXRkdksyZ3pEMEg2cERJYXczYkJKRkpwVnVDXG5uVFV3MXA3Q2RnOXBzdjZhRnpyOXZPd0J2MjMzckxpdVpCT2lCb2p2Q0QrSlZid3hyTzBRRW5hN2dmaHV1ZGYwXG5VVlJOMkxmK1g1aTkvZzJTNm5xcExoTm1DaGFJNk8ybktYZUNlRmtubEErRUJrNnFoV1FCVGozb05TYTFTOFY1XG40UFRTT2I4NUo3a2k5YllEbXRiNWxrU3dCNXdXOTdtQjg0ZzI2WHAvU3FFcmhKc0NGK3YrN09vTWYzTzJqTTNoXG5XMUQ0MzBPRitWaklwUGdoV09rZy96NXZQUWFHRzhqQ0h4VDlJR0Q0bjhyS05LQ3FTOGNyN2diTGU0cWpNdmhvXG5BQVVvaHpHR2FRNkhlWlJ4S0UvM3J1a2JldnY5dnJ2TTNRSURBUUFCXG4tLS0tLUVORCBSU0EgUFVCTElDIEtFWS0tLS0tXG4ifQ"
extra_hosts:
- "localhost:host-gateway"
+8 -107
View File
@@ -1,115 +1,8 @@
## =========================
## Base image (shared)
## =========================
#FROM rust:1.92.0 AS base
#
#RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y \
# pkg-config \
# libssl-dev \
# tzdata \
# redis-server \
# ca-certificates \
# curl \
# && apt-get clean \
# && rm -rf /var/lib/apt/lists/*
#
#
#
## ========= Install PostgreSQL client binaries (versions 12-18) =========
## Pre-downloaded binaries from assets/tools/ - no network download needed
#ARG TARGETARCH
#RUN mkdir -p /usr/lib/postgresql/12/bin /usr/lib/postgresql/13/bin \
# /usr/lib/postgresql/14/bin /usr/lib/postgresql/15/bin \
# /usr/lib/postgresql/16/bin /usr/lib/postgresql/17/bin \
# /usr/lib/postgresql/18/bin
#
## Copy pre-downloaded PostgreSQL binaries based on architecture
#COPY assets/tools/x64/postgresql/ /tmp/pg-x64/
#COPY assets/tools/arm/postgresql/ /tmp/pg-arm/
#RUN if [ "$TARGETARCH" = "amd64" ]; then \
# cp -r /tmp/pg-x64/postgresql-12/bin/* /usr/lib/postgresql/12/bin/ && \
# cp -r /tmp/pg-x64/postgresql-13/bin/* /usr/lib/postgresql/13/bin/ && \
# cp -r /tmp/pg-x64/postgresql-14/bin/* /usr/lib/postgresql/14/bin/ && \
# cp -r /tmp/pg-x64/postgresql-15/bin/* /usr/lib/postgresql/15/bin/ && \
# cp -r /tmp/pg-x64/postgresql-16/bin/* /usr/lib/postgresql/16/bin/ && \
# cp -r /tmp/pg-x64/postgresql-17/bin/* /usr/lib/postgresql/17/bin/ && \
# cp -r /tmp/pg-x64/postgresql-18/bin/* /usr/lib/postgresql/18/bin/; \
# elif [ "$TARGETARCH" = "arm64" ]; then \
# cp -r /tmp/pg-arm/postgresql-12/bin/* /usr/lib/postgresql/12/bin/ && \
# cp -r /tmp/pg-arm/postgresql-13/bin/* /usr/lib/postgresql/13/bin/ && \
# cp -r /tmp/pg-arm/postgresql-14/bin/* /usr/lib/postgresql/14/bin/ && \
# cp -r /tmp/pg-arm/postgresql-15/bin/* /usr/lib/postgresql/15/bin/ && \
# cp -r /tmp/pg-arm/postgresql-16/bin/* /usr/lib/postgresql/16/bin/ && \
# cp -r /tmp/pg-arm/postgresql-17/bin/* /usr/lib/postgresql/17/bin/ && \
# cp -r /tmp/pg-arm/postgresql-18/bin/* /usr/lib/postgresql/18/bin/; \
# fi && \
# rm -rf /tmp/pg-x64 /tmp/pg-arm && \
# chmod +x /usr/lib/postgresql/*/bin/*
#
#
#
#WORKDIR /app
#
## =========================
## Development image
## =========================
#FROM base AS dev
#
#RUN cargo install cargo-watch
#
## Pre-cache dependencies
#COPY Cargo.toml Cargo.lock ./
#RUN mkdir src && echo "fn main() {}" > src/main.rs
#RUN cargo build
#RUN rm -rf src
#
#COPY entrypoint.sh /entrypoint.sh
#RUN chmod +x /entrypoint.sh
#
#CMD ["/entrypoint.sh"]
#
## =========================
## Builder (production)
## =========================
#FROM base AS builder
#
#COPY . .
#RUN cargo build --release
#
#RUN echo "APP_VERSION=$(cargo pkgid | awk -F# '{print $2}')" > /app/version.env
#
## =========================
## Runtime (production)
## =========================
#FROM debian:bookworm-slim AS prod
##FROM ubuntu:24.04 AS prod
#
#
#RUN apt-get update && apt-get install -y \
# redis-server \
# ca-certificates \
# tzdata \
# postgresql-client \
# libpq5 \
# mariadb-client \
# && rm -rf /var/lib/apt/lists/*
#
#WORKDIR /app
#
#COPY --from=builder /app/target/release/app /usr/local/bin/app
#COPY --from=builder /app/version.env /app/version.env
#COPY entrypoint.sh /entrypoint.sh
#RUN chmod +x /entrypoint.sh
#
#ENV APP_ENV=production
#
#CMD ["/entrypoint.sh"]
# =========================
# Base image (shared)
# =========================
FROM rust:1.92.0 AS base
# Install common dependencies
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y \
pkg-config \
libssl-dev \
@@ -117,7 +10,11 @@ RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y \
redis-server \
ca-certificates \
libpq5 \
libreadline8 \
libncurses6 \
zlib1g \
curl \
mariadb-client \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
@@ -187,9 +84,13 @@ RUN apt-get update && apt-get install -y \
ca-certificates \
tzdata \
libpq5 \
libreadline8 \
libncurses6 \
zlib1g \
mariadb-client \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /app
COPY --from=builder /app/target/release/app /usr/local/bin/app
+14 -13
View File
@@ -1,11 +1,12 @@
#![allow(dead_code)]
use crate::domain::postgres::PostgresDatabase;
use crate::services::config::DatabaseConfig;
use crate::domain::mysql::database::MySQLDatabase;
use crate::domain::postgres::database::PostgresDatabase;
use crate::domain::postgres::{detect_format_from_file, detect_format_from_size};
use crate::services::config::{DatabaseConfig, DbType};
use anyhow::Result;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use crate::domain::mysql::MySQLDatabase;
#[async_trait::async_trait]
pub trait Database: Send + Sync {
@@ -20,24 +21,24 @@ pub struct DatabaseFactory;
impl DatabaseFactory {
pub async fn create_for_backup(cfg: DatabaseConfig) -> Arc<dyn Database> {
match cfg.db_type.as_str() {
"postgresql" => {
let format = PostgresDatabase::detect_format_from_size(&cfg).await;
match cfg.db_type {
DbType::Postgresql => {
let format = detect_format_from_size(&cfg).await;
Arc::new(PostgresDatabase::new(cfg, format))
}
"mysql" => Arc::new(MySQLDatabase::new(cfg)),
_ => panic!("Unsupported DB type: {}", cfg.db_type),
DbType::Mysql => Arc::new(MySQLDatabase::new(cfg)),
DbType::Mariadb => Arc::new(MySQLDatabase::new(cfg)),
}
}
pub async fn create_for_restore(cfg: DatabaseConfig, restore_file: &Path) -> Arc<dyn Database> {
match cfg.db_type.as_str() {
"postgresql" => {
let format = PostgresDatabase::detect_format_from_file(restore_file);
match cfg.db_type {
DbType::Postgresql => {
let format = detect_format_from_file(restore_file);
Arc::new(PostgresDatabase::new(cfg, format))
}
"mysql" => Arc::new(MySQLDatabase::new(cfg)),
_ => panic!("Unsupported DB type: {}", cfg.db_type),
DbType::Mysql => Arc::new(MySQLDatabase::new(cfg)),
DbType::Mariadb => Arc::new(MySQLDatabase::new(cfg)),
}
}
}
+1
View File
@@ -1,3 +1,4 @@
pub mod factory;
pub mod postgres;
pub mod mysql;
-140
View File
@@ -1,140 +0,0 @@
use crate::domain::factory::Database;
use crate::services::config::DatabaseConfig;
use anyhow::{Context, Result};
use std::collections::HashMap;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::process::Command;
pub struct MySQLDatabase {
cfg: DatabaseConfig,
}
impl MySQLDatabase {
pub fn new(cfg: DatabaseConfig) -> Self {
Self { cfg }
}
fn build_env(&self) -> HashMap<String, String> {
let mut envs = std::env::vars().collect::<HashMap<_, _>>();
envs.insert("MYSQL_PWD".to_string(), self.cfg.password.clone());
envs
}
}
#[async_trait::async_trait]
impl Database for MySQLDatabase {
fn file_extension(&self) -> &'static str {
".sql"
}
async fn ping(&self) -> Result<bool> {
let output = Command::new("mysqladmin")
.arg("--host")
.arg(&self.cfg.host)
.arg("--port")
.arg(self.cfg.port.to_string())
.arg("--user")
.arg(&self.cfg.username)
.arg("ping")
.envs(self.build_env())
.output()
.with_context(|| format!("Failed to ping MySQL server {}", self.cfg.name))?;
Ok(output.status.success())
}
async fn backup(&self, backup_dir: &Path) -> Result<PathBuf> {
let file_path = backup_dir.join(format!(
"{}{}",
self.cfg.generated_id,
self.file_extension()
));
let output = Command::new("mysqldump")
.arg("--host")
.arg(&self.cfg.host)
.arg("--port")
.arg(self.cfg.port.to_string())
.arg("--user")
.arg(&self.cfg.username)
.arg("--routines")
.arg("--events")
.arg("--triggers")
.arg("--verbose")
.arg("--single-transaction")
.arg("--quick")
.arg("--add-drop-database")
.arg("--databases")
.arg(&self.cfg.database)
.arg("-r")
.arg(&file_path)
.envs(self.build_env())
.output()
.with_context(|| format!("Failed to run mysqldump for {}", self.cfg.name))?;
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
anyhow::bail!("MySQL backup failed for {}: {}", self.cfg.name, stderr);
}
Ok(file_path)
}
async fn restore(&self, restore_file: &Path) -> Result<()> {
let sql_content = tokio::fs::read_to_string(restore_file)
.await
.with_context(|| format!("Failed to read restore file {}", restore_file.display()))?;
let drop_create_cmd = format!(
"DROP DATABASE IF EXISTS {0}; CREATE DATABASE {0};",
self.cfg.database
);
let drop_status = Command::new("mysql")
.arg("--host")
.arg(&self.cfg.host)
.arg("--port")
.arg(self.cfg.port.to_string())
.arg("--user")
.arg(&self.cfg.username)
.arg("-e")
.arg(&drop_create_cmd)
.env("MYSQL_PWD", &self.cfg.password)
.status()
.with_context(|| format!("Failed to drop/recreate database {}", self.cfg.name))?;
if !drop_status.success() {
anyhow::bail!("Failed to drop/recreate database {}", self.cfg.name);
}
let mut child = Command::new("mysql")
.arg("--host")
.arg(&self.cfg.host)
.arg("--port")
.arg(self.cfg.port.to_string())
.arg("--user")
.arg(&self.cfg.username)
.arg(&self.cfg.database)
.env("MYSQL_PWD", &self.cfg.password)
.stdin(std::process::Stdio::piped())
.spawn()
.with_context(|| format!("Failed to start mysql restore for {}", self.cfg.name))?;
let mut stdin = child.stdin.take().context("Failed to open child stdin")?;
stdin.write_all(sql_content.as_bytes())?;
stdin.flush()?;
drop(stdin);
let output = child
.wait_with_output()
.with_context(|| format!("Failed to complete mysql restore for {}", self.cfg.name))?;
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
anyhow::bail!("MySQL restore failed for {}: {}", self.cfg.name, stderr);
}
Ok(())
}
}
+50
View File
@@ -0,0 +1,50 @@
use anyhow::{Context, Result};
use tracing::debug;
use std::collections::HashMap;
use std::path::PathBuf;
use std::process::Command;
use crate::services::config::DatabaseConfig;
pub async fn run(
cfg: DatabaseConfig,
backup_dir: PathBuf,
env: HashMap<String, String>,
file_extension: &'static str,
) -> Result<PathBuf> {
tokio::task::spawn_blocking(move || -> Result<PathBuf> {
debug!("Starting backup for database {}", cfg.name);
let file_path = backup_dir.join(format!("{}{}", cfg.generated_id, file_extension));
let output = Command::new("mysqldump")
.arg("--host")
.arg(cfg.host)
.arg("--port")
.arg(cfg.port.to_string())
.arg("--user")
.arg(cfg.username)
.arg("--routines")
.arg("--events")
.arg("--triggers")
.arg("--verbose")
.arg("--single-transaction")
.arg("--quick")
.arg("--add-drop-database")
.arg("--databases")
.arg(cfg.database)
.arg("-r")
.arg(&file_path)
.envs(env)
.output()
.with_context(|| format!("Failed to run mysqldump for {}", cfg.name))?;
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
anyhow::bail!("MySQL backup failed for {}: {}", cfg.name, stderr);
}
Ok(file_path)
})
.await?
}
+52
View File
@@ -0,0 +1,52 @@
use std::collections::HashMap;
use anyhow::Result;
use async_trait::async_trait;
use std::path::{Path, PathBuf};
use super::{
backup,
ping, restore,
};
use crate::domain::factory::Database;
use crate::services::config::DatabaseConfig;
use crate::utils::locks::{DbOpLock, FileLock};
pub struct MySQLDatabase {
cfg: DatabaseConfig,
}
impl MySQLDatabase {
pub fn new(cfg: DatabaseConfig) -> Self {
Self { cfg }
}
fn build_env(&self) -> HashMap<String, String> {
let mut envs = std::env::vars().collect::<HashMap<_, _>>();
envs.insert("MYSQL_PWD".to_string(), self.cfg.password.clone());
envs
}
}
#[async_trait]
impl Database for MySQLDatabase {
fn file_extension(&self) -> &'static str {
".sql"
}
async fn ping(&self) -> Result<bool> {
ping::run(self.cfg.clone(), self.build_env().clone()).await
}
async fn backup(&self, dir: &Path) -> Result<PathBuf> {
FileLock::acquire(&self.cfg.generated_id, DbOpLock::Backup.as_str()).await?;
let res = backup::run(self.cfg.clone(), dir.to_path_buf(), self.build_env().clone(), self.file_extension()).await;
FileLock::release(&self.cfg.generated_id).await?;
res
}
async fn restore(&self, file: &Path) -> Result<()> {
FileLock::acquire(&self.cfg.generated_id, DbOpLock::Restore.as_str()).await?;
let res = restore::run(self.cfg.clone(), file.to_path_buf()).await;
FileLock::release(&self.cfg.generated_id).await?;
res
}
}
+4
View File
@@ -0,0 +1,4 @@
pub mod backup;
pub mod database;
mod restore;
mod ping;
+19
View File
@@ -0,0 +1,19 @@
use crate::services::config::DatabaseConfig;
use anyhow::Context;
use std::collections::HashMap;
use std::process::Command;
pub async fn run(cfg: DatabaseConfig, env: HashMap<String, String>) -> anyhow::Result<bool> {
let output = Command::new("mysqladmin")
.arg("--host")
.arg(cfg.host)
.arg("--port")
.arg(cfg.port.to_string())
.arg("--user")
.arg(cfg.username)
.arg("ping")
.envs(env)
.output()
.with_context(|| format!("Failed to ping MySQL server {}", cfg.name))?;
Ok(output.status.success())
}
+81
View File
@@ -0,0 +1,81 @@
use std::fs::File;
use std::io::{Read, Write};
use anyhow::{Context, Result};
use tracing::{debug, error, info};
use std::path::PathBuf;
use std::process::Command;
use crate::services::config::DatabaseConfig;
pub async fn run(cfg: DatabaseConfig, restore_file: PathBuf) -> Result<()> {
let handle = tokio::task::spawn_blocking(move || -> Result<()> {
debug!("Starting restore for database {}", cfg.name);
let mut sql_content = String::new();
let mut file = File::open(&restore_file)
.with_context(|| format!("Failed to open restore file {}", restore_file.display()))?;
file.read_to_string(&mut sql_content)
.with_context(|| format!("Failed to read restore file {}", restore_file.display()))?;
let drop_create_cmd = format!(
"DROP DATABASE IF EXISTS {0}; CREATE DATABASE {0};",
cfg.database
);
let drop_status = Command::new("mysql")
.arg("--host")
.arg(&cfg.host)
.arg("--port")
.arg(cfg.port.to_string())
.arg("--user")
.arg(&cfg.username)
.arg("-e")
.arg(&drop_create_cmd)
.env("MYSQL_PWD", &cfg.password)
.status()
.with_context(|| format!("Failed to drop/recreate database {}", cfg.name))?;
if !drop_status.success() {
error!("Drop/create database failed for {}", cfg.name);
anyhow::bail!("Failed to drop/recreate database {}", cfg.name);
}
info!("Database {} dropped and recreated", cfg.name);
let mut child = Command::new("mysql")
.arg("--host")
.arg(&cfg.host)
.arg("--port")
.arg(cfg.port.to_string())
.arg("--user")
.arg(&cfg.username)
.arg(&cfg.database)
.env("MYSQL_PWD", &cfg.password)
.stdin(std::process::Stdio::piped())
.spawn()
.with_context(|| format!("Failed to start mysql restore for {}", cfg.name))?;
let mut stdin = child.stdin.take().context("Failed to open child stdin")?;
stdin.write_all(sql_content.as_bytes())
.context("Failed to write SQL content to mysql stdin")?;
stdin.flush()?;
drop(stdin);
let output = child
.wait_with_output()
.with_context(|| format!("Failed to complete mysql restore for {}", cfg.name))?;
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
error!("MySQL restore failed for {}: {}", cfg.name, stderr);
anyhow::bail!("MySQL restore failed for {}", cfg.name);
}
info!("Restore finished successfully for database {}", cfg.name);
Ok(())
});
handle
.await??;
Ok(())
}
-301
View File
@@ -1,301 +0,0 @@
#![allow(dead_code)]
use crate::domain::factory::Database;
use crate::services::config::DatabaseConfig;
use anyhow::{Context as AnyhowContext, Result};
use async_trait::async_trait;
use flate2::Compression;
use flate2::write::GzEncoder;
use log::info;
use std::path::{Path, PathBuf};
use std::process::Command;
use tracing::debug;
#[derive(Clone, Copy)]
pub enum PostgresDumpFormat {
Fc, // legacy
Fd, // directory format
}
pub struct PostgresDatabase {
cfg: DatabaseConfig,
format: PostgresDumpFormat,
}
impl PostgresDatabase {
pub fn new(cfg: DatabaseConfig, format: PostgresDumpFormat) -> Self {
Self { cfg, format }
}
pub fn detect_format_from_file(restore_file: &Path) -> PostgresDumpFormat {
match restore_file.extension().and_then(|e| e.to_str()) {
Some("dump") => PostgresDumpFormat::Fc,
Some("gz") => PostgresDumpFormat::Fd,
// Some("tar.gz") => PostgresDumpFormat::Fd,
_ => PostgresDumpFormat::Fc,
}
}
pub async fn detect_format_from_size(cfg: &DatabaseConfig) -> PostgresDumpFormat {
let url = format!(
"host={} port={} user={} password={} dbname={}",
cfg.host, cfg.port, cfg.username, cfg.password, cfg.database
);
let output = std::process::Command::new("psql")
.arg(&url)
.arg("-t")
.arg("-c")
.arg("SELECT pg_database_size(current_database());")
.output();
match output {
Ok(out) if out.status.success() => {
let size_bytes: i64 = String::from_utf8_lossy(&out.stdout)
.trim()
.parse()
.unwrap_or(0);
// > 1 Go
if size_bytes > 1_000_000_000 {
PostgresDumpFormat::Fd
} else {
PostgresDumpFormat::Fc
}
}
_ => PostgresDumpFormat::Fc, // fallback legacy
}
}
fn get_postgres_server_version(cfg: &DatabaseConfig) -> Result<String, anyhow::Error> {
let output = Command::new("/usr/lib/postgresql/17/bin/psql")
.arg("-U")
.arg(&cfg.username)
.arg("-h")
.arg(&cfg.host)
.arg("-p")
.arg(cfg.port.to_string())
.arg("-d")
.arg(&cfg.database)
.arg("-t") // only return value
.arg("-c")
.arg("SHOW server_version;")
.env("PGPASSWORD", &cfg.password)
.output()?;
if !output.status.success() {
anyhow::bail!("Failed to get PostgreSQL version");
}
let version_str = String::from_utf8_lossy(&output.stdout).trim().to_string();
let major_version: u32 = version_str
.split('.')
.next()
.ok_or_else(|| anyhow::anyhow!("Cannot parse PostgreSQL version"))?
.parse()
.map_err(|_| anyhow::anyhow!("Failed to parse PostgreSQL major version"))?;
if !(12..=18).contains(&major_version) {
anyhow::bail!(
"PostgreSQL version {} not supported, must be between 12 and 18",
major_version
);
}
Ok(version_str)
}
fn select_pg_path(version: &str) -> PathBuf {
let major = version.split('.').next().unwrap_or("17"); // default to 17
PathBuf::from(format!("/usr/lib/postgresql/{}/bin", major))
}
}
#[async_trait]
impl Database for PostgresDatabase {
fn file_extension(&self) -> &'static str {
match self.format {
PostgresDumpFormat::Fc => ".dump",
PostgresDumpFormat::Fd => ".tar.gz",
}
}
async fn ping(&self) -> Result<bool> {
let server_version = PostgresDatabase::get_postgres_server_version(&self.cfg)?;
let pg_path = PostgresDatabase::select_pg_path(&server_version);
let pg_isready_path = format!("{}/pg_isready", pg_path.display());
debug!("Server version: {}", server_version);
debug!("pg_isready_path: {}", pg_isready_path);
let url = format!(
"postgresql://{}:{}@{}:{}/{}",
self.cfg.username, self.cfg.password, self.cfg.host, self.cfg.port, self.cfg.database
);
let status = Command::new(pg_isready_path)
.arg("--dbname")
.arg(url)
.status()
.context("Failed to ping Postgres")?;
Ok(status.success())
}
async fn backup(&self, backup_dir: &Path) -> Result<PathBuf> {
let server_version = PostgresDatabase::get_postgres_server_version(&self.cfg)?;
let pg_path = PostgresDatabase::select_pg_path(&server_version);
let pg_dump_path = format!("{}/pg_dump", pg_path.display());
debug!("Server version: {}", server_version);
debug!("pg_dump_path: {}", pg_dump_path);
match self.format {
PostgresDumpFormat::Fc => {
let file_path = backup_dir.join(format!(
"{}{}",
self.cfg.generated_id,
self.file_extension()
));
let url = format!(
"postgresql://{}:{}@{}:{}/{}",
self.cfg.username,
self.cfg.password,
self.cfg.host,
self.cfg.port,
self.cfg.database
);
let status = Command::new(pg_dump_path)
.arg("--dbname")
.arg(url)
.arg("-Fc")
.arg("-f")
.arg(&file_path)
.arg("-v")
.arg("--compress=3")
.status()?;
if !status.success() {
anyhow::bail!("Postgres backup failed for {}", self.cfg.name);
}
Ok(file_path)
}
PostgresDumpFormat::Fd => {
// directory dump -> tar.gz
let dump_dir = backup_dir.join(format!("{}_dir", self.cfg.generated_id));
let tar_file = backup_dir.join(format!("{}.tar.gz", self.cfg.generated_id));
std::fs::create_dir_all(&dump_dir)?;
let url = format!(
"postgresql://{}:{}@{}:{}/{}",
self.cfg.username,
self.cfg.password,
self.cfg.host,
self.cfg.port,
self.cfg.database
);
let status = Command::new("pg_dump")
.arg("--dbname")
.arg(url)
.arg("-Fd")
.arg("-j")
.arg("4")
.arg("-f")
.arg(&dump_dir)
.arg("-v")
.status()?;
if !status.success() {
anyhow::bail!("Postgres Fd backup failed for {}", self.cfg.name);
}
// Compression tar.gz
let tar_gz = std::fs::File::create(&tar_file)?;
let enc = GzEncoder::new(tar_gz, Compression::default());
let mut tar = tar::Builder::new(enc);
tar.append_dir_all(".", &dump_dir)?;
tar.finish()?;
Ok(tar_file)
}
}
}
async fn restore(&self, restore_file: &Path) -> Result<()> {
let server_version = PostgresDatabase::get_postgres_server_version(&self.cfg)?;
let pg_path = PostgresDatabase::select_pg_path(&server_version);
let pg_restore_path = format!("{}/pg_restore", pg_path.display());
let psql_path = format!("{}/psql", pg_path.display());
debug!("Server version: {}", server_version);
debug!("pg_restore_path: {}", pg_restore_path);
debug!("psql_path: {}", psql_path);
let url = format!(
"postgresql://{}:{}@{}:{}/{}",
self.cfg.username, self.cfg.password, self.cfg.host, self.cfg.port, "postgres"
);
// Terminate connections
let terminate_cmd = format!(
"SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname='{}' AND pid<>pg_backend_pid();",
self.cfg.database
);
Command::new(psql_path)
.arg("-U")
.arg(&self.cfg.username)
.arg("-d")
.arg("postgres")
.arg("-h")
.arg(&self.cfg.host)
.arg("-p")
.arg(self.cfg.port.to_string())
.arg("-c")
.arg(&terminate_cmd)
.env("PGPASSWORD", &self.cfg.password)
.status()?;
match self.format {
PostgresDumpFormat::Fc => {
let status = Command::new(pg_restore_path)
.arg("--no-owner")
.arg("--no-privileges")
.arg("--clean")
.arg("--if-exists")
.arg("--create")
.arg("--dbname")
.arg(url)
.arg("-v")
.arg(restore_file)
.env("PGPASSWORD", &self.cfg.password)
.status()?;
if !status.success() {
anyhow::bail!("Postgres restore failed for {}", self.cfg.name);
}
}
PostgresDumpFormat::Fd => {
let tar_gz = std::fs::File::open(restore_file)?;
let dec = flate2::read::GzDecoder::new(tar_gz);
let mut archive = tar::Archive::new(dec);
let tmp_dir = tempfile::TempDir::new()?;
archive.unpack(tmp_dir.path())?;
let dump_dir = tmp_dir.path();
info!("Restoring dump from {}", dump_dir.display());
let status = Command::new(pg_restore_path)
.arg("--no-owner")
.arg("--no-privileges")
.arg("--clean")
.arg("--if-exists")
.arg("--create")
.arg("--dbname")
.arg(url)
.arg("-v")
.arg(dump_dir)
.env("PGPASSWORD", &self.cfg.password)
.status()?;
if !status.success() {
anyhow::bail!("Postgres Fd restore failed for {}", self.cfg.name);
}
}
}
Ok(())
}
}
+144
View File
@@ -0,0 +1,144 @@
use anyhow::Result;
use tracing::{debug, error, info};
use std::path::PathBuf;
use std::process::Command;
use super::connection::{select_pg_path, server_version};
use super::format::PostgresDumpFormat;
use crate::services::config::DatabaseConfig;
pub async fn run(
cfg: DatabaseConfig,
format: PostgresDumpFormat,
backup_dir: PathBuf,
) -> Result<PathBuf> {
tokio::task::spawn_blocking(move || -> Result<PathBuf> {
debug!("Starting backup for database {}", cfg.name);
let version = match futures::executor::block_on(server_version(&cfg)) {
Ok(v) => {
debug!("Postgres version detected: {}", v);
v
}
Err(e) => {
error!("Failed to get server version for {}: {:?}", cfg.name, e);
return Err(e.into());
}
};
let pg_dump = select_pg_path(&version).join("pg_dump");
debug!("Using pg_dump at {:?}", pg_dump);
match format {
PostgresDumpFormat::Fc => {
info!("Running FC backup for {}", cfg.name);
let file_path = backup_dir.join(format!("{}.dump", cfg.generated_id));
let url = format!(
"postgresql://{}:{}@{}:{}/{}",
cfg.username, cfg.password, cfg.host, cfg.port, cfg.database
);
let status = Command::new(&pg_dump)
.arg("--dbname")
.arg(&url)
.arg("-Fc")
.arg("-f")
.arg(&file_path)
.arg("-v")
.arg("--compress=3")
.status();
match status {
Ok(s) if s.success() => info!(
"FC backup completed successfully for {} at {:?}",
cfg.name, file_path
),
Ok(s) => {
error!("FC backup failed with status {:?} for {}", s, cfg.name);
anyhow::bail!("Postgres backup failed for {}", cfg.name);
}
Err(e) => {
error!("Error executing pg_dump for {}: {:?}", cfg.name, e);
return Err(e.into());
}
}
info!("Backup finished for database {}", cfg.name);
Ok(file_path)
}
PostgresDumpFormat::Fd => {
info!("Running FD backup for {}", cfg.name);
let dump_dir = backup_dir.join(format!("{}_dir", cfg.generated_id));
let tar_file = backup_dir.join(format!("{}.tar.gz", cfg.generated_id));
if let Err(e) = std::fs::create_dir_all(&dump_dir) {
error!(
"Failed to create dump directory {:?} for {}: {:?}",
dump_dir, cfg.name, e
);
return Err(e.into());
}
let url = format!(
"postgresql://{}:{}@{}:{}/{}",
cfg.username, cfg.password, cfg.host, cfg.port, cfg.database
);
let status = Command::new(&pg_dump)
.arg("--dbname")
.arg(&url)
.arg("-Fd")
.arg("-j")
.arg("4")
.arg("-f")
.arg(&dump_dir)
.arg("-v")
.status();
match status {
Ok(s) if s.success() => {
info!("FD backup pg_dump completed successfully for {}", cfg.name)
}
Ok(s) => {
error!(
"FD backup pg_dump failed with status {:?} for {}",
s, cfg.name
);
anyhow::bail!("Postgres FD backup failed for {}", cfg.name);
}
Err(e) => {
error!("Error executing pg_dump for {}: {:?}", cfg.name, e);
return Err(e.into());
}
}
match std::fs::File::create(&tar_file) {
Ok(tar_gz) => {
let enc =
flate2::write::GzEncoder::new(tar_gz, flate2::Compression::default());
let mut tar = tar::Builder::new(enc);
if let Err(e) = tar.append_dir_all(".", &dump_dir) {
error!("Failed to append dump_dir to tar for {}: {:?}", cfg.name, e);
return Err(e.into());
}
if let Err(e) = tar.finish() {
error!("Failed to finish tar archive for {}: {:?}", cfg.name, e);
return Err(e.into());
}
info!("FD backup archive created at {:?}", tar_file);
}
Err(e) => {
error!(
"Failed to create tar.gz file {:?} for {}: {:?}",
tar_file, cfg.name, e
);
return Err(e.into());
}
}
info!("Backup finished for database {}", cfg.name);
Ok(tar_file)
}
}
})
.await?
}
+94
View File
@@ -0,0 +1,94 @@
use std::path::Path;
use crate::domain::postgres::format::PostgresDumpFormat;
use crate::services::config::DatabaseConfig;
use anyhow::Result;
use tokio_postgres::{Client, NoTls};
use tracing::info;
pub async fn connect(cfg: &DatabaseConfig) -> Result<Client> {
let dsn = format!(
"host={} port={} user={} password={} dbname={}",
cfg.host, cfg.port, cfg.username, cfg.password, cfg.database
);
let (client, connection) = tokio_postgres::connect(&dsn, NoTls).await?;
tokio::spawn(async move {
if let Err(e) = connection.await {
tracing::error!("Postgres connection error: {}", e);
}
});
Ok(client)
}
pub async fn server_version(cfg: &DatabaseConfig) -> Result<String> {
let client = connect(cfg).await?;
let version: String = client.query_one("SHOW server_version;", &[]).await?.get(0);
Ok(version)
}
pub fn select_pg_path(version: &str) -> std::path::PathBuf {
let major = version.split('.').next().unwrap_or("17");
format!("/usr/lib/postgresql/{}/bin", major).into()
}
pub async fn terminate_connections(cfg: &DatabaseConfig) -> Result<()> {
let mut admin = cfg.clone();
admin.database = "postgres".into();
let client = connect(&admin).await?;
client
.execute(
r#"
SELECT pg_terminate_backend(pid)
FROM pg_stat_activity
WHERE datname = $1
AND pid <> pg_backend_pid();
"#,
&[&cfg.database],
)
.await?;
Ok(())
}
pub fn detect_format_from_file(restore_file: &Path) -> PostgresDumpFormat {
match restore_file.extension().and_then(|e| e.to_str()) {
Some("dump") => PostgresDumpFormat::Fc,
Some("gz") => PostgresDumpFormat::Fd,
// Some("tar.gz") => PostgresDumpFormat::Fd,
_ => PostgresDumpFormat::Fc,
}
}
pub async fn detect_format_from_size(cfg: &DatabaseConfig) -> PostgresDumpFormat {
info!(
"Detecting database format {:?} - {:?}",
cfg.name, cfg.generated_id
);
let client = match connect(cfg).await {
Ok(c) => c,
Err(_) => return PostgresDumpFormat::Fc,
};
let row = match client
.query_one("SELECT pg_database_size(current_database());", &[])
.await
{
Ok(r) => r,
Err(_) => return PostgresDumpFormat::Fc,
};
let size_bytes: i64 = row.get(0);
info!("Size of database is {} bytes", size_bytes);
// > 1 Go
if size_bytes > 1_000_000_000 {
info!("Using -Fd format");
PostgresDumpFormat::Fd
} else {
info!("Using -Fc format");
PostgresDumpFormat::Fc
}
}
+53
View File
@@ -0,0 +1,53 @@
use anyhow::Result;
use async_trait::async_trait;
use std::path::{Path, PathBuf};
use super::{
backup,
format::PostgresDumpFormat,
ping, restore,
};
use crate::domain::factory::Database;
use crate::services::config::DatabaseConfig;
use crate::utils::locks::{DbOpLock, FileLock};
pub struct PostgresDatabase {
pub cfg: DatabaseConfig,
pub format: PostgresDumpFormat,
}
impl PostgresDatabase {
pub fn new(cfg: DatabaseConfig, format: PostgresDumpFormat) -> Self {
Self { cfg, format }
}
}
#[async_trait]
impl Database for PostgresDatabase {
fn file_extension(&self) -> &'static str {
match self.format {
PostgresDumpFormat::Fc => ".dump",
PostgresDumpFormat::Fd => ".gz",
// PostgresDumpFormat::Fd => ".tar.gz",
}
}
async fn ping(&self) -> Result<bool> {
ping::run(self.cfg.clone()).await
}
async fn backup(&self, dir: &Path) -> Result<PathBuf> {
FileLock::acquire(&self.cfg.generated_id, DbOpLock::Backup.as_str()).await?;
let res = backup::run(self.cfg.clone(), self.format, dir.to_path_buf()).await;
FileLock::release(&self.cfg.generated_id).await?;
res
}
async fn restore(&self, file: &Path) -> Result<()> {
FileLock::acquire(&self.cfg.generated_id, DbOpLock::Restore.as_str()).await?;
let res = restore::run(self.cfg.clone(), self.format, file.to_path_buf()).await;
FileLock::release(&self.cfg.generated_id).await?;
res
}
}
+5
View File
@@ -0,0 +1,5 @@
#[derive(Clone, Copy)]
pub enum PostgresDumpFormat {
Fc,
Fd,
}
+8
View File
@@ -0,0 +1,8 @@
pub mod backup;
pub mod database;
mod restore;
mod connection;
mod format;
mod ping;
pub use connection::{detect_format_from_size, detect_format_from_file};
+8
View File
@@ -0,0 +1,8 @@
use super::connection::connect;
use crate::services::config::DatabaseConfig;
pub async fn run(
cfg: DatabaseConfig,
) -> anyhow::Result<bool> {
Ok(connect(&cfg).await.is_ok())
}
+168
View File
@@ -0,0 +1,168 @@
use anyhow::Result;
use tracing::{debug, error, info};
use std::path::PathBuf;
use std::process::Command;
use super::connection::{select_pg_path, server_version, terminate_connections};
use super::format::PostgresDumpFormat;
use crate::services::config::DatabaseConfig;
pub async fn run(
cfg: DatabaseConfig,
format: PostgresDumpFormat,
restore_file: PathBuf,
) -> Result<()> {
tokio::task::spawn_blocking(move || -> Result<()> {
debug!("Starting restore for database {}", cfg.name);
let version = match futures::executor::block_on(server_version(&cfg)) {
Ok(v) => {
debug!("Postgres version detected: {}", v);
v
}
Err(e) => {
error!("Failed to get server version for {}: {:?}", cfg.name, e);
return Err(e.into());
}
};
let pg_restore = select_pg_path(&version).join("pg_restore");
debug!("Using pg_restore at {:?}", pg_restore);
if let Err(e) = futures::executor::block_on(terminate_connections(&cfg)) {
error!("Failed to terminate connections for {}: {:?}", cfg.name, e);
return Err(e.into());
}
info!("Connections terminated for database {}", cfg.name);
let url = format!(
"postgresql://{}:{}@{}:{}/postgres",
cfg.username, cfg.password, cfg.host, cfg.port
);
debug!("Restore URL: {}", url);
match format {
PostgresDumpFormat::Fc => {
info!("Running FC restore for {}", cfg.name);
let status = Command::new(&pg_restore)
.arg("--no-owner")
.arg("--no-privileges")
.arg("--clean")
.arg("--if-exists")
.arg("--create")
.arg("--dbname")
.arg(&url)
.arg("-v")
.arg(&restore_file)
.env("PGPASSWORD", &cfg.password)
.status();
match status {
Ok(s) if s.success() => {
info!("FC restore completed successfully for {}", cfg.name)
}
Ok(s) => {
error!("FC restore failed with status {:?} for {}", s, cfg.name);
anyhow::bail!("Postgres restore failed for {}", cfg.name);
}
Err(e) => {
error!("Error executing pg_restore for {}: {:?}", cfg.name, e);
return Err(e.into());
}
}
}
PostgresDumpFormat::Fd => {
info!("Running FD restore for {}", cfg.name);
let tar_gz = match std::fs::File::open(&restore_file) {
Ok(f) => f,
Err(e) => {
error!(
"Failed to open restore file {:?} for {}: {:?}",
restore_file, cfg.name, e
);
return Err(e.into());
}
};
let dec = flate2::read::GzDecoder::new(tar_gz);
let mut archive = tar::Archive::new(dec);
let tmp_dir = match tempfile::TempDir::new() {
Ok(d) => d,
Err(e) => {
error!(
"Failed to create temporary directory for FD restore of {}: {:?}",
cfg.name, e
);
return Err(e.into());
}
};
if let Err(e) = archive.unpack(tmp_dir.path()) {
error!("Failed to unpack FD archive for {}: {:?}", cfg.name, e);
return Err(e.into());
}
debug!("Listing contents of temp dir: {}", tmp_dir.path().display());
for entry in std::fs::read_dir(tmp_dir.path())? {
if let Ok(entry) = entry {
let path = entry.path();
let file_type = entry.file_type()?;
debug!(
" - {} | is_dir: {} | is_file: {}",
path.display(),
file_type.is_dir(),
file_type.is_file()
);
}
}
let dump_dir = if tmp_dir.path().join("toc.dat").exists() {
tmp_dir.path().to_path_buf()
} else {
std::fs::read_dir(tmp_dir.path())?
.filter_map(|e| e.ok())
.find(|entry| entry.path().join("toc.dat").exists())
.map(|e| e.path())
.ok_or_else(|| anyhow::anyhow!("Invalid FD archive: toc.dat not found"))?
};
let status = Command::new(&pg_restore)
.arg("--no-owner")
.arg("--no-privileges")
.arg("--clean")
.arg("--if-exists")
.arg("--create")
.arg("--dbname")
.arg(&url)
.arg("-v")
.arg("-j")
.arg("4")
.arg(dump_dir)
.env("PGPASSWORD", &cfg.password)
.status();
match status {
Ok(s) if s.success() => {
info!("FD restore completed successfully for {}", cfg.name)
}
Ok(s) => {
error!("FD restore failed with status {:?} for {}", s, cfg.name);
anyhow::bail!("Postgres FD restore failed for {}", cfg.name);
}
Err(e) => {
error!("Error executing pg_restore for {}: {:?}", cfg.name, e);
return Err(e.into());
}
}
}
}
info!("Restore finished for database {}", cfg.name);
Ok(())
})
.await?
}
+17 -13
View File
@@ -1,24 +1,28 @@
mod core;
mod domain;
mod services;
mod settings;
mod tasks;
mod utils;
mod core;
mod services;
mod domain;
use tracing_subscriber;
use crate::tasks::ping::ping_server;
use crate::utils::locks::FileLock;
use utils::redis_client;
use utils::task_manager::scheduler;
use crate::tasks::ping::ping_server;
use crate::utils::logging;
#[tokio::main]
async fn main() {
tracing_subscriber::fmt::init();
tokio::join!(
ping_server(),
async {
let conn = redis_client::redis_connection().await;
scheduler::scheduler_loop(conn).await;
}
);
logging::init_logger();
// Remove all locks on startup
if let Err(e) = FileLock::clean_startup().await {
eprintln!("Failed to clean locks on startup: {:?}", e);
}
tokio::join!(ping_server(), async {
let conn = redis_client::redis_connection().await;
scheduler::scheduler_loop(conn).await;
});
}
+34 -10
View File
@@ -2,12 +2,12 @@
use crate::core::context::Context;
use crate::domain::factory::DatabaseFactory;
use crate::services::config::{DatabaseConfig, DatabasesConfig};
use crate::services::config::{DatabaseConfig, DatabasesConfig, DbType};
use crate::utils::common::BackupMethod;
use crate::utils::file::full_extension;
use anyhow::Result;
use hex;
use log::{error, info};
use tracing::{error, info};
use openssl::encrypt::Encrypter;
use openssl::hash::MessageDigest;
use openssl::pkey::PKey;
@@ -23,9 +23,10 @@ use tokio::fs;
#[derive(Debug)]
pub struct BackupResult {
pub generated_id: String,
pub db_type: String,
pub db_type: DbType,
pub status: String,
pub backup_file: Option<PathBuf>,
pub code: Option<String>,
}
pub struct BackupService {
@@ -85,6 +86,7 @@ impl BackupService {
db_type,
status: "failed".into(),
backup_file: None,
code: None,
});
}
@@ -94,20 +96,42 @@ impl BackupService {
db_type,
status: "success".into(),
backup_file: Some(file),
code: None,
}),
Err(_) => Ok(BackupResult {
generated_id,
db_type,
status: "failed".into(),
backup_file: None,
}),
Err(e) => match e.to_string().as_str() {
"backup_already_in_progress" => Ok(BackupResult {
generated_id,
db_type,
status: "failed".into(),
backup_file: None,
code: Some(e.to_string()),
}),
_ => Ok(BackupResult {
generated_id,
db_type,
status: "failed".into(),
backup_file: None,
code: None,
}),
},
}
}
pub async fn send_result(&self, result: BackupResult, method: BackupMethod) {
if result.code.as_deref() == Some("backup_already_in_progress") {
info!(
"[BackupService] Skipping send for DB {}: backup already in progress",
result.generated_id
);
return;
}
info!(
"[BackupService] DB: {} Type: {} Status: {} File: {:?}",
result.generated_id, result.db_type, result.status, result.backup_file
result.generated_id,
result.db_type.as_str(),
result.status,
result.backup_file
);
let client = reqwest::Client::new();
+26 -2
View File
@@ -10,13 +10,34 @@ use std::sync::Arc;
use toml;
use tracing::info;
#[derive(Debug, Deserialize, Clone)]
#[serde(rename_all = "lowercase")]
pub enum DbType {
Mysql,
Mariadb,
Postgresql,
// Sqlite,
// Add other DB types if needed
}
impl DbType {
pub fn as_str(&self) -> &'static str {
match self {
DbType::Mysql => "mysql",
DbType::Mariadb => "mysql",
DbType::Postgresql => "postgresql",
// DbType::Sqlite => "sqlite",
}
}
}
#[allow(dead_code)]
#[derive(Debug, Deserialize, Clone)]
pub struct DatabaseConfig {
pub name: String,
pub database: String,
#[serde(rename = "type")]
pub db_type: String,
pub db_type: DbType,
pub username: String,
pub password: String,
pub port: u16,
@@ -55,7 +76,10 @@ impl ConfigService {
let path_obj = Path::new(&path);
if !path_obj.exists() {
return Err(format!("Config file not found: {}", &path));
return Err(format!(
"Config file not found: {}, check documentation and add config file.",
&path
));
}
let extension = path_obj
+3 -1
View File
@@ -5,7 +5,7 @@ use crate::domain::factory::DatabaseFactory;
use crate::services::config::{DatabaseConfig, DatabasesConfig};
use crate::services::status::DatabaseStatus;
use anyhow::Result;
use log::{error, info};
use tracing::{error, info};
use serde::Serialize;
use std::path::Path;
use std::sync::Arc;
@@ -65,6 +65,8 @@ impl RestoreService {
) -> Result<RestoreResult> {
let generated_id = cfg.generated_id.clone();
info!("File url: {}", file_url);
let client = reqwest::Client::new();
let response = client.get(file_url).send().await?;
if !response.status().is_success() {
+1 -1
View File
@@ -86,7 +86,7 @@ impl StatusService {
.iter()
.map(|db| DatabasePayload {
name: &db.name,
dbms: &db.db_type,
dbms: &db.db_type.as_str(),
generated_id: &db.generated_id,
})
.collect();
+14 -1
View File
@@ -11,12 +11,22 @@ pub struct Settings {
pub edge_key: String,
pub databases_config_file: String,
pub data_path: String,
pub pooling: usize,
pub timezone: String,
pub log: String,
}
impl Settings {
fn from_env() -> Self {
dotenv().ok();
let pooling_seconds = env::var("POOLING")
.unwrap_or_else(|_| "5".to_string())
.parse::<usize>()
.expect("POOLING must be a valid positive integer");
let tz = env::var("TZ").unwrap_or_else(|_| "UTC".to_string());
Self {
app_version: env!("CARGO_PKG_VERSION").to_string(),
app_env: env::var("APP_ENV").unwrap_or_else(|_| "development".into()),
@@ -24,8 +34,11 @@ impl Settings {
.unwrap_or_else(|_| "redis://localhost:6379/".into()),
edge_key: env::var("EDGE_KEY").unwrap_or_default(),
databases_config_file: env::var("DATABASES_CONFIG_FILE")
.unwrap_or_else(|_| "config.toml".into()),
.unwrap_or_else(|_| "config.json".into()),
data_path: env::var("DATA_PATH").unwrap_or_else(|_| "/config".into()),
pooling: pooling_seconds,
timezone: tz,
log: env::var("LOG").unwrap_or_else(|_| "info".into()),
}
}
}
+28 -1
View File
@@ -1,5 +1,6 @@
use crate::core::agent::Agent;
use crate::core::context::Context;
use crate::settings::CONFIG;
use crate::utils::common::BackupMethod;
use std::sync::Arc;
use std::time::Duration;
@@ -19,6 +20,32 @@ pub async fn ping_server() {
);
}
tokio::time::sleep(Duration::from_secs(60)).await;
tokio::time::sleep(Duration::from_secs(CONFIG.pooling as u64)).await;
}
}
// use crate::core::agent::Agent;
// use crate::core::context::Context;
// use crate::utils::common::BackupMethod;
// use std::sync::Arc;
// use tokio::sync::Mutex;
// use tokio::time::{sleep, Duration};
// use tracing::{error};
//
// pub async fn ping_server() {
//
//
// loop {
// let ctx = Arc::new(Context::new());
// let agent = Arc::new(Mutex::new(Agent::new(ctx.clone()).await));
// let agent_clone = agent.clone();
//
// tokio::spawn(async move {
// let mut agent_locked = agent_clone.lock().await;
// if let Err(e) = agent_locked.run(BackupMethod::Manual).await {
// error!("An error occurred while executing ping_server: {:?}", e);
// }
// });
//
// sleep(Duration::from_secs(5)).await;
// }
// }
+112
View File
@@ -0,0 +1,112 @@
use anyhow::{Context, Result};
use chrono::Utc;
use tracing::{info, warn, error};
use std::path::{Path, PathBuf};
use std::time::{Duration, SystemTime};
use tokio::fs::{OpenOptions, metadata, remove_file, create_dir_all, read_dir};
use tokio::io::AsyncWriteExt;
/// Lock type for logging purposes
#[derive(Debug, Copy, Clone)]
pub enum DbOpLock {
Backup,
Restore,
}
impl DbOpLock {
/// Convert enum to a static string for service name
pub fn as_str(&self) -> &'static str {
match self {
DbOpLock::Backup => "backup-service",
DbOpLock::Restore => "restore-service",
}
}
}
/// File-based lock utility
pub struct FileLock;
impl FileLock {
const LOCK_DIR: &'static str = "/var/locks";
/// Returns the path for the lock file
fn lock_file_path(id: &str) -> PathBuf {
Path::new(Self::LOCK_DIR).join(format!("{}.lock", id))
}
/// Ensure the locks directory exists
async fn ensure_lock_dir() -> Result<()> {
create_dir_all(Self::LOCK_DIR)
.await
.with_context(|| format!("Failed to create locks directory {}", Self::LOCK_DIR))?;
Ok(())
}
/// Clean all lock files on startup (remove everything, ignore stale)
pub async fn clean_startup() -> Result<()> {
Self::ensure_lock_dir().await?;
let mut dir = read_dir(Self::LOCK_DIR).await?;
while let Some(entry) = dir.next_entry().await? {
let path = entry.path();
if path.is_file() && path.extension().map(|e| e == "lock").unwrap_or(false) {
if let Err(e) = remove_file(&path).await {
warn!("Failed to remove lock file {:?}: {:?}", path, e);
} else {
info!("Removed lock file {:?}", path);
}
}
}
Ok(())
}
/// Acquire a file-based lock
pub async fn acquire(id: &str, service_name: &str) -> Result<()> {
Self::ensure_lock_dir().await?;
let path = Self::lock_file_path(id);
info!("Attempting to acquire lock for {} at {:?}", id, path);
if path.exists() {
let meta = metadata(&path).await?;
if let Ok(modified) = meta.modified() {
let age = SystemTime::now().duration_since(modified)?;
if age > Duration::from_secs(24 * 60 * 60) {
remove_file(&path).await?;
warn!("Removed stale lock for {}", id);
} else {
error!("Lock already held for {}. Cannot acquire.", id);
anyhow::bail!("backup_already_in_progress");
}
}
}
let mut f = OpenOptions::new()
.write(true)
.create_new(true)
.open(&path)
.await
.with_context(|| format!("Failed to create lock file for {}", id))?;
f.write_all(format!("Service: {}\n", service_name).as_bytes()).await?;
f.write_all(format!("PID: {}\n", std::process::id()).as_bytes()).await?;
f.write_all(format!("Timestamp: {}\n", Utc::now()).as_bytes()).await?;
info!("Successfully acquired lock for {}", id);
Ok(())
}
/// Release the file-based lock
pub async fn release(id: &str) -> Result<()> {
let path = Self::lock_file_path(id);
info!("Releasing lock for {}", id);
if path.exists() {
remove_file(&path).await?;
info!("Released file lock for {}", id);
} else {
warn!("Attempted to release lock for {}, but file does not exist", id);
}
Ok(())
}
}
+52
View File
@@ -0,0 +1,52 @@
use crate::settings::CONFIG;
use once_cell::sync::Lazy;
use time::macros::format_description;
use tracing_appender::non_blocking::{NonBlocking, WorkerGuard};
use tracing_appender::rolling::{RollingFileAppender, Rotation};
use tracing_subscriber::fmt::time::LocalTime;
use tracing_subscriber::{EnvFilter, Registry, fmt, prelude::*};
const LOGS_DIRECTORY: &str = "/var/log/app";
static FILE_APPENDER: Lazy<(NonBlocking, WorkerGuard)> = Lazy::new(|| {
let file_appender = RollingFileAppender::new(Rotation::DAILY, LOGS_DIRECTORY, "app.log");
tracing_appender::non_blocking(file_appender)
});
pub fn init_logger() {
std::fs::create_dir_all(LOGS_DIRECTORY).ok();
let (writer, _guard) = &*FILE_APPENDER;
let timer = LocalTime::new(format_description!(
"[year]-[month]-[day]T[hour]:[minute]:[second]"
));
let file_layer = fmt::layer()
.with_writer(writer.clone())
.with_timer(timer.clone())
.with_ansi(false)
.with_target(false);
// let env_filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info"));
let env_filter = EnvFilter::new(CONFIG.log.clone());
let term_layer = fmt::layer()
.with_writer(std::io::stdout)
.with_timer(timer.clone())
// .with_timer(tracing_subscriber::fmt::time::LocalTime::rfc_3339())
.with_ansi(true)
.with_target(false)
.with_filter(env_filter);
let subscriber = Registry::default().with(file_layer).with(term_layer);
tracing::subscriber::set_global_default(subscriber).expect("Failed to set global subscriber");
tracing::info!(
"Logger initialized | TZ={} | POOLING={}s",
CONFIG.timezone,
CONFIG.pooling
);
}
+3 -1
View File
@@ -3,4 +3,6 @@ pub mod edge_key;
pub mod redis_client;
pub mod task_manager;
pub mod text;
pub mod file;
pub mod file;
pub mod locks;
pub mod logging;
-10
View File
@@ -1,13 +1,3 @@
// use redis::{Client, Connection};
// use crate::settings::CONFIG;
//
// pub fn redis_connection() -> Connection {
// let client = Client::open(CONFIG.redis_url.clone()).expect("Invalid Redis URL");
// client.get_connection().expect("Failed to connect to Redis")
//
// }
use redis::{aio::MultiplexedConnection, Client};
use crate::settings::CONFIG;
+2 -69
View File
@@ -1,78 +1,11 @@
// use crate::utils::task_manager::models;
// use crate::utils::task_manager::tasks::{remove_task, upsert_task};
// use crate::utils::text::normalize_cron;
// use chrono::Utc;
// use cron::Schedule;
// use log::debug;
// use redis::Commands;
// use std::str::FromStr;
// use tracing::info;
//
// pub fn next_run_timestamp(expr: &str) -> i64 {
// let schedule = Schedule::from_str(expr).unwrap();
// schedule.upcoming(Utc).next().unwrap().timestamp()
// }
//
// pub fn check_and_update_cron(
// conn: &mut redis::aio::MultiplexedConnection,
// cron_value: Option<String>,
// args: Vec<String>,
// task: &str,
// task_name: String,
// ) {
// let redis_key = format!("redbeat:{}", task_name);
//
// let exists: bool = conn.exists(&redis_key).unwrap();
//
// match cron_value {
// None => {
// if exists {
// remove_task(conn, &task_name).unwrap();
// info!("Task {} removed", task_name);
// }
// }
//
// Some(cron) => {
// let cron = normalize_cron(&cron);
// debug!("Task cron (normalized): {:?}", cron);
//
// if exists {
// let raw: String = conn.hget(&redis_key, "data").unwrap();
// let stored: models::PeriodicTask = serde_json::from_str(&raw).unwrap();
//
// if stored.cron != cron {
// upsert_task(
// conn,
// &task_name,
// task,
// &cron,
// args,
// )
// .unwrap();
// info!("Task {} updated", task_name);
// }
// } else {
// upsert_task(
// conn,
// &task_name,
// task,
// &cron,
// args,
// )
// .unwrap();
// info!("Task {} created", task_name);
// }
// }
// }
// }
use crate::utils::task_manager::models;
use crate::utils::task_manager::tasks::{remove_task, upsert_task};
use crate::utils::text::normalize_cron;
use chrono::Utc;
use cron::Schedule;
use log::debug;
use redis::aio::MultiplexedConnection;
use tracing::debug;
use redis::AsyncCommands;
use redis::aio::MultiplexedConnection;
use std::str::FromStr;
use tracing::info;
+10 -2
View File
@@ -5,10 +5,11 @@ use crate::utils::common::BackupMethod;
use crate::utils::task_manager::cron::next_run_timestamp;
use crate::utils::task_manager::models::PeriodicTask;
use crate::utils::task_manager::tasks::SCHEDULE_KEY;
use log::info;
use tracing::info;
use redis::AsyncCommands;
use redis::aio::MultiplexedConnection;
use std::sync::Arc;
use tracing::error;
pub async fn scheduler_loop(mut conn: MultiplexedConnection) {
loop {
@@ -36,7 +37,14 @@ pub async fn scheduler_loop(mut conn: MultiplexedConnection) {
task_clone.task, task_clone.args
);
let _ = execute_task(task_clone.task.as_str(), task_clone.args).await;
// let _ = execute_task(task_clone.task.as_str(), task_clone.args).await;
if let Err(e) = execute_task(task_clone.task.as_str(), task_clone.args).await {
error!(
"An error occurred while executing task={} : {:?}",
task_clone.task, e
);
}
let next_ts = next_run_timestamp(&task_clone.cron);
let _: () = conn_clone.zadd(SCHEDULE_KEY, &key, next_ts).await.unwrap();
-55
View File
@@ -1,60 +1,5 @@
#![allow(dead_code)]
// use redis::{Connection};
// use redis::aio::MultiplexedConnection;
// use crate::utils::task_manager::cron::next_run_timestamp;
// use crate::utils::task_manager::models::PeriodicTask;
//
// pub const SCHEDULE_KEY: &str = "redbeat:schedule";
//
// pub fn upsert_task(
// conn: &mut MultiplexedConnection,
// name: &str,
// task: &str,
// cron: &str,
// args: Vec<String>,
// ) -> redis::RedisResult<()> {
//
// let key = format!("redbeat:{}", name);
// let next_ts = next_run_timestamp(cron);
//
// let entry = PeriodicTask {
// task: task.to_string(),
// cron: cron.to_string(),
// args,
// enabled: true,
// };
//
// let payload = serde_json::to_string(&entry).unwrap();
//
// redis::pipe()
// .atomic()
// .cmd("HSET")
// .arg(&key)
// .arg("data")
// .arg(payload)
// .cmd("ZADD")
// .arg(SCHEDULE_KEY)
// .arg(next_ts)
// .arg(&key)
// .query(conn)
// }
//
// pub fn remove_task(
// conn: &mut MultiplexedConnection,
// name: &str,
// ) -> redis::RedisResult<()> {
// let key = format!("redbeat:{}", name);
//
// redis::pipe()
// .atomic()
// .cmd("ZREM")
// .arg(SCHEDULE_KEY)
// .arg(&key)
// .cmd("DEL")
// .arg(&key)
// .query(conn)
// }
use crate::utils::task_manager::cron::next_run_timestamp;
use crate::utils::task_manager::models::PeriodicTask;
use redis::aio::MultiplexedConnection;