struggle 2.3.5 → 2.3.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9a55b2315ea94d918d91d363e65e15dd1fbe9b1b
4
- data.tar.gz: 862affa2a11bb4c603648db4232960e0f35445bb
3
+ metadata.gz: 80baef3cddc4ff506f15cb285aa4e635156858c5
4
+ data.tar.gz: 1d37cf7398b8c2326072da3502dc800e1b5d8422
5
5
  SHA512:
6
- metadata.gz: 5a4305e0ae51fb701102a4e510478c41d507fcf5d7737d98b60b6b5a806760b88fd698502a381bca34843f162beb67aab658ac164bcdcbd24da2208cd90a5169
7
- data.tar.gz: 12ea78da2163f44cb9409d741d2fd6387a3a05ecfe3094fb2d0797070c310548e2a60bee54494f51ad45ae85fee7b04bb37b6369d2b8cad1c7043ac694466b82
6
+ metadata.gz: 64c3f18846f0c6802451ddfddebdfe43206f61e4c97b425eb174cc4536cb26e488bda423d5413923bd62c9ee5b031c8f2848b39487ab3a3a996ae86c82411fc7
7
+ data.tar.gz: 9e28466f11ec697087eeae226d2dbb2a3c80b03826392e941df13bef9e6914304cc57c8b7d7e16525eff37dd7315485b4d6c25034d6fa3d852071b709a9e2b9d
@@ -0,0 +1,14 @@
1
+ require 'rails'
2
+
3
+ module Struggle
4
+ module Generators
5
+ class BackupConfigGenerator < Rails::Generators::Base
6
+ desc "create backup export config file"
7
+ def create_backup_config
8
+ libdir = File.expand_path(File.dirname(__FILE__))
9
+ backup_config = File.expand_path("../../templates/backup.yml", libdir)
10
+ FileUtils.cp(backup_config, "config/backup.yml")
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,62 @@
1
+ # 备份数据库和文件
2
+ # 生成配置文件命令 rails g struggle:sql_config
3
+ # Struggle::Backup.new.do 执行备份
4
+ module Struggle
5
+ class Backup
6
+ # 初始化配置文件
7
+ def initialize
8
+ _config = YAML.load_file("#{Rails.root}/config/backup.yml")
9
+ @@config = _config.merge(YAML.load_file("#{Rails.root}/config/database.yml")[_config["env"]])
10
+ @@config["backup_path"] = File.expand_path(@@config["backup_path"], Rails.root)
11
+ @@now = Time.now
12
+ # 备份路径创建
13
+ # 参数now,创建时间,默认当前
14
+ @@dir = "#{@@config["backup_path"]}/#{@@now.year}/#{@@now.month}/#{@@now.day}"
15
+ unless Dir.exist? @@dir
16
+ FileUtils.mkdir_p @@dir
17
+ end
18
+ end
19
+
20
+ # 备份数据库
21
+ def database_backup
22
+ _filename = "#{@@config["database"]}-#{@@now.strftime("%Y%m%d%H%M%S")}"
23
+ filename = @@config["adapter"] == "oracle_enhanced" ? "#{_filename}.dmp" : "#{_filename}.sql"
24
+ if @@config["adapter"] == "mysql2"
25
+ system("mysqldump -u#{@@config["username"]} -p#{@@config["password"]} -d #{@@config["database"]}>#{filename}")
26
+ elsif @@config["adapter"] == "postgresql"
27
+ system("pg_dump -U #{@@config["username"]} -d #{@@config["database"]} -E utf8 -f #{filename} -w")
28
+ elsif @@config["adapter"] == "oracle_enhanced"
29
+ system("exp #{@@config["username"]}/#{@@config["password"]}@#{@@config["database"]} file=#{filename} full=y")
30
+ end
31
+ Struggle::ZipTool.file(filename, "#{@@dir}/#{_filename}.zip")
32
+ File.delete filename
33
+ end
34
+
35
+ # 备份文件
36
+ def file_backup
37
+ @@config["asset_paths"].each do |assetPath|
38
+ assetPath = "#{Rails.root}/#{assetPath}"
39
+ ridx = assetPath.rindex("/") || 0
40
+ Struggle::ZipTool.dir(assetPath, "#{@@dir}/#{assetPath[ridx..assetPath.length - 1]}-#{@@now.strftime("%Y%m%d%H%M%S")}.zip")
41
+ end
42
+ end
43
+
44
+ # ftp上传
45
+ def ftp_backup
46
+ _filename = "backup-#{@@now.strftime("%Y%m%d%H%M%S")}.zip"
47
+ ftp_config = @@config["ftp"]
48
+ Struggle::ZipTool.dir(@@dir, _filename)
49
+ Struggle::FtpTool.new(ftp_config["ip"], ftp_config["username"], ftp_config["password"]).put(_filename, ftp_config["backup_path"])
50
+ File.delete _filename
51
+ end
52
+
53
+ # 备份
54
+ def do
55
+ database_backup
56
+ file_backup
57
+ if @@config["mode"] == "ftp"
58
+ ftp_backup
59
+ end
60
+ end
61
+ end
62
+ end
data/lib/struggle/sql.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  # 导出sql文件,生成insert语句
2
+ # 生成配置文件命令 rails g struggle:sql_config
2
3
  module Struggle
3
4
  class Sql
4
5
  class << self
data/lib/struggle.rb CHANGED
@@ -23,6 +23,7 @@ require 'struggle/concerns/time_extend'
23
23
  require "struggle/ftp_tool"
24
24
  require "struggle/zip_tool"
25
25
  require 'struggle/sql'
26
+ require 'struggle/backup'
26
27
  require 'pay/jd_gateway'
27
28
  String.send :include, Struggle::StringExtend
28
29
  Integer.send :include, Struggle::IntExtend
@@ -0,0 +1,10 @@
1
+ asset_paths: ["/public/system"] #rails项目根目录,备份静态文件路径,如果没有填[]
2
+ backup_path: "../backups" #rails项目根目录,备份目录
3
+ env: "production" #产品环境
4
+ mode: "local" #备份模式:local | ftp
5
+ ftp: { #ftp配置
6
+ ip: "139.129.195.138", #ftp ip地址
7
+ username: "ftpuser", #ftp 账号
8
+ password: "ftp123", #ftp 密码
9
+ backup_path: "/backups/nanguanzhujian_images/" #ftp 服务器备份路径
10
+ }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: struggle
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.3.5
4
+ version: 2.3.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - lean
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-01-02 00:00:00.000000000 Z
11
+ date: 2018-01-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: iconv
@@ -52,7 +52,7 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
- description: code,tfile,pager,http,logistic,pay,tmagick,class_extend,aes,rsa,ftp_tool,zip_tool,sql;
55
+ description: code,tfile,pager,http,logistic,pay,tmagick,class_extend,aes,rsa,ftp_tool,zip_tool,sql,backup;
56
56
  email: 54850915@qq.com
57
57
  executables: []
58
58
  extensions: []
@@ -60,10 +60,12 @@ extra_rdoc_files:
60
60
  - README.md
61
61
  files:
62
62
  - README.md
63
+ - lib/generators/struggle/backup_config_generator.rb
63
64
  - lib/generators/struggle/sql_config_generator.rb
64
65
  - lib/pay/jd_gateway.rb
65
66
  - lib/struggle.rb
66
67
  - lib/struggle/aes.rb
68
+ - lib/struggle/backup.rb
67
69
  - lib/struggle/code.rb
68
70
  - lib/struggle/concerns/decimal_extend.rb
69
71
  - lib/struggle/concerns/int_extend.rb
@@ -82,6 +84,7 @@ files:
82
84
  - lib/struggle/tfile.rb
83
85
  - lib/struggle/tmagick.rb
84
86
  - lib/struggle/zip_tool.rb
87
+ - lib/templates/backup.yml
85
88
  - lib/templates/tsql_config.yml
86
89
  homepage: http://rubygems.org/gems/struggle
87
90
  licenses: