-
Notifications
You must be signed in to change notification settings - Fork 0
/
clean.sh
executable file
·71 lines (64 loc) · 2.04 KB
/
clean.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
#!/bin/false
# This file should be sourced by backup.sh
#
# Script to clean up space.
case "$BACKUP_CLEAN_VAR" in
( "%" )
total_space=$(df -Pm "$BACKUP_MAIN" | awk 'FNR==2{print $2}')
FREE_SPACE_NEEDED=$(( total_space / 100 * BACKUP_CLEAN_VAL ))
;;
( "G" )
FREE_SPACE_NEEDED=${BACKUP_CLEAN_VAL}024024
;;
( * )
exit 2
esac
# return status of 0 (true) means "need to clear space"
check_space()
{
free_space_available=$(df -Pm "$BACKUP_MAIN" | awk 'FNR==2{print $4}')
test "$free_space_available" -lt "$FREE_SPACE_NEEDED"
}
check_space || return 0 # no cleanup needed
if test "$BACKUP_CLEAN_BY_FREQ" = "1"; then
# Uses 'timeline' index to get rows with freq!=0, then builds a temporary index for age.
# We can't have this index permanently, since it depends on _current_ time
# '+1' is here for files deleted "right now" - otherwise, for them
# (strftime('now') - strftime(deleted)) equals 0 and they are immune to cleaning
sql="SELECT dirname || filename || '/' || created,
freq*(strftime('%s', 'now', 'localtime')+1-strftime('%s', deleted)) AS age,
rowid
FROM history
WHERE freq != 0
AND deleted <= CASE freq
$HARDLINK_EXPR ELSE 'now' END
ORDER BY age DESC,
dirname;"
else
# Uses 'timeline' index both for WHERE and for ORDER BY
sql="SELECT dirname || filename || '/' || created,
rowid
FROM history
WHERE freq != 0
ORDER BY deleted ASC;"
fi
echo "$sql" | $SQLITE | (
echo '.timeout 10000'
echo 'BEGIN TRANSACTION;'
while IFS="$NL" read -r f; do
check_space || break
filename="${f%%|*}"
rowid="${f##*|}"
# Note: below command will fail for directories for two reasons:
# 1. because '$filename' has a creation date in it, and this is
# never a case for directories
# 2. because 'rm' command doesn't have '-r' argument, so it
# won't delete any directories, even if they matched.
rm -f "$BACKUP_MAIN/$filename"* 2>/dev/null
echo "DELETE FROM history WHERE rowid='$rowid';"
done
echo 'END TRANSACTION;'
echo 'PRAGMA optimize;'
) > "$BACKUP_TMP".sql
<"$BACKUP_TMP".sql $SQLITE
rm "$BACKUP_TMP".sql