简体   繁体   English

将最近更改的文件从一台(开发)服务器推送到远程(活动)服务器的脚本?

[英]Script to push recently changed files from one (dev) server to remote (live) server?

I haven't been able to find a specific answer on this in my Googling, so I figure maybe someone has an answer for me or can point me in the right direction. 我在谷歌搜索中找不到具体的答案,因此我认为也许有人对我有答案,或者可以将我指出正确的方向。

I have 2 servers both running CentOS 6.3. 我有2台都运行CentOS 6.3的服务器。 One is a dev server and the other is the live server. 一个是开发服务器,另一个是实时服务器。 I make all changes on the dev server, test them out, and then copy them to the live server once I'm satisfied. 我对开发服务器进行了所有更改,进行了测试,然后在满意后将其复制到实时服务器中。 The system works fine, but it's a pain to have to use scp or rsync everytime and type the entire absolute dir paths every time I push a file or dir. 该系统运行良好,但是每次都必须使用scp或rsync并在每次推送文件或目录时键入整个绝对目录路径,这是一个痛苦。

I'm not an experienced sysadmin by any means, but I would love a way to just run one script that will take all recently changed files (maybe in the last 20 minutes, or a time that I specify) and have it automatically push them to the live server, maintaining the same directory structure and everything. 无论如何,我都不是经验丰富的系统管理员,但是我希望有一种方法可以运行一个脚本,该脚本将处理所有最近更改的文件(可能是最近20分钟,或者我指定的时间)并自动将其推送到实时服务器,并保持相同的目录结构和所有内容。

Is there something easy out there that will do this for me, or some kind of bash script I can write to do this? 是否有一些简单的事情可以帮我做到这一点,或者我可以编写某种bash脚本来做到这一点?

Thanks in advance. 提前致谢。

find -mmin -20 -exec mv {} {}NEW \;

find -mmin -20 finds all files that were last modified less than 20 minutes ago. find -mmin -20查找所有少于20分钟前修改的文件。 You can also specify a directory. 您还可以指定目录。

The -exec option invokes a command on all the found files. -exec选项在所有找到的文件上调用命令。 {} is replaced with the actual filename. {}被替换为实际的文件名。 In my example the files are simply renamed, the executed command would be 在我的示例中,文件被简单地重命名,执行的命令将是

mv filename filenameNEW

for every file. 对于每个文件。 Here you'd put your scp or rsync statement of course. 在这里,您当然要放上scprsync语句。

The -exec command needs to end with a ; -exec命令需要以;结尾; , since ; 以来; is also a special character in bash, it needs to be escaped with \\ 也是bash中的特殊字符,需要用\\进行转义

I ended up figuring this out, thanks to the help I had from Keith and pfnuesel. 由于Keith和pfnuesel的帮助,我最终弄清楚了这一点。 Here's my solution in case anyone else is looking for an easy way to do this. 这是我的解决方案,以防其他任何人正在寻找一种简便的方法来做到这一点。

I ended up using the --update option in rsync, which will essentially only copy files over to the remote server that are newer than the version that exists there. 我最终在rsync中使用了--update选项,该选项实际上只会将比那里存在的版本新的文件复制到远程服务器上。 So I ended up just creating a function in my ~/.bashrc file that will run it for me. 因此,我最终只是在〜/ .bashrc文件中创建了一个函数,该函数将为我运行。 Here's my function I threw together- 这是我综合在一起的功能-

function push() {
    rsync -avzu /var/www/$1 root@12.34.56.78:/var/www/$1
}

So I can just run "push dirname" and it works like a charm. 因此,我可以运行“ push dirname”,它的工作原理就像一个超级按钮。 Thanks for the help guys! 感谢您的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM