-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig_manager.sh
executable file
·90 lines (75 loc) · 1.95 KB
/
config_manager.sh
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
#!/bin/bash
# Set the user's home directory and backup directory
HOME_DIR="$HOME"
BACKUP_ROOT="$HOME/config_backup"
# List of config files and directories to backup or restore
CONFIG_FILES_LIST=(
".config"
".gitconfig"
".inputrc"
".ipython"
".p10k.zsh"
".tmux"
".tmux.conf"
".tmux.conf.local"
".zlogin"
".zlogout"
".zpreztorc"
".zprofile"
".zshev"
".zshrc"
)
# Function to create a timestamped backup directory
create_backup_dir() {
local timestamp=$(date +%Y%m%d_%H%M%S)
BACKUP_DIR="$BACKUP_ROOT/$timestamp"
mkdir -p "$BACKUP_DIR"
}
# Function to backup config files and directories
backup() {
# Create the timestamped backup directory
create_backup_dir
for item in "${CONFIG_FILES_LIST[@]}"; do
if [ -e "$HOME_DIR/$item" ]; then
mv -v "$HOME_DIR/$item" "$BACKUP_DIR"
fi
done
echo "Config files and directories backup completed. Backup is stored in $BACKUP_DIR"
}
# Function to restore config files and directories from the backup directory
restore() {
if [ ! -d "$BACKUP_ROOT" ]; then
echo "Backup root directory $BACKUP_ROOT not found. Cannot restore."
exit 1
fi
# Get the latest backup directory
BACKUP_DIR=$(ls -td -- "$BACKUP_ROOT"/*/ | head -n 1)
if [ -z "$BACKUP_DIR" ]; then
echo "No backup found in $BACKUP_ROOT. Cannot restore."
exit 1
fi
# Remove everything managed by chezmoi
chezmoi managed --null | xargs -0 -I {} chezmoi remove {}
# Purge chezmoi's source state
chezmoi purge --force
# Restore files from backup
for item in "${CONFIG_FILES_LIST[@]}"; do
if [ -e "$BACKUP_DIR/$item" ]; then
mv -v "$BACKUP_DIR/$item" "$HOME_DIR"
fi
done
echo "Config files and directories restored from $BACKUP_DIR"
}
# Check command line arguments
if [ "$#" -ne 1 ]; then
echo "Usage: $0 {backup|restore}"
exit 1
fi
if [ "$1" = "backup" ]; then
backup
elif [ "$1" = "restore" ]; then
restore
else
echo "Invalid option. Usage: $0 {backup|restore}"
exit 1
fi