blob: 74b73c415104408c4c806b0091c28d3fc3004de2 (
plain)
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
|
#!/bin/bash
#
# Bash script by Gene Alexander (http://www.eracc.com/contact)
# of ERA Computers & Consulting (www.eracc.com, blog.eracc.com, shopping.eracc.com)
# Written using vim, the BEST plain text file editor in all of Creation.
#
# Teach yourself bash scripting: http://tldp.org/LDP/abs/html/index.html
#
# Purpose: To check fragmentation on XFS with xfs_db and run xfs_fsr on XFS mount points that
# are above a specific fragmentation threshold.
#
# What is xfs_db? Use 'man xfs_db' to find out.
# What is xfs_fsr? Use 'man xfs_fsr' to find out.
#
# Any busy files, such as open logs on /var/log, will be skipped. To defragment logs one should
# wrap this script with another script to stop and restart logging. Or, even better, write
# one's own script just for defragmentation of the logs.
#
# Warranty: NONE. Use at your own discretion and be aware that data loss is on your head if
# you choose to use this script.
#
# License: GPL 2.0 http://www.gnu.org/licenses/gpl-2.0.html
#
# Suggested Usage: crontab file for root
# 0 0 * * * /root/bin/chkxfsfrag # Run at midnight
#
# Original Release: 2011 December 15 (Merry Christmas!)
# DO NOT ALTER HEADER FROM THIS LINE UP.
#
e='/bin/echo -e' # Use the echo command, not built-in.
xfsfsr=/usr/bin/xfs_fsr # Set variable with the path to xfs_fsr.
xfsdb=/usr/bin/xfs_db # Set variable with the path to xfs_db.
pctmax=15 # Set maxiumum frag percent needed for defrag.
# This is zero here for testing purposes only
# a higher number should be used in production.
array=`df -T|grep xfs|cut -f 1 --delim=" "` # Array of all XFS file systems.
for i in ${array[@]};
do
#check that the device is SATA and skip defrag on SSDs
device=`echo ${i} | cut -f 3 --delim="/" | sed 's/[0-9]//g'`
isSATA=`cat /sys/block/${device}/queue/rotational`
if [[ $isSATA -eq 1 ]]
then
percentage=`$xfsdb -c frag -r ${i}|cut -f 7 --delim=" "`
percent2=`$e $percentage|cut -f 1 --delim=.`
if [ "$percent2" -gt "$pctmax" ]
then
$e "${i} is $percentage% fragmented. Running defragment on ${i}."
# Only uncomment one of the following two lines.
#$xfsfsr -v ${i} # Uncomment for verbose defrag.
$xfsfsr ${i} # Uncomment for quiet defrag.
else
$e "${i} is $percent2% fragmented and is below the fragmentation threshold of $pctmax%. Skipping."
fi
else
echo "${i} is an SSD. Skipping."
fi
done
exit 0
|