Add backup script
Signed-off-by: Nikolaos Karaolidis <nick@karaolidis.com>
This commit is contained in:
@@ -96,6 +96,6 @@
|
|||||||
python = import ./hosts/common/shells/python { inherit pkgs; };
|
python = import ./hosts/common/shells/python { inherit pkgs; };
|
||||||
};
|
};
|
||||||
|
|
||||||
formatter = pkgs.nixfmt-rfc-style;
|
formatter."x86_64-linux" = pkgs.nixfmt-rfc-style;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
16
hosts/common/configs/system/backup/backup.completion.zsh
Normal file
16
hosts/common/configs/system/backup/backup.completion.zsh
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
_backup_completion() {
|
||||||
|
local options=(
|
||||||
|
'-m[specify partition to mount for backup]:partition:($(_partitions))'
|
||||||
|
'-b[specify backup directory]:backup directory:_files -/'
|
||||||
|
)
|
||||||
|
local curcontext="$curcontext" state line
|
||||||
|
typeset -A opt_args
|
||||||
|
|
||||||
|
_partitions() {
|
||||||
|
lsblk -rno NAME | sed 's/^/\/dev\//'
|
||||||
|
}
|
||||||
|
|
||||||
|
_arguments -s $options
|
||||||
|
}
|
||||||
|
|
||||||
|
compdef _backup_completion backup
|
64
hosts/common/configs/system/backup/backup.sh
Normal file
64
hosts/common/configs/system/backup/backup.sh
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
if [[ "${EUID}" -ne 0 ]]; then
|
||||||
|
echo "Please run the script as root."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
usage() {
|
||||||
|
echo "Usage: $0 [-m partition] [-b backup_location]"
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
|
||||||
|
cleanup() {
|
||||||
|
if [ -d "/persist.bak" ]; then btrfs -q subvolume delete "/persist.bak"; fi
|
||||||
|
if [ -n "${backup_location}" ] && [ -f "${backup_location}.tmp" ]; then rm "${backup_location}.tmp"; fi
|
||||||
|
|
||||||
|
if [ -n "${mount_location}" ]; then
|
||||||
|
if mount | grep -q "${mount_location}"; then umount "${mount_location}"; fi
|
||||||
|
if [ -d "${mount_location}" ]; then rmdir "${mount_location}"; fi
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
partition=""
|
||||||
|
backup_location=""
|
||||||
|
mount_location=""
|
||||||
|
|
||||||
|
trap cleanup EXIT
|
||||||
|
|
||||||
|
while getopts "m:b:" opt; do
|
||||||
|
case "${opt}" in
|
||||||
|
m) partition="${OPTARG}" ;;
|
||||||
|
b) backup_location="${OPTARG}" ;;
|
||||||
|
*) usage ;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
if [ -n "${partition}" ]; then
|
||||||
|
mount_location=$(mktemp -d /mnt/backup.XXXXXX)
|
||||||
|
echo "Mounting ${partition} at ${mount_location}..."
|
||||||
|
mount "${partition}" "${mount_location}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -z "${mount_location}" ]; then
|
||||||
|
if [[ "${backup_location}" != /* ]]; then
|
||||||
|
backup_location="$(realpath "${backup_location}")"
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
if [[ "${backup_location}" = /* ]]; then
|
||||||
|
echo "Error: When a partition is mounted, backup_location must be relative."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
backup_location="$(realpath "${mount_location}/${backup_location}")"
|
||||||
|
fi
|
||||||
|
|
||||||
|
backup_location="${backup_location}/$(hostname)-$(date +%Y-%m-%d-%H-%M-%S).btrfs.gz"
|
||||||
|
|
||||||
|
echo "Creating /persist snapshot..."
|
||||||
|
btrfs -q subvolume snapshot -r "/persist" "/persist.bak"
|
||||||
|
|
||||||
|
echo "Creating backup at ${backup_location}..."
|
||||||
|
btrfs -q send "/persist.bak" | gzip > "${backup_location}.tmp"
|
||||||
|
|
||||||
|
mv "${backup_location}.tmp" "${backup_location}"
|
||||||
|
|
||||||
|
echo "Backup completed successfully!"
|
20
hosts/common/configs/system/backup/default.nix
Normal file
20
hosts/common/configs/system/backup/default.nix
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
{ pkgs, ... }:
|
||||||
|
{
|
||||||
|
environment.systemPackages = [
|
||||||
|
(pkgs.writeShellApplication {
|
||||||
|
name = "backup";
|
||||||
|
runtimeInputs = with pkgs; [
|
||||||
|
btrfs-progs
|
||||||
|
coreutils-full
|
||||||
|
util-linux
|
||||||
|
];
|
||||||
|
text = builtins.readFile ./backup.sh;
|
||||||
|
})
|
||||||
|
];
|
||||||
|
|
||||||
|
home-manager.sharedModules = [
|
||||||
|
{
|
||||||
|
programs.zsh.initExtra = builtins.readFile ./backup.completion.zsh;
|
||||||
|
}
|
||||||
|
];
|
||||||
|
}
|
@@ -27,13 +27,13 @@
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
_kubeswitch-completion() {
|
_kubeswitch_completion() {
|
||||||
local dir="$HOME/.kube"
|
local dir="$HOME/.kube"
|
||||||
local config="$dir/config"
|
local config="$dir/config"
|
||||||
compadd -- ''${(f)"$(find "$dir" -maxdepth 1 \( -type f -o -type l \) -not -name 'config' -exec basename {} \;)"}
|
compadd -- ''${(f)"$(find "$dir" -maxdepth 1 \( -type f -o -type l \) -not -name 'config' -exec basename {} \;)"}
|
||||||
}
|
}
|
||||||
|
|
||||||
compdef _kubeswitch-completion kubeswitch
|
compdef _kubeswitch_completion kubeswitch
|
||||||
'';
|
'';
|
||||||
|
|
||||||
p10k.extraRightPromptElements = [ "kubecontext" ];
|
p10k.extraRightPromptElements = [ "kubecontext" ];
|
||||||
|
@@ -7,9 +7,13 @@
|
|||||||
home-manager.users.${user}.programs.zsh = {
|
home-manager.users.${user}.programs.zsh = {
|
||||||
shellAliases.nd = "nix-develop";
|
shellAliases.nd = "nix-develop";
|
||||||
|
|
||||||
initExtra = let
|
initExtra =
|
||||||
devShells = lib.strings.concatStringsSep " " (lib.attrsets.mapAttrsToList (key: _: key) inputs.self.devShells);
|
let
|
||||||
in ''
|
devShells = lib.strings.concatStringsSep " " (
|
||||||
|
lib.attrsets.mapAttrsToList (key: _: key) inputs.self.devShells
|
||||||
|
);
|
||||||
|
in
|
||||||
|
''
|
||||||
nix-develop() {
|
nix-develop() {
|
||||||
if [ -z "$1" ]; then
|
if [ -z "$1" ]; then
|
||||||
echo "Usage: nix-develop <shell>"
|
echo "Usage: nix-develop <shell>"
|
||||||
@@ -18,12 +22,12 @@
|
|||||||
nix develop self#"$1" -c "$SHELL"
|
nix develop self#"$1" -c "$SHELL"
|
||||||
}
|
}
|
||||||
|
|
||||||
_nix-develop-completion() {
|
_nix-develop_completion() {
|
||||||
local shells=(${devShells})
|
local shells=(${devShells})
|
||||||
compadd -- $shells
|
compadd -- $shells
|
||||||
}
|
}
|
||||||
|
|
||||||
compdef _nix-develop-completion nix-develop
|
compdef _nix-develop_completion nix-develop
|
||||||
'';
|
'';
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@@ -16,9 +16,13 @@
|
|||||||
zsh = {
|
zsh = {
|
||||||
shellAliases.nde = "nix-direnv";
|
shellAliases.nde = "nix-direnv";
|
||||||
|
|
||||||
initExtra = let
|
initExtra =
|
||||||
devShells = lib.strings.concatStringsSep " " (lib.attrsets.mapAttrsToList (key: _: key) inputs.self.devShells);
|
let
|
||||||
in ''
|
devShells = lib.strings.concatStringsSep " " (
|
||||||
|
lib.attrsets.mapAttrsToList (key: _: key) inputs.self.devShells
|
||||||
|
);
|
||||||
|
in
|
||||||
|
''
|
||||||
nix-direnv() {
|
nix-direnv() {
|
||||||
if [ -z "$1" ]; then
|
if [ -z "$1" ]; then
|
||||||
echo "use flake" > .envrc
|
echo "use flake" > .envrc
|
||||||
@@ -37,12 +41,12 @@
|
|||||||
direnv allow
|
direnv allow
|
||||||
}
|
}
|
||||||
|
|
||||||
_nix-direnv-completion() {
|
_nix-direnv_completion() {
|
||||||
local shells=(${devShells})
|
local shells=(${devShells})
|
||||||
compadd -- $shells
|
compadd -- $shells
|
||||||
}
|
}
|
||||||
|
|
||||||
compdef _nix-direnv-completion nix-direnv
|
compdef _nix-direnv_completion nix-direnv
|
||||||
'';
|
'';
|
||||||
|
|
||||||
p10k.extraRightPromptElements = [ "direnv" ];
|
p10k.extraRightPromptElements = [ "direnv" ];
|
||||||
|
@@ -533,6 +533,6 @@ in
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
programs.zsh.initExtra = builtins.readFile ./theme.completion.sh;
|
programs.zsh.initExtra = builtins.readFile ./theme.completion.zsh;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
_theme-completion() {
|
_theme_completion() {
|
||||||
local options=(
|
local options=(
|
||||||
'-m[set mode: light, dark, or toggle]:mode:(light dark toggle)'
|
'-m[set mode: light, dark, or toggle]:mode:(light dark toggle)'
|
||||||
'-w[set wallpaper: specify file path]:file:_files'
|
'-w[set wallpaper: specify file path]:file:_files'
|
||||||
@@ -8,4 +8,4 @@ _theme-completion() {
|
|||||||
_arguments -s $options
|
_arguments -s $options
|
||||||
}
|
}
|
||||||
|
|
||||||
compdef _theme-completion theme
|
compdef _theme_completion theme
|
@@ -18,8 +18,9 @@ toggle_mode() {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
show_usage() {
|
usage() {
|
||||||
echo "Usage: theme [-m {light|dark|toggle}] [-w <file>]"
|
echo "Usage: theme [-m {light|dark|toggle}] [-w <file>]"
|
||||||
|
exit 1
|
||||||
}
|
}
|
||||||
|
|
||||||
finish() {
|
finish() {
|
||||||
@@ -42,8 +43,7 @@ while getopts "m:w:" opt; do
|
|||||||
toggle_mode
|
toggle_mode
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
show_usage
|
usage
|
||||||
exit 1
|
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
;;
|
;;
|
||||||
@@ -51,8 +51,7 @@ while getopts "m:w:" opt; do
|
|||||||
set_wallpaper "$OPTARG"
|
set_wallpaper "$OPTARG"
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
show_usage
|
usage
|
||||||
exit 1
|
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
@@ -7,6 +7,7 @@
|
|||||||
(import ./format.nix { device = "/dev/disk/by-id/nvme-SAMSUNG_MZVL22T0HBLB-00BL2_S64RNE0R602762"; })
|
(import ./format.nix { device = "/dev/disk/by-id/nvme-SAMSUNG_MZVL22T0HBLB-00BL2_S64RNE0R602762"; })
|
||||||
./hardware
|
./hardware
|
||||||
|
|
||||||
|
../common/configs/system/backup
|
||||||
../common/configs/system/bluetooth
|
../common/configs/system/bluetooth
|
||||||
../common/configs/system/boot
|
../common/configs/system/boot
|
||||||
../common/configs/system/brightnessctl
|
../common/configs/system/brightnessctl
|
||||||
|
@@ -11,6 +11,7 @@
|
|||||||
# elara runs on an external drive on eirene
|
# elara runs on an external drive on eirene
|
||||||
../eirene/hardware
|
../eirene/hardware
|
||||||
|
|
||||||
|
../common/configs/system/backup
|
||||||
../common/configs/system/bluetooth
|
../common/configs/system/bluetooth
|
||||||
../common/configs/system/boot
|
../common/configs/system/boot
|
||||||
../common/configs/system/brightnessctl
|
../common/configs/system/brightnessctl
|
||||||
|
Reference in New Issue
Block a user