Mam skrypt który kopiuje pliki utworzone do danego katalogu po określonym czasie.
Chciałbym aby skrypt ten przed spakowaniem pliku dopisał do niego w jakim czasie został dany plik utworzony.
Skrypt teraz pakuje plik tak:
[INDENT]bf2-1105011744-de_rats_1337.tar.bz2
bf2 - moja nazwa
110501 - 2011.05.01
1744 - 17:44
de_rats-1337 - takie tam[/INDENT]
Obecnie mogę odczytać w jakim momencie ten plik został utworzony, tylko ten plik przez 25 minut jest nadpisywany, bo jest to aplikacja, która nagrywa co się dzieje na serwerze gry.
Chciałbym otrzymać taką nazwę:
Kod: Zaznacz cały
bf2-110501[B]1744[/B]-110501[B]1759[/B]_de_rats_1337.tar.bz2
Czyli po prostu dopisać do skryptu taką funkcję aby zmieniła mi nazwę pliku przed spakowaniem na taką, w której będzie godzina utworzenia pliku według powyższego schematu.
Ponieważ, ja jestem totalna noga z basha proszę was raczej o gotowca.
Wiem, że to głupi brzmi, ale naprawdę nie mam pojęcia zielonego jak to zrobić.
A skrypt niżej:
http://wklejto.pl/96583
Kod: Zaznacz cały
#!/bin/bash
#
# _KaszpiR_ [email protected]
# Script to find and pack HLTV demos or HLDS logs or eggdrop logs
# if you want to change it to something else you must edit awk lines where the year/month/day is extracted from filename
#
# run program with any parameter to see the list of files that will be processed
#
# NOTICE: demos must have prefix in the name, for example hltv-1101201319-de_dust.dem
# so the prefix is hltv (refer to hltv configuration)
# otherwise the script will produce werid directories. (will fix it later)
#
#
# Changelog:
# ver 1.3 2011.01.20, 15:14
# + added variable to make deleting source files after packing an option
# + added basic checks to avoid error configurations, should be really more
# + running script with any parameter will show command and will require pressing any key to continue
# + added quotation marks in some places to allow specific name patterns
# ver 1.2 2011.01.16, 15:15
# + added ftp upload
# + all variables curly brackets for security reasons
# + listing old files is potional
# + added hashes on output lines for easier parsing
# ver 1.1 2009.08.31
# + added option to list directories for deletion older than X days
# + added definiton for the extension files
##################################################################
WORKDIR="/home/HLTV/bf2"; # enter this directory when executing any commands and creating TMPFILE
SRCDIR="/home/HLTV/bf2/cstrike/"; # source directory where to find files for packing
DSTDIR="/var/www/HLTV/BF2/"; # destination directory where to put packed files
NAMEPATTERN="BF2*.dem"; # file names to search when finding files
EXTENSION=".dem"; # remove this extension when creating nicely named archives, should be the same as used in NAMEPATTERN, can be empty
# for bzip2 packing try those
PACKER=`which tar`; # what packing program to use
PACK_OPT="-Pjcf"; # packing program options, refer to packer manual
PACK_EXT="tar.bz2"; # extension to use when creating archives
# example for 7zip
#PACKER=`which /usr/bin/7z`; # what packing program to use
#PACK_OPT="a -t7z -m0=lzma -mx=9 -mfb=64 -md=32m -ms=on"; # packing program options
#PACK_EXT="7z"; # extension to use when creating archives
TMPFILE=".pack_211.tmp"; # name of the temporary file to store names of the processed files
LOGFILE="pack_211.log"; # file to log actions
SILENT=0; # do not show some messages (to decrease the produced crap in crontab results via mail
NICE=20 ; # the niceness level, 20 = lowest, do it when cpu is idle , 0 = highest, do it as fast as possible by current user
DELETE=1; # should we delete the source file after packing it?
LIST_OLD=1; #list old already packed files, useful for scripts
LIST_OLD_FILE="old.txt"; # destination where to print old packed file list
DELETE_OLD=0 ; # defines if the script should delete old already packed files
OLDER_THAN=7 ; # 14 days
FTP_USE=0; # use ftp to upload files somwhere after packing them
FTP_DEBUG=0; # generate ftp transfer log with debug messages
FTP_LOG="ftp_211.log";
FTP_CLIENT=`which ncftpput`; # program to run ncftpput (part of ncftp packagae)
FTP_PATH="/"; # remore directory where to store logs
FTP_TRIES=10;
FTP_OPTS="-S .tmp";# extra ftp upload options, refer to manual
FTP_CREDENTIALS="${WORKDIR}/.secret"; # ncftp credentials file in format, refer to ncftpput -f parameter, read the ncftp manual, make sure this file is chmod 600
#
# format of the FTP_CREDENTIALS file (plaintext, three lines)
# host some.host.com
# user lamer
# pass dumbpassword
#
# end of conig
##################################################################
# Start of sript
LC_ALL=C
date >> ${LOGFILE}
echo "# ================"
# sanity checks , very basic ones
if [ ! -d ${WORKDIR} ]; then
echo "ERROR: WORKDIR: ${WORKDIR} is not a directory.";
exit 1;
fi
if [ ! -d ${SRCDIR} ]; then
echo "ERROR: SRCDIR: ${SRCDIR} is not a directory.";
exit 1;
fi
if [ ! -d ${DSTDIR} ]; then
echo "ERROR: DSTDIR: ${DSTDIR} is not a directory.";
exit 1;
fi
if [ ! -w ${DSTDIR} ]; then
echo "ERROR: DSTDIR: ${DSTDIR} is not writable.";
exit 1;
fi
if [ "${PACKER}" == "" ]; then
echo "ERROR: PACKER: is not found, check if you got apropiate packer program installed."
exit 1;
fi
if [ "${FTP_USE}" == "1" ]; then
if [ ! -r ${FTP_CREDENTIALS} ]; then
echo "ERROR: FTP_CREDENTIALS: ${FTP_CREDENTIALS} is not found, or is not readable."
exit 1;
fi
if [ "${FTP_CLIEN}" == "" ]; then
echo "ERROR: FTP_CLIEN: is not found, check if you got apropiate packer program installed."
exit 1;
fi
fi
cd ${WORKDIR}
rm -f ${TMPFILE}
##################################################################
#this is just for checking what the script will do , or debug mode
if [ $1 ]; then
echo "find ${SRCDIR} -maxdepth 1 -type f -name \"${NAMEPATTERN}\" -mmin +60"
pause
find ${SRCDIR} -maxdepth 1 -type f -name "${NAMEPATTERN}" -mmin +60
exit 0
fi
##################################################################
# real run
#find all files modifed over hour ago, so we gonna ignore the currently recorded demo
find ${SRCDIR} -maxdepth 1 -type f -name "${NAMEPATTERN}" -mmin +60 > ${TMPFILE}
OYEAR=""
OMONTH=""
ODAY=""
TOTAL=`wc -l ${TMPFILE}|awk '{print $1}'`
echo "# ${TOTAL} files."
INCR=0;
for f in `cat ${TMPFILE}` ; do
# display enumerator on screen, for better view if the progress is made
INCR=$((INCR + 1))
if [ ${SILENT} -lt 1 ]; then
echo "# ${INCR} / ${TOTAL}"
# fi
# split filename to daytime format, and used to produce subdirectories and pack files into the proper dir
# YEAR=`echo ${f} | awk -F- '{print substr($2,1,4)}'`
# MONTH=`echo ${f} | awk -F- '{print substr($2,5,2)}'`
# DAY=`echo ${f} | awk -F- '{print substr($2,7,2)}'`
# if [ "${DAY}" != "${ODAY}" ];then
# detect the day change and make new subdirectory
FULLDATE="${YEAR}/${MONTH}/${DAY}"
mkdir -p ${DSTDIR}/${FULLDATE}
ODAY=${DAY}
fi
SUBNAME=`basename ${f} ${EXTENSION}`
# run with lowest priority to avoid cpu usage
nice -n${NICE} ${PACKER} ${PACK_OPT} ${DSTDIR}/${FULLDATE}/${SUBNAME}.${PACK_EXT} ${f} >> ${LOGFILE} 2>&1
if [ $? -eq 0 ]; then
# packing completed, delete the source file if needed
if [ "${DELETE}" == "1" ];then
rm -f ${f}
fi
else
#compression failed
if [ "${DELETE}" == "1" ];then
#packing failed, we leave the source file as it was
echo "# WARNING: not deleting due to compression error: ${f}"
else
echo "# WARNING: compression error: ${f}"
fi
fi
done
##################################################################
#Listing old files
if [ ${LIST_OLD} == "1" ]; then
echo "# Listing old files:"
find ${DSTDIR} -mindepth 3 -type d -daystart -mtime +${OLDER_THAN} > ${LIST_OLD_FILE}
#find ${DSTDIR} -mindepth 3 -type d -daystart -mtime +${OLDER_THAN}
# the above line was not tested
fi
##################################################################
# Deleting old files
if [ ${DELETE_OLD} == "1" ]; then
echo "# Deleting old files:"
#echo "find ${DSTDIR} -mindepth 3 -type d -daystart -mtime +${OLDER_THAN} -print0 | xargs -0 rm -rf"
find ${DSTDIR} -mindepth 3 -type d -daystart -mtime +${OLDER_THAN} -print0 | xargs -0 rm -rf
fi
##################################################################
# ftp without removing old files
# -R recursive
# -z try to resume broken uploads
if [ ${FTP_USE} == "1" ]; then
echo "# FTP process starts:"
if [ ${FTP_DEBUG} == "1" ]; then
# -d file , generate debug log
${FTP_CLIENT} ${FTP_OPTS} -d ${FTP_LOG} -f ${FTP_CREDENTIALS} -r ${FTP_TRIES} -z -R ${FTP_PATH} ${DSTDIR}/
else
# no log
# -DD delete files after successful uploading
${FTP_CLIENT} ${FTP_OPTS} -f ${FTP_CREDENTIALS} -r ${FTP_TRIES} -z -DD -R ${FTP_PATH} ${DSTDIR}/
fi
echo "# FTP process ends."
fi
# removing old files
#
# get recursively directories , check date and rape
# execute rm based on local directory structure
#
#
##################################################################
# End of script
echo "# ================="
##################################################################