2010年4月30日 星期五

mysql匯入/匯出

備份及匯入 mysql database
備份 SQL data:
>mysqldump -u 使用者名稱 -p –default-character-set=utf8 資料庫名稱 > backup.sql

匯入 SQL data:
>mysql -u 使用者名稱 -p 資料庫名稱 < backup.sql 備份及匯入 mysql database
備份 SQL data:
>mysqldump -u 使用者名稱 -p –default-character-set=utf8 資料庫名稱 > backup.sql

匯入 SQL data:
>mysql -u 使用者名稱 -p 資料庫名稱 < backup.sql

2010年4月22日 星期四

How to use ftp in a shell script

公司內部在用的備份機制原本用wput來上傳的
但無奈換個ftp server後,就一直無法順利上傳
研判應該是wput無法針對新版的ftp server正確使用PASV模式
於是只好改用shell script直接上傳
下面的資料摘自How to use ftp in a shell script


Example (non-working) script

#!/bin/sh
HOST='ftp.users.qwest.net'
USER='yourid'
PASSWD='yourpw'
FILE='file.txt'

ftp $HOST <<END_SCRIPT
user $USER
$PASSWD
put $FILE
quit
END_SCRIPT
exit 0

The above script will just hang if run in the foreground (in an xterm), or if run in the background (from a cron job), it will fail to perform the work of transferring file.txt.

/dev/tty names a strange, magic device. Each process (more strictly each process group) has a different /dev/tty, and you can not naively make ftp clients read the password from some non-magic, yet convenient source, like a "here document". When run in an xterm, the script above appears to hang because it reads the password from /dev/tty. The xterm constitutes the script's /dev/tty, so the script waits for keyboard input.

Example Working Script

#!/bin/sh
HOST='ftp.users.qwest.net'
USER='yourid'
PASSWD='yourpw'
FILE='file.txt'

ftp -n $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
put $FILE
quit
END_SCRIPT
exit 0

公司內部在用的備份機制原本用wput來上傳的
但無奈換個ftp server後,就一直無法順利上傳
研判應該是wput無法針對新版的ftp server正確使用PASV模式
於是只好改用shell script直接上傳
下面的資料摘自How to use ftp in a shell script


Example (non-working) script

#!/bin/sh
HOST='ftp.users.qwest.net'
USER='yourid'
PASSWD='yourpw'
FILE='file.txt'

ftp $HOST <<END_SCRIPT
user $USER
$PASSWD
put $FILE
quit
END_SCRIPT
exit 0

The above script will just hang if run in the foreground (in an xterm), or if run in the background (from a cron job), it will fail to perform the work of transferring file.txt.

/dev/tty names a strange, magic device. Each process (more strictly each process group) has a different /dev/tty, and you can not naively make ftp clients read the password from some non-magic, yet convenient source, like a "here document". When run in an xterm, the script above appears to hang because it reads the password from /dev/tty. The xterm constitutes the script's /dev/tty, so the script waits for keyboard input.

Example Working Script

#!/bin/sh
HOST='ftp.users.qwest.net'
USER='yourid'
PASSWD='yourpw'
FILE='file.txt'

ftp -n $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
put $FILE
quit
END_SCRIPT
exit 0