summaryrefslogtreecommitdiffstats
path: root/scripts/clone_stage4
blob: 2704db0255117dbc1a408fce0d7521c1777de718 (plain) (blame)
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
#!/bin/bash
#
#	Helper script to generate a stage4 export
#	for a remote machine per rsync.
#

SELF="$(readlink -f $0)"
SCRIPTS_DIR="$(dirname "${SELF}")"
ROOT_DIR="${SCRIPTS_DIR%/*}"

. "${ROOT_DIR}/helper/logging.inc"

MLTK_PID="$$"

qnd_exit() {
	unset_quiet
	kill "$MLTK_PID"
	[ $# -ge 1 ] && kill "$1"
}

[ $# -lt 3 ]		&& perror "$0 <remote_ip> <stage_to_exclude> <target_dir>"
[ "$1" = "local" ]	&& perror 'It is not wise to use "local" as remote IP.'

BASE_DIR="${ROOT_DIR}/server/local_builds/$1"
BOOT_DIR="${ROOT_DIR}/server/boot/$1"
[ ! -d "$BASE_DIR" ] && perror "Unknown Vorlage '$1'"
BASE_DIR="${BASE_DIR}/$2"
[ ! -d "$BASE_DIR" ] && perror "Unknown Stage '$2' for Vorlage '$1'"
TARGET_DIR="$3"
mkdir -p "$TARGET_DIR"
mkdir -p "$BOOT_DIR"

EXCLUDE="$BOOT_DIR/exclude-stage4"
INCLUDE="$BOOT_DIR/include-stage4"

pinfo "Building rsync exclude-file for building stage 4...."
echo "## Exclude file for stage4 of $1" > "$EXCLUDE"
echo "## Include file for stage4 of $1" > "$INCLUDE"
for FILE in $(find "$ROOT_DIR"/server/blacklists/*/ -type f); do
	echo "## From $FILE" >> "$EXCLUDE"
	echo "## From $FILE" >> "$INCLUDE"
	grep '^-' "$FILE" >> "$EXCLUDE"
	grep '^+' "$FILE" >> "$INCLUDE"
done

echo "## From cloned $2 of $1" >> "$EXCLUDE"
# this next command lists all files found in BASE_DIR and removes the prefix BASE_DIR
IFS=$'\n'
for FILE in $(find "${BASE_DIR}" -type f | cut -c $[${#BASE_DIR} + 1]-); do
	echo "- $FILE" >> "$EXCLUDE"
done
unset IFS

pinfo "Done."
[ "y$UID" == "y0" ] || perror "You're not root. Cannot continue with rsync."

# prepare rsync's options depending on whether the source is local or remote
RSYNC_OPTS=""
RSYNC_SOURCE=""
if [[ "$1" == "local" ]]; then
	RSYNC_SOURCE="/"
else
	RSYNC_SOURCE="root@$1:/"
	RSYNC_OPTS="-e ssh -oStrictHostKeyChecking=no"
fi

pinfo "Cloning via rsync"
cat "$INCLUDE" "$EXCLUDE" | rsync -a --delete --delete-excluded --numeric-ids -v --exclude-from=- "${RSYNC_OPTS}" "${RSYNC_SOURCE}" "${TARGET_DIR}" || perror "rsync from '${RSYNC_SOURCE}' to '${TARGET_DIR}' failed."