-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup-nvim.bash
More file actions
115 lines (95 loc) · 2.3 KB
/
Copy pathbackup-nvim.bash
File metadata and controls
115 lines (95 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env bash
the_name="backup-nvim"
print_usage() {
echo "Usage: $the_name {backup [data|config|lazy]|restore <backup-file>}"
}
check_commands() {
local required_commands=("realpath" "hostname" "tar")
for cmd in "${required_commands[@]}"; do
if ! command -v "$cmd" >/dev/null 2>&1; then
echo "Error: '$cmd' is required but not installed."
return 1
fi
done
return 0
}
if ! check_commands; then
exit 1
fi
data_source=$(realpath -s --relative-to "$HOME" "${XDG_DATA_HOME:-$HOME/.local/share}/nvim")
config_source=$(realpath -s --relative-to "$HOME" "${XDG_CONFIG_HOME:-$HOME/.config}/nvim")
lazy_source="$data_source/lazy"
backup_dir="$HOME/Archives/Backups"
backup() {
mkdir -p "$backup_dir"
archive_name="neovim_${SYSTEM:-$(hostname)}_$(date +"%Y%m%d_%H%M%S")"
if [ "$1" = "data" ]; then
tar_targets=("$data_source")
archive_name="${archive_name}_data"
elif [ "$1" = "config" ]; then
tar_targets=("$config_source")
archive_name="${archive_name}_config"
elif [ "$1" = "lazy" ]; then
tar_targets=("$lazy_source")
archive_name="${archive_name}_lazy"
else
tar_targets=("$data_source" "$config_source")
fi
archive_path="$backup_dir/$archive_name.tar.gz"
if tar -czhf "$archive_path" -C "$HOME" "${tar_targets[@]}"; then
echo "Backup of Neovim data created successfully: '$archive_path'"
else
echo "Error: Failed to create backup."
exit 1
fi
}
restore() {
if [ -z "$1" ]; then
echo "Error: No backup file specified."
exit 1
fi
backup_file="$1"
if [ ! -f "$backup_file" ]; then
echo "Error: Backup file $backup_file does not exist."
exit 1
fi
backup
rm -rf "$data_source" "$config_source"
if tar -xzhf "$backup_file" -C "$HOME"; then
echo "Neovim data restored successfully from '$backup_file'"
else
echo "Error: Failed to restore from backup."
exit 1
fi
}
if [ "$#" -eq 0 ]; then
print_usage
exit 1
fi
case "$1" in
backup)
if [ -n "$2" ]; then
if [ "$2" = "data" ] || [ "$2" = "config" ] || [ "$2" = "lazy" ]; then
backup "$2"
else
print_usage
exit 1
fi
else
backup
fi
;;
restore)
if [ -n "$2" ]; then
restore "$2"
else
echo "Error: No backup file specified"
print_usage
exit 1
fi
;;
*)
print_usage
exit 1
;;
esac