class Struggle::Backup

Public Class Methods

new() click to toggle source

初始化配置文件

# File lib/struggle/backup.rb, line 19
def initialize
  _config = YAML.load_file("#{Rails.root}/config/backup.yml")
  @@config = _config.merge(YAML.load_file("#{Rails.root}/config/database.yml")[_config["env"]])
  @@config["backup_path"] = File.expand_path(@@config["backup_path"], Rails.root)
  @@now = Time.now
  # 备份路径创建
  # 参数now,创建时间,默认当前
  @@dir = "#{@@config["backup_path"]}/#{@@now.year}/#{@@now.month}/#{@@now.day}"
  unless Dir.exist? @@dir
    FileUtils.mkdir_p @@dir
  end
end

Public Instance Methods

database_backup() click to toggle source

备份数据库

# File lib/struggle/backup.rb, line 33
def database_backup
  _filename = "#{@@config["database"]}-#{@@now.strftime("%Y%m%d%H%M%S")}"
  filename = @@config["adapter"] == "oracle_enhanced" ? "#{_filename}.dmp" : "#{_filename}.sql"
  if @@config["adapter"] == "mysql2"
    system("mysqldump -u#{@@config["username"]} #{@@config["database"]}>#{filename}")
  elsif @@config["adapter"] == "postgresql"
    system("pg_dump -U #{@@config["username"]} -d #{@@config["database"]} -E utf8 -f #{filename} -w")
  elsif @@config["adapter"] == "oracle_enhanced"
    system("exp #{@@config["username"]}/#{@@config["password"]}@#{@@config["database"]} file=#{filename} full=y")
  end
  Struggle::ZipTool.file(filename, "#{@@dir}/#{_filename}.zip")
  File.delete filename
end
do() click to toggle source

备份

# File lib/struggle/backup.rb, line 66
def do
  database_backup
  file_backup
  if @@config["mode"] == "ftp"
    ftp_backup
  end
end
file_backup() click to toggle source

备份文件

# File lib/struggle/backup.rb, line 48
def file_backup
  @@config["asset_paths"].each do |assetPath|
    assetPath = "#{Rails.root}/#{assetPath}"
    ridx = assetPath.rindex("/") || 0
    Struggle::ZipTool.dir(assetPath, "#{@@dir}/#{assetPath[ridx..assetPath.length - 1]}-#{@@now.strftime("%Y%m%d%H%M%S")}.zip")
  end
end
ftp_backup() click to toggle source

ftp上传

# File lib/struggle/backup.rb, line 57
def ftp_backup
  _filename = "backup-#{@@now.strftime("%Y%m%d%H%M%S")}.zip"
  ftp_config = @@config["ftp"]
  Struggle::ZipTool.dir(@@dir, _filename)
  Struggle::FtpTool.new(ftp_config["ip"], ftp_config["username"], ftp_config["password"]).put(_filename, ftp_config["backup_path"])
  File.delete _filename
end