Dual-machine Data Synchronization Method Based on Network File Sharing Service

Source: Internet
Author: User

I developed a software system several years ago to provide users with uninterrupted services. To ensure service quality, dual-host Hot Standby is adopted. One of the key technologies for dual-host systems to work normally is data synchronization between the master and slave machines. The data synchronization method described below is implemented based on the network file sharing service, which is simple, secure, and reliable. When the time was tight, the task was heavy, and the investment was limited, the application results were satisfactory.

Windows Network File Sharing Service

In a Windows system, a computer can serve as a server to provide the "file sharing service" to other computers (clients) in the LAN ". Through this service, client users can perform a series of operations on shared files on the server over the network just like accessing local files.
When a user connects to a shared location on the server through a client, they can:

  • Open a file
  • Save files
  • Delete an object
  • Create, modify, and delete folders
  • Execute other tasks

There are multiple methods for a client to access the shared file location on the server, such as network neighbors and ing network drives. Here we use the "UNC path access method" suitable for software programming ".

UNC (Universal Naming Convention) "Universal Naming Convention" is used to determine the location of files stored on the network server. UNC complies with the \ servername \ sharename format, where servername is the server name and sharename is the name of the shared resource. The UNC name of a directory or file can include the directory path under the shared resource name. The format is \ servername \ sharename \ directory \ filename, where directory is the directory name and filename is the shared file name.

The data synchronization method is designed to enable the backup server (server) by using the UNC path access method when data synchronization is required between two servers) to write the latest running data to the data file. This ensures data consistency between the master and slave servers.

Because the network file sharing service is a standard service provided by the Windows operating system based on TCP/IP protocol, before the server provides services, the client must provide valid access creden (such as the domain name, user name, and password). The operations that a user can perform depend on the permissions granted, therefore, using the network file sharing service, combined with common technologies such as file operations and Ping, the data synchronization mechanism of the dual-machine hot standby system can be implemented without database, secure, convenient, and easy to maintain.

Service configuration process

Because the network file sharing service is not the default service provided by the Windows operating system, perform the following steps:

Figure 1. service configuration steps

Step 1: Install the Windows operating system on both servers. Generally, Windows 2000 or later operating systems can provide network file sharing services.

Step 2: create the same user in the dual-host operating system and use the same password, make sure that you have the permission to create, read, write, modify, and delete shared files and directories. This setting ensures that the client passes the server security verification and accesses the server's shared resources.

Step 3: Install the TCP/IP protocol in the operating system and ensure that the dual-host in the same LAN can communicate normally.

Step 4: Install the "Microsoft network client" and "Microsoft network file and printer sharing" network components in the operating system. These components are the basic configurations of the file sharing service provided by the operating system.

At this time, the configuration is complete, and the dual machine already meets the requirements for mutual server and client.

Data Synchronization implementation

The specific data synchronization operation between two machines is performed by the application software running in the operating system. The data synchronization operation is usually initiated by the application software on the host, so the execution process is as follows:

 

Figure 2. Host Data Synchronization Process

In the above process, the host application software first "writes data to the local data file" to ensure that the running data is not lost when the application software is forced to shut down, after the restart, you can obtain data from the local data file and continue to run normally.

The purpose of the ping operation to the slave server is to check whether the network between the master and slave servers is smooth at that time, ensuring subsequent data file operations. The Ping (packet Internet groper) operation is to send ICMP (Internet Control Messages protocol, Internet Control Packet protocol) echo request message to the target IP address, determine the network disconnection status based on whether the other Party responds. Here, you can write the IP address of the slave machine in the configuration file of the application software. The application software reads the IP address in the startup phase and uses it as a parameter for UNC path access and ping operations.

The C ++ code for executing the "access data files of the standby machine through UNC path" operation is as follows:

1 cfile netlogfile; 2 cstring strfilename; 3 strfilename. format ("\ % s \ C $ \ staterm \ log. TMP ", m_strbackupip); 4 5 // open the backup data file log. TMP 6 try 7 {8 If (! Netlogfile. open (strfilename, cfile: modecreate | cfile: modewrite) 9 {10 trace0 ("\ n cannot open the backup data file log. TMP \ n "); 11 return; 12} 13} 14 Catch (cexception * pe1) 15 {16 trace0 (" \ n system open the backup data file log. error in TMP! \ N "); 17 pe1-> Delete (); 18 return; 19}

The above 3rd lines of code use the UNC path access method to determine the location of the backup data file. In this way, you can use the local file opening function (cfile: open) on the second line to open the network shared data file on the slave machine, thus reducing the difficulty and complexity of software development.

When you perform the "write data to the backup data file" operation, the host application software can synchronize data to the backup machine in two ways:

  • Open the backup data file, write the host data directly, and close the file;
  • Copy the latest data file of the host to the data file directory of the backup machine to overwrite the original data file of the backup machine.

Applications running on the slave machine do not generally perform read/write operations on the data files of the Local Machine to avoid the impact of synchronous operations on the host. The application software reads data from the local data file and enters the normal execution logic only when it receives commands that are upgraded to the host State from the master/Slave ruling module. Because the data file is updated through the previous synchronization process, it is consistent with the latest data on the original host. Therefore, the data synchronization function between two machines is implemented during Master/Slave switchover.

 

 

-[Email protected] blog-

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.