rs-backup-suite/server/usr/sbin/spin-down-storage

49 lines
1.8 KiB
Bash
Executable File

#!/usr/bin/env bash
##
# Copyright (C) 2013-2016 Janek Bevendorff
# Website: http://www.refining-linux.org/
#
# Utility program. Check if disk has been used since last check and spin it down if not.
# Some distributions such as Synology DSM implement this by themselves.
#
# The MIT License (MIT)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
##
if [ "${1}" == "" ]; then
echo "Usage: $(basename ${0}) <device file>"
exit
fi
last_state_file="/tmp/storage-state-${1}"
touch $last_state_file
chmod 600 $last_state_file
new_storage_state=$(cat /proc/diskstats | grep "$1")
old_storage_state=$(cat $last_state_file)
if [ "$new_storage_state" = "$old_storage_state" ]; then
sync
sdparm --flexible --readonly --command=stop /dev/$1 2>&1 > /dev/null
fi
echo "$new_storage_state" > $last_state_file