Timebased search script

Hi there

first of all, sry for my bad english and possible mistakes .-)

I write scripts for my server to get my live easier. So my next one will be a script, where I can find special files in a folder.

I put with a script of mine files in a folder on the server with following name structure: name_date_time_ext
This happens every 3 hours. Now I can seperate the name components into variables for a possible search script like that:

   thisdate=`/bin/date "+%d.%m.%Y_%H:%m"`
	date=${thisdate%_*}
	time=${thisdate##*_}

   string=$(ls ~/teamspeak/teamspeak1_* | tail -1)
	path=${string%/*}
	file=${string##*/}
	filename=${file%.*}
	filext=${file##*.}
	filetime=${filename##*_}
	filedate=${filename%_*}
	filedate=${filedate##*_}

Now I can find every file with any date or time like this:

   if [ -e $string ]; then
	if [[ $filename == *$date* ]]; then
		clear && echo "For this date the file exist";
	else
		clear && echo "For this date the file do not exist";
	fi
   else
	clear && echo "No file found, please make backup first"
    fi

What I want to do is, to find the latest file, the last safed file in this folder.
Theoreticly I know I have to do something like a loop. But I don't know how.
I think I need a loop where the date varaible ask for a match.
If not jump back date - 3hours, if match ok if not jump back date - 3hours, and so on.
Bu tI don't know how. I hope someone can help me or give me direction to look for, or a tip to find something similar.

Greets & thanks

You could use sed to paste the year month day hour and minute to the front of the filename like this:

ls ~/teamspeak/teamspeak1_* |
sed -nE 's/([^_]+)_([^.]+).([^.]+).([^_]+)_([^:]+):([^.]+)./\4\3\2\5\6 &/p'

reverse order sort by this value and cut file name back out of top line:

ls ~/teamspeak/teamspeak1_* |
sed -nE 's/([^_]+)_([^.]+).([^.]+).([^_]+)_([^:]+):([^.]+)./\4\3\2\5\6 &/p' | 
sort -r | sed -nE '1s/[^ ]+ //p'

Example:

$ ls
dummyfile  lean_30.01.2015_12:22.txt  team22_10.03.2013_14:19.sql  test_07.12.2013_16:55.txt

$ ls | sed -nE 's/([^_]+)_([^.]+).([^.]+).([^_]+)_([^:]+):([^.]+)./\4\3\2\5\6 &/p' | sort -r
201501301222 lean_30.01.2015_12:22.txt
201312071655 test_07.12.2013_16:55.txt
201303101419 team22_10.03.2013_14:19.sql

$ ls | sed -nE 's/([^_]+)_([^.]+).([^.]+).([^_]+)_([^:]+):([^.]+)./\4\3\2\5\6 &/p' | sort -r | sed -nE '1s/[^ ]+ //p'
lean_30.01.2015_12:22.txt

You used && where ; would suffice.
Its not about the numbers of chars used, but its better to use a condition, which && is, only when it is required.

Since you will display only once, you could put the message to a variable, and print it at the end.
But i guess this could be called preference...

...
	else
		MSG="For this date the file do not exist";
	fi
else
	MSG="No file found, please make backup first"
fi
clear
echo "$MSG"

And to be honest, in my opinion clear is not required at all.

hth

Hi

@Chubler_XL
Really thanks to you, with that option there are new doors open for me, needed a litte bit to understand it but thats great

@sea
thanks for the tip of ending with ; instead of &&. thats better now and I can write compact and better now.
And about clear, its not nessesary but its better to see only the current text and question for users.
Its more clear display and not too much code to see

@all
"...and not too much code to see"
Is it possible to hide all of the action of a code? Like silent mode or somthing like that?
the important stuff sould be shown to the user but the rest I think makes it a little bit too much for some users.

The code isnt shown to the user.
Unless you have something like:

#!/bin/bash -x
OR 
set -x

In your script.

But anyway, imagine you would like to check like 3 files.
So you would call the script passing either 3 times on file, or one time 3 files to the script.

Using clear you'd get a word printed, cleared the screen, printed a word, cleared the screen, and printed a word.
In the end, you'd see the last word checked, but not the other two, as the screen was cleared two times.

hth

Hi

@sea
Oh yes thats of course true I din't think about that, so clear isn't good for me, I think any other options arn't there so it doesn't matter then the code should better be seen.

But with the three files I don't know if its that way I will do in the end, I think over it because I am not sure like I should realice a full backupscript for users. This is only a backbone which I still improve that when I know how.

@all
Is it better for you (think as a user) when you need to bring back a backup to get automaticly the last one, or get standart option the last one, with question to get other ones?
And how long do you would hold the backups? I think aboute to delete them per script after 2 weeks to hold my system free.

---------- Post updated at 11:53 AM ---------- Previous update was at 11:37 AM ----------

@all

At this time my script is working like this:

1:
user can make backup himselfe and automaticly all 3 hours with safing db file and serverfiles on my server as default
user is asked if he wants do take a own ftp server for filebackup, if yes the ftp data are safed in a db of mine to connect everytime

2:
user get asked to get standard option (take last backup) or to take a specified one per date
if a specified he can choose the date of listing all files of last 2 weeks if not script continues
user get shown if a backup is there of (last) actuell date/time and get asked to take this or last backup
if last backup (at this moment) only takes the backup of the last safe and ask to do
then the script takes a "special" backup of the db before restoring backup, do restore and make a new backup for the user

3:
User get asked to restore files too or only db
if files too, the script save the files now on my server first before restoring, restore backup files from his/mine server,
makes a new backup and user get asked if he wants to get the special backup of these files bevor they get restored
if not he get asked if he agrees to delete this special filebackup to save space

Is this ok for you or did I forget something or do better one?

EDIT:
And is it better go make the script for user to execut like ./backup store|restore..... or as a service?