-
Notifications
You must be signed in to change notification settings - Fork 15
/
rootsync.sh
executable file
·138 lines (119 loc) · 2.69 KB
/
rootsync.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
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
#!/bin/bash -e
#
# Syncs the backups stored in the specified directory
# with the system-wide files in /
#
# It will ask confirmation for every file to apply the changes or
# discard them entirely.
#
# Fernando Carmona Varo
#
# Source directory to be synced with the root directory tree
SRCDIR="${XDG_USER_CONFIG:-$HOME/.config}"/ROOT
if [ $(id -u) -ne 0 ]
then
sudo -Ep "This tool must be run as root. Enter password:" "$0" "$@"
exit $?
fi
cd "$(dirname "$0")"
[ -d "$SRCDIR" ] || { echo "Source directory '$SRCDIR' not found."; exit 1; }
seloption() {
# If set to accept all changes, don't ask
[ -z "$acceptall" ] || return 0;
echo -e "+ \e[36mThere are changes to apply to \"$old\""
local ret
select action in "Apply Changes to the file, overwritting the current one (can also press Control+D)" \
"Revert all changes from the newer file, losing them" \
"Ignore this file" \
"Show diff" \
"Edit $new and apply the changes" \
"Ignore all files and cancel update" \
"Accept ALL changes and perform update without asking again"
do
case "$action" in
Apply*)
echo apply
ret=0
break
;;
Revert*)
local tmp="$old" # exchange old and new
old="$new"
new="$tmp"
ret=0
break
;;
Ignore*)
ret=1
break
;;
Show*)
diff -u "$old" "$new" | ${PAGER:-less}
continue
;;
Edit*)
${EDITOR:-nano} "$new"
ret=0
break
;;
Accept*)
acceptall="true"
ret=0
break
;;
Exit*)
exit 1
;;
*)
echo none
break
;;
esac
done
echo -ne "\e[0m"
return $ret
}
# Replace $2 with the data in $1 without altering $2's owner
owninstall() {
# Replace
local owner=$(stat -c%U "$2")
install -o "$owner" -v "$1" "$2"
}
for sfile in $(find "$SRCDIR" -type f \( ! -path '*/.svn/*' \) \( ! -path "*/.git*" \) \( ! -path "*[#~]" \) )
do
# obtain root file path from source file path
rfile="${sfile#$SRCDIR}"
[ -f "$rfile" ] || {
echo "$rfile : file doesn't exist, copying it"
sudo mkdir -p "$(dirname $rfile)"
install -v "$1" "$2"
continue
}
if [ "$rfile" -nt "$sfile" ]
then
new=$rfile
old=$sfile
else
new=$sfile
old=$rfile
fi
if diff -u "$old" "$new"
then
unchanged+="\n$new"
continue
fi
seloption || continue
owninstall "$new" "$old"
change=yes;
done
echo -e "Unresolved files: ${unchanged}"
if [ "$change" ]
then
echo "Assuring permissions..."
# Make sure the relevant files have the right permissions
chmod 440 /etc/sudoers.d/*
#chmod g+rw -R /var/www/
# echo "Syncing filesystem..."
# # Force a filesystem sync so that changes are saved to disk
# sync
fi