Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Case 20, automated operation and maintenance-code online

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

The use of the expect script has been introduced in case 16. You can log in to the machine remotely and execute commands, but the expect script can also transfer files. The requirement of this case is to synchronize the files through the expect script so that the code can be online. In a production environment, a business usually runs on multiple servers, that is, load balancing, so the code running on these machines must be consistent. How to achieve consistency? There are two options.

1. By means of sharing

If the number of machines is small, you can use NFS. For stability, it is best to use professional storage devices (NAS, SAN, etc.). This method is structured as follows:

The advantage of this architecture is that it is easy to maintain. For example, when there is a code update, only the code on one machine needs to be updated, and all other machines will follow. The disadvantage is that if there are a large number of machines, shared storage will become a bottleneck, and even cause performance problems due to the scramble for files. In addition, shared storage here is a big single point of hidden danger, without failure, everything is OK, once there is a failure, the whole business is dead, the impact is very great.

2. Distributed

Since there are many disadvantages to sharing, choose another way, that is, save the code on the local disk of each web server, as shown in the figure:

The advantage of this is that there are no storage performance problems, no resource conflicts, and no hidden danger of a single point of failure. The disadvantage is that each code update requires an update to all web servers, which is tedious. Although the steps are cumbersome, most enterprises will choose this approach. The background of this case is the second approach, and the specific requirements are as follows:

1) provide an IP list of all web servers, ip.list.

2) suppose there is a normal user user01 on all web servers with a password of SGs2ox6uj, and that user is the user of the synchronization code.

3) each time the code is launched, a file list file.list (that is, the list of files to be changed) is provided.

Knowledge point 1: rsync synchronizes files through the file list

Although rsync is a tool for synchronizing files in previous cases, it is not common to synchronize files through a file list. Let's take a look at the example:

# cat 1.txt / tmp/123/1.sh/root/test/a.txt/etc/passwd# rsync-av-- files-from=./1.txt / root@192.168.93.130:/

Description: 1.txt is a list of files, that is, a list of files to be synchronized; the-- files-from option of rsync specifies the list files to be synchronized (the path of 1.txt, which can be absolute or relative). The content of this file list is actually a pile of file paths, which is recommended to use an absolute path, otherwise an error will occur. If the file path in the file list is an absolute path, the source directory of the rsync must be /, and the destination directory must be /.

Knowledge point 2: expect script synchronizes files

Similar to remote execution commands, if expect scripts want to synchronize files, the shell command after spawn is no longer ssh, but rsync. Example:

#! / usr/bin/expectset passwd "SGs2ox6uj" set host "192.168.93.130" spawn rsync-a user01@$host:/tmp/test.txt / tmp/expect {"yes/no" {send "yes\ r"} "password:" {send "$passwd\ r"} expect eof can also pass parameters to expect scripts Example: #! / usr/bin/expectset passwd "SGs2ox6uj" set host [lindex $argv 0] set file [lindex $argv 1] spawn rsync-a-- files-from=$file / user01@$host:/expect {"yes/no" {send "yes\ r"} "password:" {send "$passwd\ r"} expect eof

Reference script for this case

# / bin/bash## Code online # # author: # # date: # remind users whether the code list file read-p to be launched has been updated "have you updated the file list. / file.list? confirm, please enter y or Y, otherwise press any other key to exit the script." C # if you press enter directly, you will also exit the script if [- z "$c"] then exit 1fiif [$c = = "y"-o $c = = "Y"] then echo "the script will continue after 2 seconds." # output one per second. A total of two are output. For i in 1 2 do echo-n "." Sleep 1 done echoelse exit 1fi# determines whether there is a / rsync.exp file [- f. / rsync.exp] & & rm-f. / rsync.exp# definition rsync.expcat >. / rsync.exp

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report