homebrew-core/Formula/borgmatic.rb

175 lines
6.5 KiB
Ruby

class Borgmatic < Formula
include Language::Python::Virtualenv
desc "Simple wrapper script for the Borg backup software"
homepage "https://torsion.org/borgmatic/"
url "https://files.pythonhosted.org/packages/ff/6a/436b4b3a3f38f62261bf4a7e106c01096f36092a968880ee91686206b60e/borgmatic-1.7.4.tar.gz"
sha256 "b1b1e46252e12f42c17a0984c40ace10fc22d52806f0d687f2d0a1a99869a007"
license "GPL-3.0-or-later"
bottle do
sha256 cellar: :any_skip_relocation, arm64_monterey: "e21a307f3639f4875222a848664810d2cdf49f82f55c52421e5d4792dffd751a"
sha256 cellar: :any_skip_relocation, arm64_big_sur: "8afb204fc44b64572327a06b9a47a526676ba8248dac484dbf2226152eb09709"
sha256 cellar: :any_skip_relocation, monterey: "9a956b54656b34375be6e0e737ba4695a188682996f94b93fc8511b803d625d9"
sha256 cellar: :any_skip_relocation, big_sur: "4b06ccf79d5e92a42baaca245b62d672175651d796af648a79e26b269a17d1a3"
sha256 cellar: :any_skip_relocation, catalina: "406c0860868d2661472a970010febf1d62ad25745d88eb70fa89667bba779376"
sha256 cellar: :any_skip_relocation, x86_64_linux: "8c57fad50a243b203a6116ce6e2a413f1ba6bb433aa3e9b6bbef73b4c6e602bd"
end
depends_on "jsonschema"
depends_on "python@3.10"
resource "certifi" do
url "https://files.pythonhosted.org/packages/cb/a4/7de7cd59e429bd0ee6521ba58a75adaec136d32f91a761b28a11d8088d44/certifi-2022.9.24.tar.gz"
sha256 "0d9c601124e5a6ba9712dbc60d9c53c21e34f5f641fe83002317394311bdce14"
end
resource "charset-normalizer" do
url "https://files.pythonhosted.org/packages/a1/34/44964211e5410b051e4b8d2869c470ae8a68ae274953b1c7de6d98bbcf94/charset-normalizer-2.1.1.tar.gz"
sha256 "5a3d016c7c547f69d6f81fb0db9449ce888b418b5b9952cc5e6e66843e9dd845"
end
resource "colorama" do
url "https://files.pythonhosted.org/packages/2b/65/24d033a9325ce42ccbfa3ca2d0866c7e89cc68e5b9d92ecaba9feef631df/colorama-0.4.5.tar.gz"
sha256 "e6c6b4334fc50988a639d9b98aa429a0b57da6e17b9a44f0451f930b6967b7a4"
end
resource "idna" do
url "https://files.pythonhosted.org/packages/8b/e1/43beb3d38dba6cb420cefa297822eac205a277ab43e5ba5d5c46faf96438/idna-3.4.tar.gz"
sha256 "814f528e8dead7d329833b91c5faa87d60bf71824cd12a7530b5526063d02cb4"
end
resource "requests" do
url "https://files.pythonhosted.org/packages/a5/61/a867851fd5ab77277495a8709ddda0861b28163c4613b011bc00228cc724/requests-2.28.1.tar.gz"
sha256 "7c5599b102feddaa661c826c56ab4fee28bfd17f5abca1ebbe3e7f19d7c97983"
end
resource "ruamel.yaml" do
url "https://files.pythonhosted.org/packages/46/a9/6ed24832095b692a8cecc323230ce2ec3480015fbfa4b79941bd41b23a3c/ruamel.yaml-0.17.21.tar.gz"
sha256 "8b7ce697a2f212752a35c1ac414471dc16c424c9573be4926b56ff3f5d23b7af"
end
resource "ruamel.yaml.clib" do
url "https://files.pythonhosted.org/packages/8b/25/08e5ad2431a028d0723ca5540b3af6a32f58f25e83c6dda4d0fcef7288a3/ruamel.yaml.clib-0.2.6.tar.gz"
sha256 "4ff604ce439abb20794f05613c374759ce10e3595d1867764dd1ae675b85acbd"
end
resource "urllib3" do
url "https://files.pythonhosted.org/packages/b2/56/d87d6d3c4121c0bcec116919350ca05dc3afd2eeb7dc88d07e8083f8ea94/urllib3-1.26.12.tar.gz"
sha256 "3fa96cf423e6987997fc326ae8df396db2a8b7c667747d47ddd8ecba91f4a74e"
end
def install
virtualenv_install_with_resources
# we depend on jsonschema, but that's a separate formula, so install a `.pth` file to link them
site_packages = Language::Python.site_packages("python3.10")
jsonschema = Formula["jsonschema"].opt_libexec
(libexec/site_packages/"homebrew-jsonschema.pth").write jsonschema/site_packages
end
test do
borg = (testpath/"borg")
borg_info_json = (testpath/"borg_info_json")
config_path = testpath/"config.yml"
repo_path = testpath/"repo"
log_path = testpath/"borg.log"
sentinel_path = testpath/"init_done"
# Create a fake borg info json
borg_info_json.write <<~EOS
{
"cache": {
"path": "",
"stats": {
"total_chunks": 0,
"total_csize": 0,
"total_size": 0,
"total_unique_chunks": 0,
"unique_csize": 0,
"unique_size": 0
}
},
"encryption": {
"mode": "repokey-blake2"
},
"repository": {
"id": "0000000000000000000000000000000000000000000000000000000000000000",
"last_modified": "2022-01-01T00:00:00.000000",
"location": "#{repo_path}"
},
"security_dir": ""
}
EOS
# Create a fake borg executable to log requested commands
borg.write <<~EOS
#!/bin/sh
echo $@ >> #{log_path}
# return valid borg version
if [ "$1" = "--version" ]; then
echo "borg 1.2.0"
exit 0
fi
# for first invocation only, return an error so init is called
if [ "$1" = "info" ]; then
if [ -f #{sentinel_path} ]; then
# return fake repository info
cat #{borg_info_json}
exit 0
else
touch #{sentinel_path}
exit 2
fi
fi
# skip actual backup creation
if [ "$1" = "create" ]; then
exit 0
fi
EOS
borg.chmod 0755
# Generate a config
system bin/"generate-borgmatic-config", "--destination", config_path
# Replace defaults values
inreplace config_path do |s|
s.gsub!(/# ?local_path: borg1/, "local_path: #{borg}")
s.gsub! "- ssh://user@backupserver/./sourcehostname.borg", "- #{repo_path}"
s.gsub! "- ssh://user@backupserver/./{fqdn}", ""
s.gsub! "- /var/local/backups/local.borg", ""
s.gsub! "- /var/log/syslog*", ""
s.gsub! "- /home/user/path with spaces", ""
end
# Initialize Repo
system bin/"borgmatic", "-v", "2", "--config", config_path, "init", "--encryption", "repokey"
# Create a backup
system bin/"borgmatic", "--config", config_path
# See if backup was created
system bin/"borgmatic", "--config", config_path, "--list", "--json"
# Read in stored log
log_content = File.read(log_path)
# Assert that the proper borg commands were executed
assert_match <<~EOS, log_content
--version --debug --show-rc
info --json #{repo_path}
init --encryption repokey --debug #{repo_path}
--version
prune --keep-daily 7 --glob-archives {hostname}-* #{repo_path}
compact #{repo_path}
create #{repo_path}::{hostname}-{now:%Y-%m-%dT%H:%M:%S.%f} /etc /home
info --json #{repo_path}
check --glob-archives {hostname}-* #{repo_path}
--version
EOS
end
end