Showing posts with label scripting. Show all posts
Showing posts with label scripting. Show all posts

Thursday, May 10, 2012

Dos Batch

DOS Batch file to delete folders by date range
@Echo Off
Setlocal EnableDelayedExpansion
:: User Variables
:: Set this to the number of days you want to keep
Set _DaysKept=7
:: Set this to the folder that contains the folders to check and delete
Set _Path=C:\Test1
:: Get todays date
Call :GetDate
Set _yy=%_fDate:~,4%
Set _mm=%_fDate:~4,2%
Set _dd=%_fDate:~6,2%
:: Convert todays date to Julian
Call :JDate %_yy% %_mm% %_dd%
Set _JToday=%_JDate%
:: Set delete date
Set /a _DelDate=_JToday-%_DaysKept%-1
:: Get time format, _iTime will be 0 if 12 hourt clock, 1 if 24 hour clock
:: Delims= is a TAB followed by a space in the next line
:: If you copy this code, you must edit this line
For /F "TOKENS=2* DELIMS=     " %%A In ('REG QUERY "HKCU\Control Panel\International" /v iTime') Do Set _iTime=%%B
If Exist "%temp%\tf}1{" Del "%temp%\tf}1{"
PushD %_Path%
Set _s=s
If %_DaysKept%==1 set _s=
Echo Please wait, searching for folders more than %_DaysKept% day%_s% old
If %_iTime%==0 (Set _Tok=1,4*) Else (Set _Tok=1,3*)
For /F "tokens=%_Tok% skip=4" %%I In ('dir "%_Path%\1*" /AD /OD /TW ^|Findstr /E "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]"') Do (
Set _Name=%%K
  If "!_Name:~10!"=="" If %%J==^ (
    Call :GetDate %%I
    Call :JDate !_fDate:~,4! !_fDate:~4,2! !_fDate:~6,2!
    If !_JDate! LEQ %_DelDate% (
      If Exist "%temp%\tf}1{" (
        Echo %%I:%%~fK >>"%temp%\tf}1{"
        ) Else (
        Echo.>"%temp%\tf}1{"
        Echo Do you wish to delete the following folders?>>"%temp%\tf}1{"
        Echo Date       Name>>"%temp%\tf}1{"
        Echo %%I:%%~fK >>"%temp%\tf}1{"
      )) Else (
      Goto :_allFound
      )))
PopD
:_allFound
If Not Exist "%temp%\tf}1{" Echo No Folders Found to delete&Goto _Done
Type "%temp%\tf}1{" | More
Set _rdflag= /q
:_Prompt1
Set /P _resp=Delete All, None, or Prompt for each (A/N/P)?
If /I "%_resp:~0,1%"=="N" Goto _Done
If /I "%_resp:~0,1%"=="A" Goto _Removeold
If /I NOT "%_resp:~0,1%"=="P" (Echo (A/N/P only please)&Goto _Prompt1
Set _rdflag=
:_Removeold
For /F "tokens=1* skip=3 Delims=:" %%I In ('type "%temp%\tf}1{"') Do (
 If "%_rdflag%"=="" Echo Deleting
 rd /s%_rdflag% "%%J")
:_Done
If Exist "%temp%\tf}1{" Del "%temp%\tf}1{"
Goto:EOF
::===================================::
::                                         ::
::   -   S u b r o u t i n e s   -   ::
::                                         ::
::===================================::
:JDate
:: Convert date to Julian
:: Arguments : YYYY MM DD
:: Returns   : Julian date in variable _JDate
:: Usage
::Call :JDate %__GYear% %_GMonth% %_GDay%
:: First strip leading zeroes; a logical error in this
:: routine was corrected with help from Alexander Shapiro
::Code taken from datediff.bat written by Rob van der Woude
::http://www.robvanderwoude.com
Set _JMM=%2
Set _JDD=%3
IF 1%_JMM% LSS 110 Set _JMM=%_JMM:~1%
IF 1%_JDD% LSS 110 Set _JDD=%_JDD:~1%
::
:: Algorithm based on Fliegel-Van Flandern
:: algorithm from the Astronomical Almanac,
:: provided by Doctor Fenton on the Math Forum
:: (http://mathforum.org/library/drmath/view/51907.html),
:: and converted to batch code by Ron Bakowski.
Set /A _JMonth1 = ( %_JMM% - 14 ) / 12
Set /A _JYear1  = %1 + 4800
Set /A _JDate  = 1461 * ( %_JYear1% + %_JMonth1% ) / 4 + 367 * ( %_JMM% - 2 -12 * %_JMonth1% ) / 12 - ( 3 * ( ( %_JYear1% + %_JMonth1% + 100 ) /

100 ) ) / 4 + %_JDD% - 32075
For %%A In (_JMonth1 _JYear1) Do Set %%A=
Goto:EOF
:GetDate
:: This subroutine will always display the same results,
:: for the date independent of "International" settings.
:: This batch file uses REG.EXE from the NT Resource Kit
:: (already installed with WinXP and Vista)
:: to read the "International" settings from the registry.
:: Date is returned as yyyymmdd in variable _fdate
:: Modified from SortDate Written by Rob van der Woude
:: http://www.robvanderwoude.com
::
If NOT [%1]==[] Set Date=%1
If "%date%A" LSS "A" (Set _NumTok=1-3) Else (Set _NumTok=2-4)
:: Delims= is a TAB followed by a space in the next two lines
:: If you copy this code, you must edit these two lines
For /F "SKIP=3 TOKENS=2* DELIMS=     " %%A In ('REG QUERY "HKCU\Control Panel\International" /v iDate') Do Set _iDate=%%B
For /F "SKIP=3 TOKENS=2* DELIMS=     " %%A In ('REG QUERY "HKCU\Control Panel\International" /v sDate') Do Set _sDate=%%B
IF %_iDate%==0 For /F "TOKENS=%_NumTok% DELIMS=%_sDate% " %%B In ("%date%") Do Set _fdate=%%D%%B%%C
IF %_iDate%==1 For /F "TOKENS=%_NumTok% DELIMS=%_sDate% " %%B In ("%date%") Do Set _fdate=%%D%%C%%B
IF %_iDate%==2 For /F "TOKENS=%_NumTok% DELIMS=%_sDate% " %%B In ("%date%") Do Set _fdate=%%B%%C%%D
Goto:EOF
FTP Batch
FTP -v -i -s:ftpscript.txt

open example.com
username
password
!:--- FTP commands below here ---
lcd c:\MyLocalDirectory
cd  public_html/MyRemoteDirectory
binary
mput "*.*"
disconnect
bye

Thursday, May 3, 2012

bash scripting

daily samba backup using crontab
#!/bin/bash
# backup sun01 server : /home/samba_folder 
echo `date` 'backup.sh: START' > /tmp/cron.log
cd /
DATESTAMP=`date +%F`
LIST="/tmp/backuplist_$$.txt"
SOURCE_PATH="/home/samba_folder"
DESTINATION_PATH="/media/backup"
OLDBACKUP_PATH="/home/oldbackup"

#mount -n -t cifs -o rw,username=,password= $DESTINATION_PATH

sleep 5
cat /dev/null > /tmp/bcksun01.out
find /tmp/*.ods  -cmin +200 -type f -exec rm -f '{}' \; 2>>/dev/null
find /tmp/*.csv  -cmin +200 -type f -exec rm -f '{}' \; 2>>/dev/null

#find /home/backup/*.gz -mtime +60 -type f -exec rm -f '{}' \;

set $(date +%d)
if test "$1" = "03" ; then
#   rm -f $OLDBACKUP/sun01.admin.full.tar.gz 2>>/dev/null
#   tar cvfz "$OLDBACKUP/sun01.admin.full.tar.gz" $SOURCE_PATH/admin  2>>/tmp/cron.log >>/tmp/backup.log
   rm -f $OLDBACKUP/sun01.samba_folder.full.tar.gz 2>>/dev/null
   tar cvfz "$OLDBACKUP/sun01.samba_folder.full.tar.gz" $SOURCE_PATH  2>>/tmp/cron.log >>/tmp/backup.log
else
   rm -f $OLDBACKUP/sun01.samba_folder.$1.tar.gz 2>>/dev/null
   find $SOURCE_PATH -depth -type f \( -mtime -2 \) -print > $LIST
   tar cvfzT "$OLDBACKUP/sun01.samba_folder.$1.tar.gz" $LIST 2>>/tmp/cron.log >/tmp/backup.log
   rm -f "$LIST"
fi
  
sleep 1
#umount $DESTINATION_PATH/tn_sql
history -c
echo `date` 'backup.sh: Completed' >> /tmp/cron.log
other backup sample
# use backticks " ` ` " to execute shell command

 # TARIKH=`date +"%Y%m%d_%s"`

mount -t smbfs //131.107.2.20/volume_1/  /home/vol1 -o username=adabi,password=akula
mount -t smbfs //131.107.2.20/volume_2/  /home/vol2 -o username=adabi,password=akula
TARIKH=`date +"%Y-%m-%d"`
TARIKH2=`date +"%Y%m%d"`
TARIKH3=`date +"%Y%m"`
DAY0=`date +"%d"`
WDAY0=`date +"%u"`
YDATE=`date -d '1 day ago' +'%Y%m%d'`
UPDSCRIPT=update$TARIKH.sh
PATCHSCRIPT=patch$TARIKH.sh
SENARAI=/tmp/bcklist.txt
SENARAIF=/tmp/fbcklist.txt

BCK_TNLINUX3=/home/vol1/tnlinux3
echo ${BCK_TNLINUX3}'/tnlinux3.full.tar.gz' > ${SENARAIF}
echo ${BCK_TNLINUX3}'/tnlinux3.artwork.full.tar.gz' >> ${SENARAIF}
echo ${BCK_TNLINUX3}'/tnlinux3.samba.administrator.full.tar.gz' >> ${SENARAIF}
echo ${BCK_TNLINUX3}'/tnlinux3.samba.customer1.full.tar.gz' >> ${SENARAIF}



yesterday date:

date -d '1 day ago' +'%Y-%m-%d'

or

date -d @`echo $((\`date +%s\` – 86400))` +”%Y-%m-%d”
MYSQL :: SQL LEFT JOIN Syntax
SELECT column_name(s)
FROM table_name1
LEFT JOIN table_name2
ON table_name1.column_name=table_name2.column_name

Saturday, April 28, 2012

mysql command

dump data without creating table with condition :
mysqldump -t --max_allowed_packet=1G  --host=ip.add -uuser -ppassword database table --where=sys_date>='2012-02-01'  > /tmp/1.sql

dump data & table :

mysqldump --host=www.pembeda.com -uuser -ppassword database table > /tmp/1.sql

dump data & whole database:

mysqldump --host=www.pembeda.com -uuser -ppassword database > /tmp/1.sql


restore :

mysql -uuser -ppassword
mysql> use database_name
mysql> source /tmp/1.sql
mysql> exit;