#!/bin/sh # # MiniTar (extraction & compression) utility - minitar.cgi ver='0.05b' # # This script works as a CGI and also as a command line tool. # It extract/compress files and directories. # Written in almost pure Shellscript. # # When invoked as a CGI, it shows simple form to input parameters. # This script accepts both GET and POST method. # # Installation: # # Simply, copy this file to a well protected directory as 'minitar.cgi' # and set permission to 700 (or 755 in non-SuExec environment). # No editing required (unless mysql commands are in non-standard path). # Juse make sure to protect it at least with password (for CGI) # # Usage: # # As a CGI: Access https://example.com/safedir/minitar.cgi # and enter parameters in the form. # # # WARNING: Although I've tested many times, this script is still under testing # and you should keep a backup of your archive or file/dirs. # # Latest version will be available at: # http://check-these.info/MiniTar.html # http://check-these.info/tools/minitar_cgi.txt # # Note: I made this out of MiniSqlDump, removing MySQl related part, # and modifying texts/options. # http://check-these.info/MiniSqlDump.html # CHECK='http://check-these.info' DATE=`date "+%y-%m-%d"` ####### Config section ########## ps='ps uxww' up='uptime' ####### END config ######### waitbg () { CC=''; CCC='0' while :;do CC="${CC}x"; CCC=$(( $CCC + 1 )) case `jobs` in '') break;; esac sleep 3; echo -n '.' case "$CC" in "xxxxxxxxxx") CC=''; echo;;esac case "$CCC" in "$Max") echo -e "$Mess"; kill $!;Killed='1'; return;; esac done } ######## compress function ########## compress () { echo -e "compress: f='$f', p='$p', t='$t', o='$o', opt='$opt'" if [ -n "$p" -a "$p" != '.' ];then if cd $p ;then echo "Changed current dir to '$p'"; else return 3;fi fi ff="$f" case "$f" in *.tar.gz|*.tar.bz2|*.zip|*.tar|*.gz|*.tgz|*.bz2|*.tbz2) ff="${f%.*}" warn1="### Warning: only the first target '${t%%\ *}' will be compressed.\n### Use tar.gz, tar,bz2 (or tgz, tbz2) for multiple targets." case "$ff" in *.tar) ff="${ff%.*}";; esac case "$f" in *.zip) if [ -f "$f" ]; then rm "$f";fi cm="zip $opt -r \"$ff\" $t";; *.tar.gz|*.tgz) cm="tar $opt czvf \"$f\" $t" ;; *.tar.bz2|*.tbz2) cm="tar $opt cjvf \"$f\" $t";; *.gz) case "$t" in *' '*) echo -e "$warn1";;esac cm="gzip $opt -c -f ${t%%\ *} >\"$f\"";; *.bz2) case "$t" in *' '*) echo -e "$warn1";;esac cm="bzip2 $opt -c -f ${t%%\ *} >\"$f\"";; esac echo -e "Compressing ....\nCommand: $cm" if [ -n "$AS_CGI" ];then cm="$cm &";eval "$cm" Max='300'; Mess="Error: compression process running too long. Terminating.\n" waitbg; case "$Killed" in '1') return 2;;esac else eval "$cm" fi case "$o" in *r*) rm -rf $t ;;esac ;; *) echo -e "Error: the extension not supported '$f'\n"; return 1;; esac echo "Done.";ls -al "$f"; return } ######## extract function ########## extract () { echo -e "extract f='$f', p='$p', t='$t', o='$o', opt='$opt'" if [ -n "$p" -a "$p" != '.' ];then if cd $p ;then echo "Changed current dir to '$p'"; else if mkdir "$p" ;then echo "'$p' created"; cd "$p" && echo "Changed current dir to '$p'" || return 4 else return 3 fi fi fi case "$f" in *.tar.gz|*.tar.bz2|*.zip|*.gz|*.tgz|*.bz2|*.tbz2) echo "Extracting from '$f' ...";; *) echo "Wrong file type '$f'";return;; esac case "$f" in *.zip) unzip -o "$f" $t;; *.tar) tar xvf "$f" $t;; *.tar.gz|*.tgz) tar xzvf "$f" $t;; *.gz) ff=`gunzip -l "$f"`; ff=${ff##*\ };gunzip -c $f >$ff ;; *.tar.bz2|*.tbz2) tar xjvf "$f";; *.bz2) ff=`bunzip2 -l "$f"`;ff=${ff##*\ };bunzip2 -c $f >$ff ;; esac case "$o" in *r*) rm -rf "$f" ;;esac #echo "DBG ff=$ff"; echo "Done." return } ####### helper function(s) ####### updatenow () { # Install/update function exec 2>&1 case "$UF" in *_[pc][hg][pi].txt) RF="${UF%_*}.${UF##*_}" RF="${RF%.txt}";; *)RF="$UF";;esac if [ -f "$RF" ];then if [ ! -w "$RF" ];then echo "Error: The file: $RF isn't writalbe. Please change the permission to 700 (or 755)"; if [ -n "$AS_CGI" ];then return;else exit;fi fi cp -vf "$RF" "$RF.$DATE" fi echo "Downloading current version ...." wget -t 1 -nd "http://serv.check-these.info/tools/$UF" && sleep 2 && chmod 700 "$UF" && mv -vf "$UF" "$RF" && echo "Update done" || echo "Update failed. Please retry later if it was downloading problem. If the problem persists, contact author: minisqldump (atmark) check-these.info "; case "$RF" in *.cgi) export SCRIPT_FILENAME=""; ./$RF -V ;;esac if [ -n "$AS_CGI" ];then echo '
"
AS_CGI=1
exec 2>&1
# Get query data from QUERY_STRING (GET method) or stdin (POST method)
case "$REQUEST_METHOD" in
# [pP][oO][sS][tT]) while : ;do read -r post || break;qs="$qs$post";done;;
# Read only one line
[pP][oO][sS][tT]) read -r qs;;
*) qs="$QUERY_STRING";;
esac
qsl="${#qs}"
C=0
qss="$qs"
while : ;do # Primitive form parsing. No special chars treated
C=$(($C + 1))
pair="${qs%%[&]*}"; qsp="$qs";qs="${qs#*[&]}"
id="${pair%%=*}"; v="${pair#*=}"
#echo -e "DBG $C:\nqss=$qss\nqs=$qs\npair=$pair\nid=$id\nv=$v"
case "$id" in
'');;
*[!0-9a-zA-Z_-.]*) echo "Error:
You can't use special characters in parameter name '$id'";exit 3;;
"$pair") eval "$id=''";;
*) eval "$id='$v'";;
esac
case "$qs" in ''|\&|"$qsp") break;;esac
case "$C" in '50') echo "Error: Max loop reached $C";exit 2;;esac
done
case "$c" in "w") UF="$fn"; updatenow;qsl=0;;esac
case "$m" in 1) m1="$m0";m0='';; esac
case "$res" in 1) res0="$r0";; esac
case "$r" in 1|[cC]) cmd="$compr";r1="$r0";r0='' 2>&1;; esac
# Replace %20 and + in $t with a space.
while :;do case $t in *%20*)t="${t%%[%]20*} ${t#*[%]20}";; *)break;;esac;done
while : ;do case $t in *+*) t="${t%%+*} ${t#*+}";; *)break;; esac;done
host=`hostname`
case "$host" in *powweb*) pow=1;; *) ps='' ;;esac
else ### Invoked as a command ###
pssave="$ps";ps=''
case "$#" in '0') usage;exit;;esac
while :; do case "$1" in #Check and consume options
'-res') shift; res0="$r0";res=1;;
'-e') ;;
'-c') cmd="$compr"; shift; r1="$r0";r0='';r=1;;
'-ps') shift;ps="$pssave";; # run ps. ps may reaveal the password
'-nops') shift;ps='';; # Don't run ps. (Default in the command line mode)
'-noup') shift;up='';; # Don't run uptime.
'-V'|'--version') shift; echo $version;exit;;
'-h'|'--help') shift;usage;exit;;
'-x'|'--extensive') shift;usage;echo;grep '^#\( \|$\)' $0;exit;;
'-w'|'--update') updatenow;exit;; # Get the new version. Auto update.
'-'?*) echo -e "Error: unknown option:$1\n";usage;exit;;
*) break;;
esac;done
#echo "1=$1, 2=$2, 3=$3, 4=$4, 5=$5, 6=$6, 7=$7, 8=$8, 9=$9"
qsl="$#"
f="$1"
shift && p="$1" && shift &&
while : ;do
case "$1" in '') break;; *) t="$t $1";shift || break;;esac
done &&
case "$t" in " "*) t="${t#?}";;esac
#echo "1=$1, 2=$2, 3=$3, 4=$4, 5=$5, 6=$6, 7=$7, 8=$8, 9=$9"
fi
#echo -e "DBG:f='$f', p='$p', r='$r' t='$t'"
# Check parameters, use the default if it's defined
if [ "$qsl" != "0" ];then
if [ -z "$f" ];then
echo -e "Error: archive filename not specified
f='$f', p='$p', r='$r' t='$t'"
else
if [ -z "$t" -a "$r" != "0" ];then
echo -e "Error: target file(s)/dir(s) not specified
f='$f', p='$p', r='$r'"
else
fs="$f"
case "$f" in /*) ;; *) f="`pwd`/$f";;esac
hostname;$up; $cmd; $up; $ps; # Execut the command
f="$fs"
fi
fi
fi
# Input form (if invoked as a CGI). Modify as you wish.
if [ -n "$AS_CGI" ];then
if [ "$qsl" != "0" ];then echo "
";fi
echo "
To extract:
Simply enter the file/path of the archive, select 'Extract'
and push 'Execute' button.
Optionally, you can specify different directory (Target path),
and the list of files/dirs you want to extract from the archive, too.
The contents of archive will be extracted in the target path
instead of under current directory.
If the target path specified doesn't exist, it will be created.
To compress:
You MUST enter the name/path of archive and the list of files/dirs.
You can specify the target path to create an archive that has
relative path structure.
ex. Target path: /www/U/USER/htdocs
Target files/dirs: subdir1 subdir2
==> Archive will have 'subdir1 subdir2' in main entry,
and you can extract them in different target path, if you wish.
You can use wild card * in the files/dirs list.
But becareful not to create huge archive.
Also, this utility will stop operating after certain time to avoid
more serious problems.
WARNING: Treating a big archive is very intensive task.
Your site may get shut down due to resource abuse.
Test first with a smaller example.