Dec 14

在 DD-WRT 下非交互式状态执行脚本时,所执行的脚本最好加上下面的内容:

export PATH=/bin:/usr/bin:/sbin:/usr/sbin:/jffs/sbin: 
/jffs/bin:/jffs/usr/sbin:/jffs/usr/bin:/mmc/sbin:/mmc/bin: 
/mmc/usr/sbin:/mmc/usr/bin:/opt/sbin:/opt/bin:/opt/usr/sbin: 
/opt/usr/bin
export LD_LIBRARY_PATH=/lib:/usr/lib:/jffs/lib:/jffs/usr/lib: 
/jffs/usr/local/lib:/mmc/lib:/mmc/usr/lib:/opt/lib:/opt/usr/lib

特别是 LD_LIBRARY_PATH 这个环境变量,要不很多脚本在 shell 下可以执行,一旦放到计划任务等非交互状态下就不能执行了,还不好调试!

Tags: ,

Aug 14

网上的都要用到 find -mtime,这个参数默认的 busybox 是不支持的,当然你可以下 findunti ,我这个脚本无需额外的程序支持:

#!/bin/sh
# optware backup
# Sun Feb 19 00:34:42 CST 2012
# by Punk

keep=3                                                #保留历史备份数
backup_source="opt"                            #需要备份的目录,不可有/开头
backup_dest="/cifs2/hdtv/!opt_bak/"      #备份文件存储目录
date=`date +%Y-%m-%d`
hostname=$(hostname -s)
prename="$hostname-optware"
filename="$prename-$date.tgz"

delete_old() {
echo Deleting old backup of Optware...
totalline=`find "$backup_dest" -name "$prename*.tgz" | wc -l`
if [ $totalline -ge $keep ];then
    dell=$(($totalline-$keep))
    find "$backup_dest" -name "$prename-*.tgz" | sort | head -n $dell | xargs rm -rf
fi
echo Deleting old backup of Optware Finished!
}

backup() {
echo "Backing Up Your Optware System..."
tar czf $backup_dest/$filename -C / $backup_source
echo "Optware Backup Finished!"
}
backup
delete_old

May 29

2015.07.03 更新,更好的支持 Tomato ARM 版本:

#!/bin/sh

#USE AT YOUR OWN RISK.
#THIS SCRIPT DOES NOT COME WITH ANY WARRANTY WHATSOEVER.
#
#Backs up selected nvram variables in "nvram export --set" format.
#
#Correctly handles multi-line entries.
#
#Thanks to ryzhov_al for basic approach.
#
#Should work equally well with both MIPS and ARM builds.
#
#Looks for a list of items to export in $etc/scriptname.ini
#OR enter items to grep for below.
#
#The items list is a list of regular expressions to match against the
#nvram variable names.
#
#Script assumes all entries are at beginning of line(prefixed with ^).
#
#Leave items list blank to backup up all of nvram.  Resulting in essentially
#the same output as MIPS "nvram export --set"
#
#The items list below is only intended as example and is not complete or
#comprehensive. Customize for your own use.
#

#Edit list below if not using .ini file, it is ignored if .ini file is found
items='
DSCP_
atm_overhead
cifs[1-2]
ctf_
ct_
dhcp_
dhcpd_
ddnsx[0-2]
dnsmasq_
dns_
https_
http_enable
http_lanport
http_wanport
http_passwd
lan_hostname
lan_ipaddr
lan_proto
modem_ipaddr
ne_
new_qoslimit_
nf_
ntp_
portforward
ppp_
pppoe_
qos_
qosl_
router_name
rrule[0-9]
cstats_
rstats_
script_
smbd_
sch_
sshd_eas
sshd_forwarding
sshd_motd
sshd_pass
sshd_port
sshd_remote
sshd_rport
tm_
tomatoanon_
usb_
upnp_
wan_dns
wan_proto
wan_hostname
wan_domain
wan_hwaddr
wan_mtu
web_css
web_mx
wl[0-9]_security_mode
wl[0-9]_ssid
wl[0-9]_wpa_psk
'

etc=/opt/etc
base=${0##*/}; base=${base%.*}
config=$etc/$base.ini

#file to output - default to stdout
filename="$1"
curr_date=$(date +"%Y-%m-%d")
if [ $# -eq 0 -o "$filename" = "" ]; then
    backupfile="/opt/etc/init.d/nvram_backup-${curr_date}.txt"
else
    backupfile="/opt/etc/init.d/${filename}-${curr_date}.txt"
fi

grepstr=$( { [ -r $config ] && cat $config || echo "$items" ; } | sed -e 's/[t ]//g;/^$/d' | sed ':a;N;$!ba;s/n/\|^/g')

{
echo "#Exporting $grepstr"
for item in $(nvram show 2>/dev/null | grep "^.*=" | grep "$grepstr" | grep -v "hwaddr" | awk -F= "{print $1}" | sort -u)
do
  item_value="$(nvram get $item | sed 's!([$\"`])!\1!g'; echo nvgetwasnull)"
  case $item_value in
  nvgetwasnull) ;;
  *) echo "nvram set ${item}=\"${item_value%
nvgetwasnull}\"" ;;
  esac
done
}>"$backupfile"

比较完美的解决了多行文本框内容的备份,例如脚本等处。

#!/bin/sh

#
#USE AT YOUR OWN RISK.
#THIS SCRIPT DOES NOT COME WITH ANY WARRANTY WHATSOEVER.
#

#file to output
backupfile='/mnt/optware/opt/etc/init.d/nvram_restore.sh'

#Enter strings to grep for here.
#
#Use heredocs to build grep from one entry per
#line for ease of maintenance.
#
#Could easily be changed to read external config file.
#
#sed is not my best skill - there may well be cleaner syntax, and
#admittedly this is some quick cut and paste from another script
grepstr=$(cat << EOF |  sed -e 's/[t ]//g;/^$/d' | sed ':a;N;$!ba;s/n/\|/g'
^dhcp_
^dhcpd_
^ddnsx
^dnsmasq
^dns_
^https_
^http_passwd
^lan_hostname
^lan_ipaddr
^wan_dns
^wan_proto
^wan_hostname
^wan_domain
^wan_hwaddr
^router_name
^modem_ipaddr
^rrule1
^cstats_
^rstats_
^qos
^new_qoslimit_
^ctf_
^ct_
^bt_
^ne_
^nf_
^ntp_
^smbd_
^usb_
^upnp_
^script_
^wl[0-9]_security_mode
^wl[0-9]_ssid
^wl[0-9]_wpa_psk
^cifs[0-9]
EOF
)

#uncomment next line if you want to see what above creates
#echo $grepstr
[ -e $backupfile ] && rm $backupfile

echo "echo Restoring NVRAM settings" > $backupfile
echo "echo Note: This will overwrite your current NVRAM settings in NVRAM," >> $backupfile
echo "echo but they will not be permanent unless you do a 'nvram commit'" >> $backupfile

echo "echo Restore NVRAM rules..." >> $backupfile
echo Exporting NVRAM rules to $backupfile

#Below mostly from ryzhov_al
#sed to escape chars is mine, may not be complete.

for item in $(nvram show | grep "$grepstr"  | awk -F= "{print $1}")
do
    item_value=$(nvram get ${item} | sed 's!([$\"`])!\1!g')
    echo "nvram set ${item}=\"$item_value\"" >> $backupfile
done

echo "echo Restore of NVRAM settings complete" >> $backupfile
echo "echo To make them permanent, do: nvram commit" >> $backupfile

chmod +x $backupfile

echo NVRAM settings have been exported to $backupfile
echo Now feel free to try new settings
echo To restore from backup, type: $backupfile

感谢 linksysinfo 的高手们!


Oct 06

Script Converter是个很简单的工具,就是把HTML代码转成PHP, ASP, JSP, Perl, Python, VBScript输出的代码,很方便,不用小心翼翼那些引号了...

Script Converter 1.0是免费软件,下载中包含了安装版和HTML离线版。


Sep 22

如果以前我知道这个脚本,可能我以前的数据就不会丢得那么冤枉了。由于以前用数据库保存了一些数据文件,所以数据库非常大(50+M),本地导入都长期超时。而BigDump就是用来解决这个问题的。

使用:

  1. 下载解压出bigdump.php,用你的编辑器编辑数据库连接参数,就是用户名,密码,表那些;
  2. 删除目标数据库的旧表,或者在用phpMyAdmin导出数据时选‘Add DROP TABLE’, ‘Add IF NOT EXISTS’,这样在导入数据时就会覆盖目标表;
  3. 在服务器上建立一个工作目录,例如:dump;
  4. 如果你想通过服务器的HTTP方式上传数据库,请把dump目录的权限设为777(基于Linux的服务器 chmod 777),不过这样通常会受到PHP配置上传文件大小的限制,最好还是用FTP上传;
  5. 上传bigdump.php和你的数据库文件(*.sql或者*.gz)到dump目录,记得如果是.gz后缀的数据库要用BINARY模式上传;
  6. 在浏览器运行bigdump.php脚本,例如:http://www.yourdomain.com/dump/bigdump.php,这时会有列表让你选择dump目录下的数据库文件;
  7. BigDump会自动更新显示导入的过程,只要你没关闭浏览器的Javascript支持,给点耐心等待导入的完成,不要关闭当前窗口!
  8. 导入成功后,记得删除bigdump.php文件和你的数据库!

中文用户可能需要调整 bigdump.php 文件中的 $db_connection_char_set 参数符合自己的数据库,不至于导入后乱码。

BigDump的作者是Alexey Ozerov,您可以去官方下载这个PHP脚本的最新版本,也可以在本地下载0.28版。