From f1c4a08ae3f560a67cbbc87ecfc1380d3d5e1072 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Fri, 25 Nov 2016 20:21:58 +0000 Subject: [PATCH] add new commands --- backup-container-borg | 109 ++++++++++++++++++++++++++++++++++++++++++ decrypt-zfs | 2 + lxc-gem2arch | 11 +++++ lxc-yaourt | 4 +- package-add | 7 ++- sendmail | 1 + update-motd | 2 +- vim | 1 + 8 files changed, 132 insertions(+), 5 deletions(-) create mode 100755 backup-container-borg create mode 100755 lxc-gem2arch create mode 120000 sendmail create mode 120000 vim diff --git a/backup-container-borg b/backup-container-borg new file mode 100755 index 0000000..4846131 --- /dev/null +++ b/backup-container-borg @@ -0,0 +1,109 @@ +#!/usr/bin/env ruby +require 'json' +require 'pathname' +require 'fileutils' +require 'open3' + +LXC_PATH = Pathname.new("/data/containers") +BACKUP_LOCATIONS = %w{home srv etc usr/local} +CONFIG_PATH = "/etc/lxc/container.json" +BACKUP_PATH = "/mnt/backup/borg" +BORG_PATH = Pathname.new("/data/borg") +PASSWORD_FILE = BORG_PATH.join("passwordfile").to_s +KEEP_DAILY = 7 +KEEP_WEEKLY = 4 +KEEP_MONTHLY = 0 + +def load_config + return JSON.load(File.open(CONFIG_PATH)) +rescue SystemCallError => e + abort "failed to open configuration '#{CONFIG_PATH}', #{e}" +rescue JSON::ParserError => e + abort "failed to parse configuration '#{CONFIG_PATH}', #{e}" +end + +def sh(cmd, env={}, *args) + pretty_args = args.map {|arg| "'#{arg}'"} + puts ([cmd] + pretty_args).join(" ") + system(env, cmd, *args) +end + +class Container + def initialize(name, backup_paths, backup_scripts) + @name = name + @backup_paths = backup_paths + @backup_scripts = backup_scripts + @path = LXC_PATH.join(name, "rootfs") + end + def backup_paths + paths = BACKUP_LOCATIONS + if @backup_paths.is_a?(Array) + paths += @backup_paths + end + paths.map do |relative_path| + @path.join(relative_path) + end + end + def run_backup_scripts + if @backup_scripts.is_a?(Array) + @backup_scripts.map do |script| + backup_script(script) + end + else + [] + end + end + + private + def backup_script(script) + unless script.is_a?(Hash) + abort("backup-scripts: Expected an Object, got #{script.class}") + end + command = script["command"] + if command.nil? + abort("command not set for backup-scripts for container '#{@name}'") + end + backupname = script["backupname"] + if backupname.nil? + abort("backupname not set for backup-scripts for container '#{@name}'") + end + backupname = BORG_PATH.join(backupname.gsub("/", "")) + FileUtils.mkdir_p(backupname) + puts "cd #{backupname}" + Dir.chdir(backupname) do + sh(command) + end + backupname + end + + def empty_directory?(path) + return false unless Dir.exists?(path) + return Dir.entries(path).size <= 2 # - [".", ".."] + end +end + +config = load_config +backup_paths = BACKUP_LOCATIONS.map do |location| + "/#{location}" +end +config["network"].each do |container, data| + next if data["lxc"] == false + container = Container.new(container, data["backup-paths"], data["backup-scripts"]) + backup_paths += container.backup_paths + backup_paths += container.run_backup_scripts +end + +env = { "BORG_PASSPHRASE" => File.read(PASSWORD_FILE).chomp } +now = Time.now.strftime("%Y-%m-%d-%H:%M:%S") +paths = backup_paths.map {|path| path.to_s } +sh("borg", env, "create", "--stats", "#{BACKUP_PATH}::eve-#{now}", + '--compression', 'zlib,9', + '--exclude', '*/srv/repo', + '--exclude', '*/srv/deluge', + '--exclude', '*/home/joerg/git', + '--exclude', '*/home/joerg/login/git', + *paths) +sh("borg", env, "prune", "-v", BACKUP_PATH, + "--keep-daily", KEEP_DAILY.to_s, + "--keep-weekly", KEEP_WEEKLY.to_s, + "--keep-monthly", KEEP_MONTHLY.to_s) diff --git a/decrypt-zfs b/decrypt-zfs index 1df9d13..dba83be 100755 --- a/decrypt-zfs +++ b/decrypt-zfs @@ -34,3 +34,5 @@ mount -o bind /data/containers/login/rootfs/home/devkid /home/devkid/login mount -o bind /data/containers/pyload/rootfs/var/lib/pyload /data/pyload mount -o bind /data/pacman/pkg /var/cache/pacman/pkg mount -o bind /data/pacman/sync /var/lib/pacman/sync + +systemctl start lxc.target diff --git a/lxc-gem2arch b/lxc-gem2arch new file mode 100755 index 0000000..4e953f2 --- /dev/null +++ b/lxc-gem2arch @@ -0,0 +1,11 @@ +#!/bin/bash + +if [ $EUID -ne 0 ]; then + echo "Must be root!" >&2 + exit 1 +fi + +cd "${DIR:-/tmp}" +lxc-attach --name login -- sudo -u aurrepo -- aurrepo --sign --verbose +lxc-attach --name login -- sudo -u aurrepo -- gem2arch "$@" +lxc-attach --name login -- sudo -u aurrepo -- aurrepo --sign --verbose diff --git a/lxc-yaourt b/lxc-yaourt index a5389c7..cb2efe1 100755 --- a/lxc-yaourt +++ b/lxc-yaourt @@ -6,7 +6,7 @@ if [ $EUID -ne 0 ]; then fi cd "${DIR:-/}" -lxc-attach --name login -- sudo -u aurrepo -- aurrepo --sign --verbose +lxc-attach --name login -- sudo -u aurrepo -- aurrepo --sign --verbose lxc-attach --name login -- sudo -u aurrepo -- yaourt "$@" -lxc-attach --name login -- sudo -u aurrepo -- aurrepo --sign --verbose +lxc-attach --name login -- sudo -u aurrepo -- aurrepo --sign --verbose lxc-attach --name login -- pacman -Sy diff --git a/package-add b/package-add index a703bd3..a3a7675 100755 --- a/package-add +++ b/package-add @@ -4,11 +4,14 @@ if [[ "$#" < 1 ]]; then echo "USAGE: $0 packages..." fi +pkgs="" for pkg in "$@" do cp -- "$pkg" "/data/containers/login/rootfs/var/lib/aurrepo/" name="$(basename $pkg)" lxc-attach -n login -- sudo -u aurrepo gpg --detach-sign --no-armor "/var/lib/aurrepo/$name" - lxc-attach -n login -- pacman -U "/var/lib/aurrepo/$name" - lxc-attach -n login -- sudo -u aurrepo aurrepo --sign --verbose + pkgs="$pkgs /var/lib/aurrepo/$name" done + +lxc-attach -n login -- pacman -U $pkgs +lxc-attach -n login -- sudo -u aurrepo aurrepo --sign --verbose diff --git a/sendmail b/sendmail new file mode 120000 index 0000000..fc37e58 --- /dev/null +++ b/sendmail @@ -0,0 +1 @@ +/usr/bin/msmtp \ No newline at end of file diff --git a/update-motd b/update-motd index 9c56dc2..f2abcd6 100755 --- a/update-motd +++ b/update-motd @@ -8,7 +8,7 @@ motd="/etc/motd" # $USER is automatically defined HOSTNAME=`uname -n` KERNEL=`uname -rm` -INSTALLED_KERNEL=$(pacman -Qi linux-lts | awk '/Version/ { print $3 }') +INSTALLED_KERNEL=$(pacman -Qi linux | awk '/Version/ { print $3 }') CPU=`awk -F '[ :][ :]+' '/^model name/ { print $2; exit; }' /proc/cpuinfo` CPU_TEMP=$(($(