-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup-Small.sh
executable file
·28 lines (26 loc) · 1.14 KB
/
backup-Small.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
#!/bin/bash
# Searches files/dirs matching "wildcard" (should at least be "*") in "SearchDir" of size below 1MiB
# or whatever "size" is set to (value in KiB units) and tars and compresses them
# "archive" should not already exist, this script can't add files to a pre-existing archive
# if archive name is "-" it will be written out to stdout, e.g. for compression
# EXAMPLE:
# ./Backup/backup-Small.sh ./Backup - "*" | xz > testb.tar.xz
# tar -xJf Backup.tar.xz
# xz -cdk Backup.tar.xz | tar -tf -
SearchDir="$1"
archive="$2"
wildcard="$3"
size="${4:-1024}"
[ -d "$SearchDir" ] && { [ ! -f "$archive" ] || [ "$archive" == "-" ]; } && [ "" != "$wildcard" ] || { \
printf "Usage: %s SearchDir archive [wildcard]\\n" "$0" >&2
exit -1
}
printf %s\\n "Searching Items..." >&2
items=()
while IFS= read -rd '' item; do
if [ "$(du -cx "$SearchDir"/"$item" | awk 'END { print $1 }')" -lt "$size" ]; then # less than 1MiB or $size
items+=("$item")
fi
done < <(cd "$SearchDir" && find ./* -iname "$wildcard" ! -name "$archive" -print0 2>/dev/null)
printf %s\\n "Archiving Items..." >&2
tar -cf "$archive" -C "$SearchDir" --null -T <(printf %s\\0 "${items[@]//\\/\\\\}")