forked from cilix-lab/ubuntu-wrt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge
executable file
·81 lines (73 loc) · 2.18 KB
/
merge
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
#!/bin/bash
ask() { local q="$1"; local d=${2:-"n"}
read -p "$q [$d]: " r; r=${r:-"$d"}
local i=0; while true; do
case $r in
y|Y|yes|Yes|yES|YES )
return 0
;;
n|N|no|No|nO )
return 1
;;
* )
i=$((i+1))
[ $i -le 3 ] && read -p "Not a valid answer. Try 'y' or 'n': " r || exit 1
continue
;;
esac
done
}
usage() {
[ ! -z "$2" ] && echo "Error: $2."
echo "Usage: $0 -s <path/to/source> -t <path/to/target> [-i -e <backup-extension>]"
echo "Options:"
echo -e "\t-i\tOverwrite files without backup."
echo -e "\t-e\tBackup extension. Default: \"-dpkg\""
exit $1
}
sync() {
# copy directory structure
for d in `find $SRC -type d`; do
[ $d == "$SRC" ] && continue
CDIR=$(echo $d | sed "s|$SRC||")
[ ! -d "$DST$CDIR" ] && echo "Making $CDIR" && mkdir -p "$DST$CDIR"
done
# copy files backing up existing
for f in `find $SRC -type f`; do
CFILE=$(echo $f | sed "s|$SRC||")
$BKP && [ -f "$DST$CFILE" ] && echo "Backing up $CFILE to $(basename $CFILE)$EXT" && cp "$DST$CFILE" "$DST$CFILE$EXT"
echo "Copying $CFILE"
cp -f "$f" "$DST$CFILE"
done
}
# get options
BKP=true # Making backups is default.
EXT="-dpkg"
OPTSTR="hs:t:ie:"
while getopts "$OPTSTR" OPT; do
case "$OPT" in
h) usage ;;
s) SRC=$OPTARG ;;
t) DST=$OPTARG ;;
i) BKP=false ;;
e) EXT=$OPTARG ;;
"?") usage 2 "Unknown option: -$OPTARG" ;;
":") usage 3 "No argument value for option: -$OPTARG" ;;
esac
done
if [ ! -z "$SRC" ] && [ ! -z "$DST" ]; then
# check ownership
SRC_OWNER=`ls -lA "$(dirname $SRC)" | grep ' '"$(basename $SRC)"'$' | awk '{print $3}'`
DST_OWNER=`ls -lA "$(dirname $DST)" | grep ' '"$(basename $DST)"'$' | awk '{print $3}'`
if [ ! -z "$SRC_OWNER" ] && [ ! -z "$DST_OWNER" ] &&\
[ "$SRC_OWNER" == "$DST_OWNER" ] && [ "$SRC_OWNER" == "$(whoami)" ] ||\
[ "$(whoami)" == "root" ]; then
sync
else
echo "You are not the owner of the source and/or destination folders and you are not currently running the script as root."
echo "If you wish to modify a CHROOT environment, you will probably need to be root."
ask "Do you wish to continue as $(whoami)?" "n" && sync
fi
else
usage 4 "Missing options"
fi