[sh] Moving content reading from a file

Posted by

As an Linux administrator sometimes some systems produce lots of data in a few hours, normally the data will be stored on a specific partition on the hard drive, but sometimes the systems use shared partitions by default, so, on the same path you will have different data from different sources.

A monitored environment will have threashold configuration to alert if something not normal is happening on the device, so you can take inmediately action, on this example exist a threashold to trigger an alert if the partition $PATH_SOURCE is reaching the 90% of capacity.

If the alert is triggered, the next action is to check what is the content of the $PATH_SOURCE and move the content you are able to move to another partition on the same device, as I mentioned at the begining of the post, probably you have different data from different sources, so you will need to specify the data you want to move.

At this time you are moving data that was listed line by line on a file, so the script will read each line from the file.

For example:

new4-notepad2b2b
log.txt

The script will read line by line, it will capture and save the result [thtas24_1.txt] on a variable and it will go to the specific path [$PATH_SOURCE] to find the file you want to move, if it matches with any file on the [$PATH_SOURCE] , the file is moved. The script will repeat the process with every line on the file until it is completed.

Also, you will see the start and end time of the process.

For example,

[root@localhost securitytweak]# ./move_data.sh
—————
Moving data…
—————
**3**
**2**
**1**
—————
Start: Thu Jan 31 17:57:59 EST 2019
End: Thu Jan 31 18:12:28 EST 2019
Duration: 15:31 minutes

-SCRIPT-

#!/bin/sh

#
#Autor: brenseg
#Year: 2019
#

PATH_SOURCE=/home/securitytweak/datafiles
PATH_DEST=/home/valparaiso/datafiles_backup
LOG=/home/brenseg

echo “—————”
echo ” Moving data…”
echo “—————”
sleep 1
echo “**3**”
sleep 1
echo “**2**”
sleep 1
echo “**1**”
echo “—————”

echo “Start: `date`”
START=`date +%s`

while read LINE; do

FILENAME=`echo $LINE`
cd $PATH_SOURCE
mv $FILENAME $PATH_DEST

done < $LOG/log.txt
#echo “$FILENAME”

SPENT=$(((`date +%s` – $START)/60))

echo “End: `date`”
echo “Duration: $SPENT minutes”
exit 0

Note: The script will only work on the same device.

Advertisements

One comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s