-
Notifications
You must be signed in to change notification settings - Fork 2
/
wx
executable file
·189 lines (168 loc) · 5.16 KB
/
wx
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/bin/bash
# name: wx
# author: Wil Hall
# email: [email protected]
READBACK=true
usage()
{
cat << EOF
usage: $(basename $0)
This utility performs backups and restorations of Windows application settings.
Backups are managed in '~/.appdata/'.
OPTIONS:
-h | --help Show this message
-r | --restore <SCOPE> <DOMAIN> [FILE_GLOB] The domain to restore
-R | --restore-all <SCOPE> Restore all domains that have backups
-b | --backup <SCOPE> <DOMAIN> [FILE_GLOB] The domain to back up
-B | --backup-all <SCOPE> Back up all domains that already have backups
<SCOPE> is "Roaming", "Local", or "Registry"
<DOMAIN> is one of:
- When <SCOPE> is "Registry": The registry key to operate on.
- When <SCOPE> is "Roaming" or "Local": A subdirectory of %USERPROFILE%/AppData/<Roaming|Local>; may be a subpath.
EOF
}
resolve_domain() {
if [ $SCOPE = "Registry" ]; then
echo "$1"
else
WINDOWS_APPDATA_PATH="$(wslpath "$(wslvar USERPROFILE)")/AppData"
DOMAIN="$(echo "$1" | tr \\\\ "/")"
echo "$WINDOWS_APPDATA_PATH/$SCOPE/$DOMAIN"
fi
}
backup() {
if [ $SCOPE = "Registry" ]; then
REG_FILE_NAME="$(echo "$1" | tr \\\\ "%")"
reg.exe export "$1" "$HOME/.dotfiles/.appdata/$SCOPE/$REG_FILE_NAME.reg" /y
else
BACKUP_PATH="$HOME/.appdata/$(echo $1 | cut -d'/' -f7- | rev | cut -d'/' -f2- | rev)"
mkdir -p $(dirname $BACKUP_PATH)
rsync -zarvm --prune-empty-dirs --mkpath --include="*/" --include="${2:-"*"}" --exclude="*" "$1" "$BACKUP_PATH"
fi
}
restore() {
if [ $SCOPE = "Registry" ]; then
REG_FILE_NAME="$(echo "$1" | tr \\\\ "%")"
reg.exe import "$HOME/.dotfiles/.appdata/$SCOPE/$REG_FILE_NAME.reg"
else
BACKUP_PATH="$HOME/.appdata/$(echo $1 | cut -d'/' -f7-)"
RESTORE_PATH="$(echo $1 | rev | cut -d'/' -f2- | rev)"
mkdir -p $(dirname $RESTORE_PATH)
if [ ! -d "$BACKUP_PATH" ]; then
echo "No backup exists for the domain '$1'"
exit 1
fi
rsync -zarvm --prune-empty-dirs --mkpath --include="*/" --include="${2:-"*"}" --exclude="*" "$BACKUP_PATH" "$RESTORE_PATH"
fi
}
backup_all() {
READBACK=false
if [ $SCOPE = "Registry" ]; then
for BACKUP_FILE in $HOME/.dotfiles/.appdata/$SCOPE/*.reg; do
FILENAME="${BACKUP_FILE##*/}"
REGISTRY_KEY="$(echo "${FILENAME%.*}" | tr "%" \\\\)"
reg.exe export "$REGISTRY_KEY" "$BACKUP_FILE" /y
done
else
WINDOWS_APPDATA_PATH="$(wslpath "$(wslvar USERPROFILE)")/AppData"
shopt -s globstar dotglob
for BACKUP_FILE in $HOME/.appdata/$SCOPE/**/*; do
if [ -f "$BACKUP_FILE" ]; then
RELATIVE_PATH="$(echo $BACKUP_FILE | cut -d'/' -f6-)"
SOURCE_PATH="$WINDOWS_APPDATA_PATH/$SCOPE/$RELATIVE_PATH"
BACKUP_PATH="$HOME/.appdata/$SCOPE/$RELATIVE_PATH"
mkdir -p $BACKUP_PATH
rsync -az --mkpath "$SOURCE_PATH" "$BACKUP_PATH"
fi
done
fi
}
restore_all() {
READBACK=false
if [ $SCOPE = "Registry" ]; then
for BACKUP_FILE in $HOME/.dotfiles/.appdata/$SCOPE/*.reg; do
reg.exe import "$BACKUP_FILE"
done
else
WINDOWS_APPDATA_PATH="$(wslpath "$(wslvar USERPROFILE)")/AppData"
shopt -s globstar dotglob
for BACKUP_FILE in $HOME/.appdata/$SCOPE/**/*; do
if [ -f "$BACKUP_FILE" ]; then
RELATIVE_PATH="$(echo $BACKUP_FILE | cut -d'/' -f6-)"
SOURCE_PATH="$WINDOWS_APPDATA_PATH/$SCOPE/$RELATIVE_PATH"
BACKUP_PATH="$HOME/.appdata/$SCOPE/$RELATIVE_PATH"
mkdir -p $SOURCE_PATH
rsync -az --mkpath "$BACKUP_PATH" "$SOURCE_PATH"
fi
done
fi
}
scopes=("Roaming" "Local" "Registry")
while [ ! $# -eq 0 ]; do
case "$1" in
-r | --restore)
if [[ ${scopes[@]} =~ $2 ]]; then
SCOPE="$2"
else
echo "<SCOPE> is required"
exit 1
fi
if [ "$3" ]; then
DOMAIN="$(resolve_domain "$3")"
if [[ $? != 0 ]]; then exit 1; fi
restore "$DOMAIN" "$4"
exit
else
echo '--restore requires a DOMAIN'
exit 1
fi
;;
-b | --backup)
if [[ ${scopes[@]} =~ $2 ]]; then
SCOPE="$2"
else
echo "<SCOPE> is required"
exit 1
fi
if [ "$3" ]; then
DOMAIN="$(resolve_domain "$3")"
if [[ $? != 0 ]]; then exit 1; fi
backup "$DOMAIN" "$4"
exit
else
echo '--backup requires a DOMAIN'
exit 1
fi
;;
-R | --restore-all)
if [[ ${scopes[@]} =~ $2 ]]; then
SCOPE="$2"
else
echo "<SCOPE> is required"
exit 1
fi
restore_all
exit
;;
-B | --backup-all)
if [[ ${scopes[@]} =~ $2 ]]; then
SCOPE="$2"
else
echo "<SCOPE> is required"
exit 1
fi
backup_all
exit
;;
-h | --help)
usage
exit
;;
*)
usage
exit
;;
esac
shift
done
usage