Auto backup mysql database and use ftp command to cron transfer(bash shell)

August 27th, 2010

Use mysqldump command to dump the database sql files,then merge it with site file sources to tar named suffixed by YYYYMMDD(year-month-day).Then use ftp protocal tranfering the tarball to your remote backup server.
Here goes the script:
#!/bin/bash
#wordpress_backup.sh,chmod +x wordpress_backup.sh
YYYY=`date +%Y`
MM=`date +%m`
DD=`date +%d`
filename="$YYYY$MM$DD"
mysqldump -h localhost -u root -p123456 --default-character-set=utf8 db1>/tmp/db1_${filename}.sql
mysqldump -h localhost -u root -p123456 --default-character-set=utf8 db2>/tmp/db2_${filename}.sql
tar zcvpPf /tmp/doxer_$filename.tar.gz /var/www/virtual/doxer.org /tmp/db1_${filename}.sql db2_${filename}.sql
# to your backup server
ftp -v -n xxx.xxx.xxx.xxx <<EOF
userĀ  ftp@xxx.com password
binary
cd backup_sql #change directory on remote backup server
lcd /tmp #change directory on localhost
prompt
mput doxer_$filename.tar.gz #upload tarball
close
bye
EOF

At last,if you want to backup the files three times a week,use crond to accomplish it.Here is the usage:
crontab -e
Type in the following line at the end of it:
0 23 * * 0,2,4 /usr/local/wordpress_backup.sh
Save & quit.Use /etc/init.d/cron restart,and enjoy the convienience.

php recursively count files under directory

August 24th, 2010

Sometimes(seems all times),virtual host provider has a limitation of file number on you(inode issue),and you want to use php to count files under directory.Here is a script for you.

many.php

FileCount("wordpress&rmbweb&liang99&huainong&discuz&cms&book",true);//true to count in files that positioned the same directory as this php script.First param is directory name.Use & to seperate from each param.
echo $count;

function FileCount($dir,$include=false){
global $count;
$arr=explode('&',$dir);
foreach($arr as $val){
if(is_dir($val)&&file_exists($val)){
$ob=scandir($val);
foreach($ob as $file){
if($file=="."||$file==".."){
continue;
}
$file=$val."/".$file;

if(is_file($file)){
$count++;
}elseif(is_dir($file)){
FileCount($file);
}
}
}
}
if($include){
$pathinfo=pathinfo(__FILE__);
$ob2=scandir($pathinfo['dirname']);
foreach($ob2 as $val){
if($val=='.'||$val=='..'){
continue;
}
$file=$pathinfo['dirname'].'/'.$val;
if(is_file($file)){
$count++;
}
}
}
}

php recursively delete directorys

August 24th, 2010

When using utf8 or unicode charsets,you have the chance to find messy codes with your file or directory descriptor.And the worse,you'll find that you cann't delete files that has a messy code file descriptor.If you have root access to your server,just use rm -rf to delete them,but what would you do if you just have ftp access to it?Here goes the solution for you.
<?php
function _imagecache_recursive_delete($dir) {
$d = dir($dir);
if (is_dir($dir) && !is_link($dir)) {
if ($d = opendir($dir)) {
while (($entry = readdir($d)) !== false) {
if ($entry == '.' || $entry == '..') continue;
$path = $dir .'/'. $entry;
if (is_file($path)) unlink($path);
if (is_dir($path)) _imagecache_recursive_delete($path);
}
closedir($d);
}
return rmdir($dir);
}
return unlink($dir);
}
$dir='./wordpress/wp-content/uploads/2010/03/';
_imagecache_recursive_delete($dir);
?>

Solved:mysql without access to remote hosts

August 23rd, 2010

1.Firstly,find your mysql my.cnf configuration file:(mysql --help|grep "Default options are read" -1 #-1,show 1 line before and 1 line after the match line)
Default options are read from the following files in the given order:
/etc/mysql/my.cnf ~/.my.cnf /usr/etc/my.cnf
2.vim and 47gg to jump to the 47 line,comment away bind-address

3.log in mysql,and execute the following sql statement:
CREATE USER 'root'@'%';
GRANT ALL PRIVILEGES ON * . * TO 'root'@'%' WITH GRANT OPTION MAX_QUERIES_PER_HOUR 0
MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0 ;
SET PASSWORD FOR 'root'@'%' = PASSWORD( '******' )

Using bash shell to auto-detect linux OS types(centos,debian,others)

August 18th, 2010

#!/bin/bash
#linux_type.sh
os="";
grep "centos" /etc/issue -i -q
if [ $? = '0' ];then
os='centos'
fi

grep "debian" /etc/issue -i -q
if [ $? = '0' ];then
os='debian'
fi

if [ $os = "" ];then
echo "not a valid system os"
exit 1
fi
Save the above code as linux_type.sh,chmod +x ./linux_type.sh.Then,execute the bash script to get the result.

Small fix to Google Syntax Highlighter for WordPress

August 15th, 2010

1.Where to put <pre></pre>

If you put source file in the 'Virsual' mode of your wordpress editor,you should then change to 'HTML' mode and enclose the source code with <pre></pre>.

2.About <pre> name='code'(use <pre lang='code'> to substitute <pre name='code'>)

By defualt,Google Syntax Highlighter for WordPress use <pre name='code'> to declare it's css's beginning.However,you'll find the 'name' attribute lost when you switching from Virsual to HTML.We can solve the problem by using the W3C standard attribute of <pre> tag,that is <pre lang='code'>.

Here are the steps:

Find wp-content/plugins/google-syntax-highlighter/Scripts/shCore.js,locate to line 364,substitute
<pre class="html" lang="code">if(_73[i].getAttribute("name")==_71){</pre>
to<!--more-->
<pre class="html" lang="code">if(_73[i].getAttribute("lang")==_71){</pre>
And that's all about the revision.Afterwards,use <pre lang="code" class="..."> when you want to highlight your code.

Apache server statistics:SetHandler,.htaccess,server-info,server-status

August 11th, 2010

<Files "server-info.php">
SetHandler server-info
</Files>

<Files "server-status.php">
SetHandler server-status
</Files>
Just put them in one of your .htaccess files,and then access the file:
http://yourdomain.tld/server-info.php
http://yourdomain.tld/server-status.php
you'll see the lamp server configure infomation and the current status of your server(how many concurrent people on your site,etc.).