Use SFTP in Ubuntu to synchronize files to different servers

Source: Internet
Author: User
Let's talk about the background of the application. Today, the company asked me to write a segment program and asked me to synchronize the configuration files of the master server to different servers. After studying for a while, the code written is as follows: Part of the configuration file config. inc. php: 'servergroup' = & gt; array ('192. 168.0.240 '= & gt;

Let's talk about the background of the application.

Today, the company asked me to write a segment program and asked me to synchronize the configuration files of the master server to different servers. After a while,

The code written is as follows:

Configuration file config. inc. php:

  1. 'Servergroup' =>Array(
  2. '1970. 168.0.240 '=>Array(
  3. 'User' => 'root ',
  4. 'Pass' => 'sjgj2508 ',
  5. 'Path' => '/var/www/html /',
  6. ),
  7. '1970. 168.0.244 '=>Array(
  8. 'User' => 'root ',
  9. 'Pass' => 'sjgj2508 ',
  10. 'Path' => '/var/www/html /',
  11. ),
  12. ),

The main code of the synchronization program is as follows:

  1. $ ConfigName = 'config. xml'; // name of the configuration file of the master server
  2. $ ConfPath = ROOT_PATH. '/_ cfg/config. xml'; // the configuration file path of the master server
  3. // Slave server host name and other information
  4. $ ServerGroup = $ this-> conf ['servergroup']; // $ this-> conf [] here; is a built-in array of the Framework. You can obtain the content in the preceding configuration file.
  5. Foreach($ ServerGroupAs$ K => $ v ){
  6. // Connect to the slave server
  7. $ ResConnection = ssh2_connect ($ k );
  8. // Determine whether the log on to the slave server is successful
  9. If(Ssh2_auth_password ($ resConnection, $ v ['user'], $ v ['pass']) {
  10. // Initialize the SFTP Subsystem
  11. $ ResSFTP = ssh2_sftp ($ resConnection );
  12. If(! Copy ($ confPath, "ssh2.sftp: // {$ resSFTP} {$ v ['path']} _ inc/class/Algorithm/{$ configName }")){
  13. Echo 'synchronization configuration file failed ';
  14. }
  15. }Else{
  16. Echo "unable to connect to the slave server {$ k }";
  17. }
  18. }

Before testing, make sure that the system has related extensions (such as libssh2 ).

Write a blog for the first time. Write down these items as a backup. Please kindly advise.

In addition, one of the programs is not very understandable.

If (! Copy ($ confPath, "ssh2.sftp: // {$ resSFTP} {$ v ['path']} _ inc/class/Algorithm/{$ configName }")){

Echo 'synchronization configuration file failed ';

}

In this section, ssh2.sftp: // {$ resSFTP} {$ v ['path']} _ inc/class/Algorithm/{$ configName }"

The point is that it is unclear why ssh2.sftp: // {$ resSFTP} should be added. Why? I hope the great gods who read this article can help me solve the problem. Thank you.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.