Because the Web server that is normally managed is a VM server, in order to save hard disk space, generally to the virtual machine allocation of hard disk space is relatively small, only 8G, because, can not save how many logs, so every day need to transfer each Web log to a larger server on a hard disk, and then use NBU centralized backup , this program uses the Go language implementation to automatically upload the Web log FTP server via FTP, using the filepath. Walk traverses the log directory and the third party Pure Go Library "github.com/jlaffaye/ftp", while the log VM local storage path format is/var/log/weblog/www.domainname.com/month/20140616. Access.log,
Uploadlog/* 1. This program is mainly implemented under Linux upload Web log use, 2. The use method is Uploadlog Logfile_dir program only uploads the current point-in-time log file, * * Package main import ("Fmt "FTP" github.com/jlaffaye/ftp "Log" "NET" "OS" "Path/filepath" "StrConv" "Strings" "Time" func Main () {FMT.
Println ("Hello world!") If Len (OS. Args)!= 2 {log. Fatal ("Usage:" + filepath.) Base (OS. Args[0]) + "Log_dir") os. Exit (1)}//logfilename is the log logfilename that will be parsed, _, _: = Getlogfilename () ServerIP: = Getlocalipaddr ()//servername, _: = OS. Hostname () time. Sleep (time. Duration * time. Second) dir: = os. ARGS[1] filepath. Walk (dir, func (path string, f OS). FileInfo, err Error) error {if f = = nil {return err} if F.isdir () {return nil} if F.N Ame () = = LogFileName {fmt. The role of PRINTLN (path)//pathfields is to parse the log path into a data so that it can get the domain name of the log, and note that if it is a Linux system, use the "/" pathfields: = Strings. Split (path, "\") var domainname string If Len (Pathfields) > 3 {domainname = PathfielDs[len (Pathfields)-3]} FMT. Println (time.
Now ()) Ftpuploadfile ("ftp-server-ip:21", "Logftpuser", "Ftp-password", Path, DomainName, serverip+ "_" +logfilename) Fmt. Println (time. Now ())} return nil})} func Getlogfilename () (String, String, String) {monthtostr: = map[string]string{] Uary ":" "," February ":" "," March ":" "," April ":" "," may ":" A "," June ":" 06 "," July ":" "", "August": "", "September": "", "October": "Ten", "November": "One", "December": "12 "} TimeNow: = time. Now () year, month, day: = TimeNow. Date ()//monthstr: = month. String () hour, _, _: = TimeNow. Clock () Yearstr: = Strings. Trimleft (StrConv. Itoa (year), "20")//Remove the preceding four-digit year as "2014" daystr, HOURSTR: = StrConv. Itoa (day), StrConv. Itoa (hour) if day < {Daystr = "0" + daystr} if hour < ten {hourstr = "0" + hourstr} fileName : = "Ex" + Yearstr + monthtostr[month. String ()] + daystr + Hourstr + ". Log" Logday: = Yearstr + monthtostr[month. String ()] + daystr Logmonth: = Yearstr + monthtostr[month. String ()]//monthsrt: = StrConv. Itoa (TimeNow. Month ())//fmt. PRINTLN (filename, logday) return to filename, Logday, Logmonth//fmt. Println (FileName)} func getlocalipaddr () string {//Here is a legitimate IP on the line, the port casually, even if not open the line, perhaps because of the use of UDP, if the use of TCP, the end of the Open will have problems conn, ERR: = Net. Dial ("UDP", "192.168.8.51:80") if Err!= nil {//fmt. Println (Err. Error ()) return "127.0.0.1"} defer conn. Close ()//fmt. PRINTLN (Conn. Localaddr ().
String ())//conn. Fmt. PRINTLN (Strings. Split (Conn. Localaddr (). String (), ":") [0]) return strings. Split (Conn. Localaddr (). String (), ":") [0]} func ftpuploadfile (Ftpserver, Ftpuser, PW, LocalFile, Remotesavepath, savename string) {FTP, err: = Ftp.connect (ftpserver) If Err!= nil {fmt. PRINTLN (ERR)} err = Ftp.login (ftpuser, PW) If Err!= nil {fmt. PRINTLN (ERR)}//Note is Pub/log, cannot take "/" The Beginning Ftp.changedir ("Pub/log") dir, err: =Ftp.currentdir () fmt. Println (dir) ftp.makedir (Remotesavepath) ftp.changedir (Remotesavepath) dir, _ = Ftp.currentdir () fmt. Println (dir) file, err: = OS. Open (LocalFile) If Err!= nil {fmt. PRINTLN (ERR)} defer file. Close () Err = Ftp.stor (savename, file) If Err!= nil {fmt. PRINTLN (Err)} ftp.logout () Ftp.quit () fmt.
PRINTLN ("Success Upload file:", LocalFile)}
The above is the entire contents of this article, I hope you can enjoy.