Created
November 25, 2013 16:51
-
-
Save TrevorS/7644474 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/env ruby | |
| require 'net/sftp' | |
| # Settings | |
| @feed = 'Commnet GGSN Pull' | |
| @host = '127.0.0.1' | |
| @user = 'username' | |
| @password = 'password' | |
| @root = '/path/to/root/directory' | |
| @input = "#{@root}/.auto/onboard/hd-raid/records/cdr/mediation215" | |
| @archive = "#{@input}/transferred_files" | |
| @destination = "/path/to/destination" | |
| @log_file = "#{@root}/ggsn/ggsn_pulled_files.list" | |
| @file_pattern = /^ICOM_GGSN_(\d+)_(\d+)_(\d+).*/ | |
| @days_to_keep = 10 | |
| # Download a file to the destination directory given a sftp connection and file name. | |
| def download_file(sftp, name) | |
| print "#{name}: " | |
| if sftp.download!("#{@input}/#{name}", "#{@destination}/#{name}") | |
| puts 'PULLED.' | |
| else | |
| puts 'PULL FAILED.' | |
| end | |
| end | |
| # Archive a file to the archive directory given a sftp connection and file name. | |
| def archive_file(sftp, name) | |
| print "#{name}: " | |
| if sftp.rename!("#{@input}/#{name}", "#{@archive}/#{name}") | |
| puts 'ARCHIVED.' | |
| else | |
| puts 'ARCHIVE FAILED.' | |
| end | |
| end | |
| # Log which file we are working on. | |
| def log_file(name) | |
| File.open(@log_file, 'a') do |f| | |
| f.puts(name) | |
| end | |
| end | |
| # Examine the archived directory and delete out of date files. | |
| def check_archived_files(sftp) | |
| current_date = Time.now.to_date | |
| sftp.dir.foreach(@archive) do |file| | |
| if file.file? && file.name =~ @file_pattern | |
| # $3 = year, $1 = month, $2 = date from the regex | |
| file_date = Date.new($3.to_i, $1.to_i, $2.to_i) | |
| if current_date - file_date > @days_to_keep | |
| print "#{file.name}: " | |
| if sftp.remove!("#{@archive}/#{file.name}") | |
| puts 'DELETED.' | |
| else | |
| puts 'DELETE FAILED.' | |
| end | |
| end | |
| end | |
| end | |
| end | |
| # Start the pull process. | |
| puts "#{Time.now} -- Starting #{@feed}." | |
| Net::SFTP.start(@host, @user, password: @password) do |sftp| | |
| sftp.dir.foreach(@input) do |file| | |
| if file.file? && file.name =~ @file_pattern | |
| download_file(sftp, file.name) | |
| archive_file(sftp, file.name) | |
| log_file(file.name) | |
| end | |
| end | |
| check_archived_files(sftp) | |
| end | |
| puts "#{Time.now} -- Ending #{@feed}." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment