-
Notifications
You must be signed in to change notification settings - Fork 2
/
fileRestore.sh
executable file
·61 lines (56 loc) · 2.01 KB
/
fileRestore.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
#!/usr/bin/env bash
## delete regular files only depending on structure of dotfiles
## example: if $1 is a path leading to $HOME/back/FOLDER, we restore
## restoring only works, if there is currently no file
# The following script assumes filenames do not contain
# control characters and dont contain leading dashes(-).
set -o errexit # abort on nonzero exitstatus
set -o nounset # abort on unbound variable
set -o pipefail # don't hide errors within pipes
IFS="$(printf '\n\t')" # change IFS to just newline and tab
FAIL="TRUE" # will be filled with defaults
cd "${HOME}/dotfiles"
dotfilePaths="$(fd -uu --type f --ignore-file "$HOME/dotfiles/ignorefiles" --ignore-file "$HOME/dotfiles/.gitignore")"
if test -z "$1"; then
echo "no path to restore folder"
echo "usage: ./fileRestore.sh $HOME/back/FOLDER"
fi
ROOT_BACK_FOLDER="$1"
rootbackupFolderPath=$(dirname "$ROOT_BACK_FOLDER")
if test "$rootbackupFolderPath" != "${HOME}/back"; then
echo "$1 (backup folder) is not in $HOME/back"
echo "usage: ./fileRestore.sh $HOME/back/FOLDER"
fi
if ! test -d "$rootbackupFolderPath"; then
echo "$1 (backup folder) is no folder"
echo "usage: ./fileRestore.sh $HOME/back/FOLDER"
fi
while IFS= read -r dfPath; do
printf '%-40s' "$dfPath"
dfabsPath="${HOME}/dotfiles/${dfPath}"
backupAbsPath="${ROOT_BACK_FOLDER}/${dfPath}"
# backupFolderPath=$(dirname "$backupAbsPath")
sysAbsPath="${HOME}/${dfPath}"
# sysFolderPath=$(dirname "$sysAbsPath")
if test -e "$dfabsPath"; then
if test -f "$backupAbsPath"; then
if ! test -e "$sysAbsPath"; then
cp "$backupAbsPath" "$sysAbsPath"
printf '%-20s' "did restore"
FAIL="FALSE"
else
printf '%-20s' "file in user profile"
fi
else
printf '%-20s' "backup: no such regular file"
fi
else
printf '%-20s' "FATAL: fix fd or ignorefiles"
fi
echo -en "\n"
done <<<"$dotfilePaths"
if test "$FAIL" = "FALSE"; then
echo "restored from ${ROOT_BACK_FOLDER}"
else
echo "could not restore from ${ROOT_BACK_FOLDER}"
fi