Pomoc Cron -czyli automatyka zadań

bulgar71

System Engineer
Q Specialist
8 Styczeń 2020
172
2
12
18
44
WROCŁAW
QNAP
TS-x73A
Ethernet
802.11ac (Wi-Fi 5)
Witam wszystkich
czy jest aplikacja która umożliwia zaplanowanie zadań w CRONE?
tak która np usuwała by stare pliki na po 20 dniach?
czyściła katalog temp?
 
Q:
czy jest aplikacja która umożliwia zaplanowanie zadań w CRONE?
A:
Qnapclub Store: WebCrontab

Q:
tak która np usuwała by stare pliki na po 20 dniach?
A:
Wymagane zainstalowanie Entware w App Center. Jeśli nie to skrypt sam zainstaluje wersje dla x86_64.
Bash:
#!/bin/sh

## settings
## ###########################################################################

DIR="/share/Pool/test"
PERCENT_DISK_USAGE=90
MAX_DAYS_LIMIT=90	# must be > MIN_DAYS_LIMIT
MIN_DAYS_LIMIT=1	# must be >= 1

DEBUG=1			# if DEBUG set 1 it will not delete data

## helpers
## ###########################################################################

## usage:   _exit Dummy message
## Result:  print message and abort program
## ###########################################################################
function _exit() {
	echo -e "$*"
	echo
	exit 1
}

## usage:   test_dir ${var} && echo Directory exist and is accessible
## Returns: true if $1 is accessible
## ###########################################################################
function test_dir() {
	local _ret=0
	_ret=`cd $1 2>/dev/null && echo 0 || echo 1`
	return $_ret
}

## usage:   echo ${var} direct path is: $(real_dir ${var})
## Result:  print real direct path for $1
## Returns: true if $1 is accessible or false if failed
## ###########################################################################
function real_dir() {
	local _ret=0
	local _dir=
	_dir=`readlink -f "$1"`
	if [ -z "$_dir" ]; then
		_dir="/directory/path/is/incorrect/or/symlink/is/corrupted"
		_ret=1
	fi
	echo -n "$_dir"
	return $_ret
}

## usage:   is_val ${var} && echo "that's an int"
## Returns: true if $1 is Integer
## ###########################################################################
function is_val() {
	if test ${1} -eq ${1} 2>/dev/null; then
		return 0
	fi
	return 1
}

## usage:   entware_install
## Result:  Install x86_64 Entware to the system
## ###########################################################################
function entware_install() {
	local _entware_download_url="https://qnapclub.eu/pl/qpkg/model/download/468616/Entware_1.02std.qpkg"
	local _pkg_install_path="$(getcfg Entware Install_Path -f /etc/config/qpkg.conf -d error)"
	if [ "$_pkg_install_path" = "error" ]; then
		echo [1/5] We are going to install Entware x86_64 ...
		local _pwd=`pwd`
		cd /share/Public
		echo [2/5] Downloading Entware ...
		wget -q --no-check-certificate "$_entware_download_url" -O entware.qpkg
		[ -f "entware.qpkg" ] || _exit $LINENO: Cannot download Entware QPKG.
		echo [3/5] Installing Entware ...
		sh entware.qpkg
		rm entware.qpkg
		cd "$_pwd"
	fi
	local _pkg_enable="$(getcfg Entware Enable -f /etc/config/qpkg.conf -d FALSE)"
	if [ "$_pkg_enable" = "FALSE" ]; then
		setcfg -f /etc/config/qpkg.conf Entware Enable TRUE
#		/etc/init.d/Entware.sh start
	fi
	[ -x "/opt/bin/opkg" ] || /etc/init.d/Entware.sh start
	[ -x "/opt/bin/opkg" ] || _exit $LINENO: Cannot install or run Entware
}

## usage:   entware_deps
## Result:  Installing program dependencies
## ###########################################################################
function entware_deps(){
	if [ ! -x "/opt/bin/find" ]; then
		echo [4/5] Updating Entware repository
		/opt/bin/opkg update		2>/dev/null 1>/dev/null
		echo [5/5] Installing find utils
		/opt/bin/opkg install findutils	2>/dev/null 1>/dev/null
	fi
	[ -x "/opt/bin/find" ] || _exit "$LINENO: Cannot install entware's find util"
}

## usage:   read_disk_usage ${var}
## Result:  print disk usage for $1 path
## Returns: true if disk usage for $1 is readable or false if failed
## ###########################################################################
function read_disk_usage(){
	local _ret=0
	local _du=$(df -hP "$1" 2>/dev/null | grep -v "Filesystem" | awk '{print $5}' | tail -1 | sed 's/%$//g')
	if [ -z "$_du" ]; then
		echo $LINENO: Cannot read disk usage
		_ret=1
	fi
	echo -n $_du
	return $_ret
}

## check prerequisites
## ###########################################################################
test_dir "$DIR" || _exit Clean-up directory does not exist: $DIR
DIR=$(real_dir "$DIR")

entware_install
entware_deps

## program
## ###########################################################################
__du=$(read_disk_usage "$DIR")
is_val 5 || _exit $LINENO: test
is_val $__du || _exit $LINENO: Cannot read disk usage properly
echo "Disk usage for: $DIR --- is: $__du %"
if [ ! "$__du" -gt "$PERCENT_DISK_USAGE" ]; then
	echo Skipping files clean-up...
	exit 0
fi

__days_to_remove=$MAX_DAYS_LIMIT
while [ "$__du" -gt "$PERCENT_DISK_USAGE" ] && [ "$__days_to_remove" -gt "$MIN_DAYS_LIMIT" ]
do
	echo Deleteing files older than $__days_to_remove
	if [ "$DEBUG" = "1" ]; then
		echo DEBUG enabled. Files will NOT be deleted!
		/opt/bin/find "$DIR" -name "*"  -type d -mtime +${__days_to_remove} -print -exec echo "{}" \;
	else
		echo Deleting these files:
		/opt/bin/find "$DIR" -name "*"  -type d -mtime +${__days_to_remove} -print -exec rm -rf "{}" \;
	fi
	echo

	__du=$(read_disk_usage "$DIR")
	is_val $__du || _exit $LINENO: Cannot read disk usage properly
	echo "Disk usage for: $DIR --- is: $__du %"

	__days_to_remove=$(expr $__days_to_remove - 1)
done

## END

Wisisz mi flaszke :)
1. Instalujesz w App Center dwie aplikacje (w Twoim przypadku wersje: TS-NASX86_64)
2. Za pomocą SSH łączysz się z serwerem

3. Teraz wgrywasz gdzies na serwer ten skrypt
Bash:
cd /opt
mkdir -p autoclean_older_than
cd autoclean_older_than
wget http://pool.qnapclub.pl/projects/tools/autoclean_older_than/autoclean_older_than.sh
chmod +x autoclean_older_than.sh

4. Musisz zmodyfikować w jego nagłówku parametr gdzie znajdują się dane, które chcesz czyścić.

a. W tym celu zainstaluj sobie łatwiejszy do poruszania nawigator/edytor po plikach.
Bash:
opkg update
opkg install mc

b. Następnie otwórz plik autoclean_older_than.sh do edycji:
Bash:
mcedit autoclean_older_than.sh

5. W linijce 6 znajduje się: DIR="/share/Pool/test" należy tutaj wpisać lokalizacje danych, które chcesz czyścić.
Np. jeśli dane znajdują się w udziale sieciowym "Dane" to lokalizacja bedzię odpowiednio: DIR="/share/Dane".
Jeśli to będzie "Kopie zapasowe" to w tym miejscu powinno być wpisane: DIR="/share/Kopie zapasowe"

6. Kolejna sprawa to musisz wyłaczyć tryb debugowania.
W linijce 11 przestawić DEBUG na 0: DEBUG=0

7. Teraż możesz sprawdzić czy skrypt działa prawidłowo, uruchamiając go:
Bash:
sh autoclean_older_than.sh
Skrypt kasuje pliki jeśli wykorzystanie dysku będzie wynosiło 90%. Jeśli ten próg jest za wysoki albo za niski to możesz go zmienić w linijkach 7 i 8:
Bash:
MAX_DAYS_LIMIT=90	# must be > MIN_DAYS_LIMIT
MIN_DAYS_LIMIT=1		# must be >= 1
Ja bym jednak tego nie zmieniał.

8. Teraz ważne jest aby skrypt co minutę sprawdzał ile jest wykorzystanej przestrzeni dyskowej, a więc musisz go dodać do harmonogramu.
Wracasz więc do panelu zarządzania QNAP, wchodzisz w App Center, w którym zainstalowałeś aplikacje Web Crontab (punkt 1) i uruchamiasz ją.

9. Dodajesz nowe zadanie naciskając
26383.png

Następnie w polach wpisujesz odpowiednio:
Name: autoclean
Schedule: 0 * * * * *
Command: sh /opt/autoclean_older_than/autoclean_older_than.sh
Naciskasz SAVE.

10. I to tyle.

Jeśli chcesz, to mogę pomóc Ci to zrobić przy użyciu TeamViewera.
Rozwiązany - Nadpisywanie danych
 

Użytkownicy znaleźli tą stronę używając tych słów:

  1. cron