‘rsync’ command source and destination explained

information sign on paper

rsync command is super useful because it could incrementally sync files between locations without the overhead of compressing and transferring. There are heaps of tutorials about how to use it, how very few cover the point that how to sync two folders, which I believe is the most common use.

I’d run rsync -avzP dir1 dir2, but unfortunately, this syncs dir1 under dir2. Some people would then run rsync -avzP dir1 dir2-parent. This could work, but it’s particularly dangerous if you add the --delete option. So how to sync the content of the two folders.

If you run man rsync – not many people would use nowadays, you’d get the very import/useful instructions:

A trailing slash on the source changes this behavior to avoid creating an
additional directory level at the destination. You can think of a trailing /
on a source as meaning “copy the contents of this directory” as opposed to
“copy the directory by name”, but in both cases the attributes of the
containing directory are transferred to the containing directory on the
destination. In other words, each of the following commands copies the files
in the same way, including their setting of the attributes of /dest/foo:
rsync -av /src/foo /dest
rsync -av /src/foo/ /dest/foo

The instructions are there like forever, right? But the first command is tricky where you could override the destination directory with the content of foo, so I’d recommend you to always use the second way:

rsync -avP dir1/ dir2

And it works the same way for remote directories.

Bash速成

条件语句(注意:条件里两边的空格,引号,等号)

if [ “$var” = “abc” ]; then

elif [ “$var” = “ac” ]; then

else

fi

for循环

for var in $(ls *.sh); do
echo $var
done

while循环

var=1
while [ “$var” -le 20 ] ; do
var=$(($var+1))
done

until循环(跟while循环相反的)

until condition
do

done

case条件(可用正则,;;相当于break)

case “$var” in
yes | YES | y )
echo “YES”
echo “haha”
;;
[Nn]* ) echo “NO”;;
* ) echo “OTHER”;;
esac

定义/赋值变量

var=xxx  (等号两边不能有空格)

变量读取

echo $var

读取用户输入

read var

不输出换行

echo -n

执行命令并捕获返回值

$(command)

其它

shell里默认类型是字符串型

交互式shell编程利器expect

手里有几台Linux服务器需要经常添加用户,每次都要登录到相应的机器上去添加,特别麻烦。于是想,可不可以在一台机器上写一个脚本来远程管理其它服务器呢?
目标首先瞄准了我熟悉的PHP-CLI,它有一个开发中的模块ssh2,可以完成相应的功能。这个不想说了,因为用了半天都不行,Bug还太多,建议大家如非必要还是不要用这个模块的好。
没了PHP,很迷茫,然后很幸运地发现了expect。expect是交互式shell编程的利器,可以根据返回值来确定下面发送什么命令,特别好用。我把自己编写的远程增加用户的shell跟大家分享下(需要机器装有expect,没有的自己装吧),脚本如下:
Continue reading “交互式shell编程利器expect”

Linux中文件和目录的权限问题

最近搞了几个VPS玩,VPS一般来说内存都不多,配置轻量级的Nginx+PHP,折腾当中权限问题搞了半天。
大家都知道,Linux中文件和目录都有自己的权限,分为rwx三种,分别代表读、写、执行的权限。但是目录和文件又不一样,不能被写和执行,文件rwx三种权限与目录的对比如下:

权限 文件 目录
r 可以列表该目录中的文件
w 可以在该目录中创建或者删除文件
x 执行 可以搜索或者进入该目录

现在很多的博客代码都提供在线安装插件或者升级等方便的功能,但是如果权限设置的不正确就无法使用,比如《WordPress“执行请求操作,连接信息必需提供”解决方法》中提到就是这样的问题。
Continue reading “Linux中文件和目录的权限问题”

php-cli简介——不会Shell语言,一样用Shell!

1.基础知识

1.1 什么是Shell编程?

在 Unix 中,shell 可不是简单的命令解释器(典型的有 Windows 中的 DOS ),而是一个全功能的编程环境。Shell 是操作系统的一部分,用来与用户打交道,并且可以用来协调各个命令【1】。用Shell编程可以灵活地解决大量重复任务,十分方便。但是,Shell的语法十分怪异(个人意见),不容易记,如果现在熟悉的语言可以用来写shell那就好了——比如php——就可以快速开发Shell程序了(比如我的Preminder的后台程序),于是便有了这篇文章,本文以Linux为例说明php-cli的用法,其它平台的版本类似。

1.2 什么是php-cli?

刚才说到,我们可以用php来开发Shell程序。有的同学可能会问啦:“php不是用来做网页的么?-_-”。是的,php可以用来做动态网页,并且当初php就是为做动态网页而开发的语言,但是理论上php可以用来做任何的程序,甚至是桌面程序,而php-cli是php在命令行运行的支持环境,也就是我们说的可以用来写Shell的环境支持。

php-cli是php Command Line Interface的简称,如同它名字的意思,就是php在命令行运行的接口,区别于在Web服务器上运行的php环境(php-cgi, isapi等)【2】。

也就是说,php不单可以写前台网页,它还可以用来写后台的程序。
Continue reading “php-cli简介——不会Shell语言,一样用Shell!”