-
Notifications
You must be signed in to change notification settings - Fork 2
/
metaclone
56 lines (42 loc) · 1.44 KB
/
metaclone
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
#! /bin/sh
# Exit if any command fails
set -e
# 2012-10-03 05:51:23
# a script to create a meta copy of the required directory
src="$1"
test -z "$src" -o "${src:0:1}" != "/" && echo "Usage is: `basename $0` directory_full_path" && exit
test "$(ls "$src")" = "$(ls /)" && echo "I will not clone the root folder, sorry" && exit
test ! -d "$src" && "'$src' is not a valid directory"
cd "$src"
tempdir=$(mktemp -d)
basedir=$(basename "$src")
dst=$tempdir/$basedir
mkdir "$dst"
mdsync()
{
touch --reference="$1" "$2"
chown --reference="$1" "$2"
chmod --reference="$1" "$2"
}
echo "Creating directories ..."
find . -depth -type d -exec mkdir -p "$dst"/\{\} \;
echo "Cloning files ..."
find . -type f | while read filename; do
test -f "$dst/$filename" && echo "$dst/$filename already exists. This should have never happened. Bailing out!" && exit 100
fsize=`stat -c '%s' "$filename"`
if [ $fsize -gt 0 ]; then
fallocate -o $((fsize-1)) -l 1 "$dst/$filename" || exit
fi
mdsync "$filename" "$dst/$filename"
done
echo "Cloning directories dates/owners/accesses ..."
find . -depth -type d | while read filename; do
mdsync "$filename" "$dst/$filename"
done
echo "All done"
cd "$tempdir"
test -f "$basedir.tar" && echo "$basedir.tar already exists. This should have never happened. Bailing out!" && exit 100
tar -Scf "$basedir.tar" "$basedir"
echo
echo "The resulting file can be located at $tempdir/$basedir.tar"
echo "$tempdir/$basedir can be safely deleted"