github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/system/backup/local.go (about) 1 // This file is part of the Smart Home 2 // Program complex distribution https://github.com/e154/smart-home 3 // Copyright (C) 2023, Filippov Alex 4 // 5 // This library is free software: you can redistribute it and/or 6 // modify it under the terms of the GNU Lesser General Public 7 // License as published by the Free Software Foundation; either 8 // version 3 of the License, or (at your option) any later version. 9 // 10 // This library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 // Library General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public 16 // License along with this library. If not, see 17 // <https://www.gnu.org/licenses/>. 18 19 package backup 20 21 import ( 22 "fmt" 23 "os" 24 "os/exec" 25 "path" 26 ) 27 28 type Local struct { 29 cfg *Config 30 } 31 32 func NewLocal(cfg *Config) *Local { 33 return &Local{cfg: cfg} 34 } 35 36 func (l *Local) New(tmpDir string) (err error) { 37 38 options := l.dumpOptions() 39 40 // get data 41 var dataFile = path.Join(tmpDir, "data.sql") 42 cmd := exec.Command("pg_dump", append(options, "-a", "-f", dataFile)...) 43 cmd.Env = append(os.Environ(), fmt.Sprintf("PGPASSWORD=%s", l.cfg.Password)) 44 45 log.Infof("run command: %s", cmd.String()) 46 47 var b []byte 48 b, err = cmd.CombinedOutput() 49 fmt.Fprintln(os.Stdout, string(b)) 50 if err != nil { 51 return 52 } 53 54 // get scheme 55 var schemeFile = path.Join(tmpDir, "scheme.sql") 56 cmd = exec.Command("pg_dump", append(options, "-s", "-f", schemeFile)...) 57 cmd.Env = append(os.Environ(), fmt.Sprintf("PGPASSWORD=%s", l.cfg.Password)) 58 59 log.Infof("run command: %s", cmd.String()) 60 61 b, err = cmd.CombinedOutput() 62 fmt.Fprintln(os.Stdout, string(b)) 63 if err != nil { 64 return 65 } 66 67 return 68 } 69 70 func (l *Local) Restore(path string) (err error) { 71 72 options := l.restoreOptions() 73 74 options = append(options, "-v", path) 75 76 cmd := exec.Command("pg_restore", options...) 77 cmd.Env = append(os.Environ(), fmt.Sprintf("PGPASSWORD=%s", l.cfg.Password)) 78 79 log.Infof("command: %s", cmd.String()) 80 81 var b []byte 82 b, err = cmd.CombinedOutput() 83 fmt.Fprintln(os.Stdout, string(b)) 84 if err != nil { 85 return 86 } 87 88 return 89 } 90 91 func (l *Local) dumpOptions() []string { 92 options := []string{} 93 94 // db name 95 if l.cfg.Name != "" { 96 options = append(options, "-d", l.cfg.Name) 97 } 98 99 // host 100 if l.cfg.Host != "" { 101 options = append(options, "-h", l.cfg.Host) 102 } 103 104 // port 105 if l.cfg.Port != "" { 106 options = append(options, "-p", l.cfg.Port) 107 } 108 109 // user 110 if l.cfg.User != "" { 111 options = append(options, "-U", l.cfg.User) 112 } 113 114 // compress level 115 //options = append(options, "-Z", "9") 116 117 // formats 118 options = append(options, "-F", "t") 119 120 // etc 121 options = append(options, "-w", "-v", "--disable-triggers", "--quote-all-identifiers", "-b", "-O", "-x") 122 123 return options 124 } 125 126 func (l *Local) restoreOptions() []string { 127 128 options := []string{} 129 130 // db name 131 if l.cfg.Name != "" { 132 options = append(options, "-d", l.cfg.Name) 133 } 134 135 // host 136 if l.cfg.Host != "" { 137 options = append(options, "-h", l.cfg.Host) 138 } 139 140 // port 141 if l.cfg.Port != "" { 142 options = append(options, "-p", l.cfg.Port) 143 } 144 145 // user 146 if l.cfg.User != "" { 147 options = append(options, "-U", l.cfg.User) 148 } 149 150 return options 151 }