CentOS上的Cronjob,通过scp上传文件,成功后删除

发布于 2024-12-27 23:42:16 字数 152 浏览 0 评论 0原文

我正在运行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冬天的雪花 2025-01-03 23:42:16

为此,我建议使用 rsync 而不是 scp,因为它更强大。只需将以下内容放入可执行脚本中即可。在这里,我假设所有文件(仅此而已)都位于 local_dir/ 指向的目录中。

#!/bin/env bash
rsync -azrp --progress --password-file=path_to_file_with_password \
local_dir/ remote_user@remote_host:/absolute_path_to_remote_dir/
if [ $? -ne 0 ] ; then 
echo "Something went wrong: don't delete local files."
else
rm -r local_dir/
fi

选项如下(有关详细信息,请参见,例如 http://ss64.com/bash/rsync .html):

 -a, --archive               Archive mode
 -z, --compress              Compress file data during the transfer
 -r, --recursive             recurse into directories
 -p, --perms                 Preserve permissions
     --progress              Show progress during transfer
     --password-file=FILE    Get password from FILE
     --delete-after          Receiver deletes after transfer, not during

编辑:删除 --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/.

#!/bin/env bash
rsync -azrp --progress --password-file=path_to_file_with_password \
local_dir/ remote_user@remote_host:/absolute_path_to_remote_dir/
if [ $? -ne 0 ] ; then 
echo "Something went wrong: don't delete local files."
else
rm -r local_dir/
fi

The options are as follows (for more info, see, e.g., http://ss64.com/bash/rsync.html):

 -a, --archive               Archive mode
 -z, --compress              Compress file data during the transfer
 -r, --recursive             recurse into directories
 -p, --perms                 Preserve permissions
     --progress              Show progress during transfer
     --password-file=FILE    Get password from FILE
     --delete-after          Receiver deletes after transfer, not during

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文