2

I need to write a script that is run as a cron job every night which transfers some report files via sftp to another server.

The report files are created every night using another cron in the format 'support_[date].csv' & 'download_[date].csv'.

I'm wondering if you had any pointers on how to do the following:

  1. Find the 2 files created on latest [date]
  2. Copy these files to another server using SFTP

I've tried several PHP scripts utilising the ssh2 extension, but to no avail. Is there a way to do it using a shell script? It's not something I am hugely familiar with to be honest (hence going down the PHP route initially)

This was one of my PHP scripts which didn't work:

$src = 'test.csv';

$filename = 'test.csv';
$dest = '/destination_directory_on_server/'.$filename;

$connection = ssh2_connect('example.com', 22);
ssh2_auth_password($connection, 'username', 'password');

// Create SFTP session
$sftp = ssh2_sftp($connection);

$sftpStream = fopen('ssh2.sftp://'.$sftp.$dest, 'w');

try {
       
            if (!$sftpStream) {
                throw new Exception("Could not open remote file: $dest<br>");
            }
           
            $data_to_send = file_get_contents($src);
           
            if ($data_to_send === false) {
                throw new Exception("Could not open local file: $src.<br>");
            }
           
            if (fwrite($sftpStream, $data_to_send) === false) {
                throw new Exception("Could not send data from file: $src.<br>");
            } else {
                //Upload was successful, post-upload actions go here...
            }
           
            fclose($sftpStream);
                           
        } catch (Exception $e) {
           
            //error_log('Exception: ' . $e->getMessage());
           echo 'Exception: ' . $e->getMessage();
            if($sftpStream) {fclose($sftpStream);}
            
        }

This were the error messages I got:

Warning: fopen() [function.fopen]: URL file-access is disabled in the server configuration in /path_to_script/sftp-test.php on line 17

Warning: fopen(ssh2.sftp://Resource id

3/destination_directory_on_server/test.csv)

[function.fopen]: failed to open stream: no suitable wrapper could be found in /path_to_script/sftp-test.php on line 17 Exception: Could not open remote file: /destination_directory_on_server/test.csv

4

6 に答える 6

3

端末を使用してファイルの最新の日付を見つけるには、ls -1tr. 次に、(sftp ではなく) scp を使用して、例を介してファイルをコピー/転送します。

#!/bin/bash
latest_download=$(ls -1tr download*csv | tail -1)
latest_support=$(ls -1tr support*csv | tail -1)
scp $latest_download user@somehost.com:somedir  # syntax from memory, check man page for correct syntax
scp $latest_support user@somehost.com:somedir

使用方法については、scp の man ページを確認してください

于 2010-02-23T12:05:11.800 に答える
2

Muchosはghostdog74に称賛を送ります!これを機能させることができましたが、sftpを使用しました。

最初にキー認証を設定し、次にghostdog74のスクリプトを部分的に使用してこれを実行しましたが、完全に機能しました。

cd /directorywithfilesin
latest_download=$(ls -1tr download* | tail -1)
latest_support=$(ls -1tr support* | tail -1)
sftp username@example.com <<EOF
cd /dir_to_copy_to
put $latest_download
put $latest_support
EOF

ありがとう!

于 2010-02-24T12:43:33.510 に答える
1

Ghostdog74 の方法の他の問題の 1 つは、移植性がないことです。純粋な PHP SFTP 実装である phpseclib を使用することをお勧めします。

于 2011-02-25T05:49:00.067 に答える
0

答えはそこにあり、エラーメッセージにあります。

警告:fopen()[function.fopen]:サーバー構成でURLファイルアクセスが無効になっています

サーバー構成でURLラッパーを介したファイルアクセスが無効になっていることを意味します。

PHP構成、特にallow_url_fopenを確認してください。PHPのドキュメントには、「この設定はセキュリティ上の理由からphp.iniでのみ設定できます」と記載されているので、そこで確認してください。

fopenも参照してください:「ファイル名が登録済みプロトコルを指定し、そのプロトコルがネットワークURLとして登録されているとPHPが判断した場合、PHPはallow_url_fopenが有効になっていることを確認します。オフにすると、PHPは警告を発し、 fopen呼び出しは失敗します。」私が知る限り、それがまさにそこで起こっていることです。

allow_url_fopenを有効にできない、または有効にしない場合でも、いくつかのオプションがあります。

  • sftpを直接呼び出す
  • sshfsと共有をマウントし、それを通常のフォルダーとして使用します
于 2010-02-23T11:51:41.770 に答える
0

php.iniでリモートラッパーが無効になっているため、これはサーバーのPHPでは機能しません。

allow_url_fopen boolean

このオプションは、ファイルのようなURLオブジェクトへのアクセスを可能にするURL対応のfopenラッパーを有効にします。ftpまたはhttpプロトコルを使用してリモートファイルにアクセスするためのデフォルトのラッパーが提供されています。zlibなどの一部の拡張機能は、追加のラッパーを登録する場合があります。

注:この設定は、セキュリティ上の理由からphp.iniでのみ設定できます。

sftpただし、cronジョブに、またはrsync直接使用するシェルスクリプトを呼び出させることができます。PHPでこれを行う必要はありません。

シェルスクリプトのサポートを改善するために、これをServerFaultに移動することに投票します。

于 2010-02-23T11:54:44.490 に答える