-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfunctions.sh
executable file
·107 lines (94 loc) · 2.99 KB
/
functions.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/bin/bash
set -e
DRYRUN="true"
minfind=60
path=
verbose=0
function usage() {
echo "$0" [ -t MIN_MINUTES] -a PATH_TO_DRAIN [-b PATH_TO_FILL]
}
function getFirstDrive() {
#$1 is the pool. Like /mergerfsVol_with_fwfs
echo -n $(xattr -l "$1"/.mergerfs|grep srcmounts|cut -d" " -f2|cut -d":" -f1)
}
function getPoolFromDrive() {
drive="$1"
echo -n "$(df -k|grep $(basename "$drive")|grep -v "$drive"|awk '{print $6}')"
}
function getDriveFromPath(){
echo -n $(df "$1"|tail -n1|awk '{print $6}')
}
function getDriveWithMostFreeSpace() {
#$1 is the pool. Like /mergerfsVol_with_fwfs
diskToDrain="$1"
if [[ -z "$2" ]]; then
pool="$(getPoolFromDrive "$diskToDrain")"
else
pool="$2"
fi
free=0
drives=$(xattr -l "$pool"/.mergerfs|grep srcmounts|cut -d" " -f2| sed 's/:/\n/g')
while read drive;do
if [[ $drive == $diskToDrain ]];then
continue;
fi
currentFree=$(df -k "$drive"|awk '/[0-9]%/{print $(NF-2)}')
if [[ $currentFree -gt $free ]];then
mostFreeDrive="$drive"
free=$currentFree
fi
done <<<"$drives"
echo -n "$mostFreeDrive"
}
function moveIdleFilesInPath() {
oldpwd=$PWD
sourceFolder="$1"
if [[ -z $2 ]]; then
targetFolderSet=0
#df "$sourceFolder"|tail -n1|grep "$sourceFolder" > /dev/null||echo "Without target folder, the source folder needs to be the root of the drive." && exit 1 #make sure sourceFolder is at root of disk.
if ! df "$sourceFolder"|tail -n1|grep "$sourceFolder" > /dev/null;then
echo "Without target folder, the source folder needs to be the root of the drive."
exit 1
fi
else
targetFolderSet=1
targetFolder="$2"
if [[ ! -e "$targetFolder" ]]; then
echo "Target folder does not exist."
exit 1
fi
fi
rootOfSource="$(getDriveFromPath "$sourceFolder")"
if [[ -z "$POOL" ]]; then
POOL="$(getPoolFromDrive "$rootOfSource")"
if [[ $(echo "$POOL" |wc -l) -gt 1 ]]; then
echo "More than one pool with this drive. Use POOL in settings to specify."
exit 1
fi
fi
echo "Entering $sourceFolder. Scanning. Standby."
# cd "$sourceFolder" && find . "${FINDOPTS[@]}" -type f -mmin +$minfind -print0 | while read -d "" path;do
cd "$sourceFolder" && find . "${FINDOPTS[@]}" -type f -mmin +$minfind -printf "%C@ %p\n" |sort|cut -d ' ' -f2-| while read path;do
[[ -e "$path" ]] || continue #rsync can take a long time and files can have been moved
lsof "$path" &> /dev/null && continue # move along if the file is in use
#
filesize=$(du -k "$path"|awk '{print $1}')
#if no targetFolder is set, then find the one with most free space.
if [[ $targetFolderSet -eq 0 ]]; then
targetFolder=$(getDriveWithMostFreeSpace "$rootOfSource" $POOL)
fi
freeSpaceOnTarget=$(df -k "$targetFolder"|awk '/[0-9]%/{print $(NF-2)}')
if [[ $filesize -lt $freeSpaceOnTarget ]];then
echo ""$path" "$targetFolder"/"
if [[ $DRYRUN == "false" ]]; then
ionice rsync --remove-source-files -Rha --progress "$path" "$targetFolder"/
fi
sleep .5
# exit
else
echo "Not enough space on target."
cd $oldpwd && exit 1
fi
done
cd $oldpwd
};