-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync.sh
executable file
·62 lines (48 loc) · 1.16 KB
/
sync.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
# stop on error
set -e
usage() {
echo "Usage: $0 {dir2sync}" >&2;
exit 1;
}
if [ -z "$1" ]
then
usage
fi
SYNC_PATH=$1
SCRIPT_PATH="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
SRC_ROOT=$(realpath $SCRIPT_PATH/../root-fs)
DST_ROOT=/
SRC=$(realpath -L $SRC_ROOT/$SYNC_PATH)
DST=$(realpath -mL $DST_ROOT/$SYNC_PATH)
BACKUP_BASE=/root/_ng-lamp.bak
BACKUP_DIR=$(realpath -mL $BACKUP_BASE/$SYNC_PATH)
BACKUP_SUFFIX=$(date +"_%Y%m%d%H%M")
[[ -e "$SRC" ]] || {
echo "Error: Source $SRC does not exist!";
exit 1;
}
if [[ -d "$SRC" ]]; then
SRC="$SRC/";
DST="$DST/";
else
# $SRC is a file then $BACKUP_DIR and $DST should map file's directory
BACKUP_DIR=$(dirname "$BACKUP_DIR");
DST=$(dirname "$DST");
fi
RSYNC="rsync -v"
RSYNC_ARGS="-r --checksum -b --suffix=$BACKUP_SUFFIX --backup-dir=$BACKUP_DIR"
echo "About to copy files"
echo "from: $SRC"
echo " to: $DST"
echo "(a backup will be made here $BACKUP_DIR)"
while true; do
read -p "Do you wish to proceed? [Y/n] " yn
case $yn in
[Yy]* | '' )
$RSYNC $RSYNC_ARGS $SRC $DST;
break;;
[Nn]* ) exit;;
* ) echo "Please answer Yes or No.";;
esac
done