CentOS上的Cronjob,通过scp上传文件,成功后删除
我正在运行 CentOS 6。
我需要每小时上传一些文件到另一台服务器。
我可以使用密码通过 SSH 访问服务器。但 ssh-keys 等不是一个选择。
任何人都可以帮助我使用 .sh 脚本通过 scp 上传文件并在成功上传后删除原始文件吗?
I'm running CentOS 6.
I need to upload some files every hour to another server.
I have SSH access with password to the server. But ssh-keys etc. is not an option.
Can anyone help me out with a .sh script that uploads the files via scp and delete the original after a successful upload?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为此,我建议使用 rsync 而不是 scp,因为它更强大。只需将以下内容放入可执行脚本中即可。在这里,我假设所有文件(仅此而已)都位于 local_dir/ 指向的目录中。
选项如下(有关详细信息,请参见,例如 http://ss64.com/bash/rsync .html):
编辑:删除 --delete-after,因为这不是 OP 的意图
设置包含密码的文件的权限时要小心。理想情况下,只有您有权访问该文件。
像往常一样,我建议尝试一下 rsync 以熟悉它。在删除本地文件之前最好检查一下rsync的返回值(使用$?)。
有关 rsync 的更多信息:http://linux.about.com/library/cmd/blcmdl1_rsync。嗯
For this, I'd suggest to use rsync rather than scp, as it is far more powerful. Just put the following in an executable script. Here, I assume that all the files (and nothing more) is in the directory pointed to by local_dir/.
The options are as follows (for more info, see, e.g., http://ss64.com/bash/rsync.html):
Edit: removed --delete-after, since that's not the OP's intent
Be careful when setting the permissions for the file containing the password. Ideally only you should have access tot he file.
As usual, I'd recommend to play a bit with rsync in order to get familiar with it. It is best to check the return value of rsync (using $?) before deleting the local files.
More information about rsync: http://linux.about.com/library/cmd/blcmdl1_rsync.htm