Last active
March 12, 2020 09:32
-
-
Save SilentGopherLnx/d79806f61f985f061ccdb8dc198589d5 to your computer and use it in GitHub Desktop.
1С:Предприятие - Бэкапер (для Windows)
This file contains 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
package main | |
// Делает бекапы сразу нескольких 1С-баз по очереди, | |
// можно не отключать регламентные задания! | |
// так как делается несколько попыток пока не попадём в окно паузы между ними | |
// 1config.txt - пример файла настройки. Пути с пробелами запрещены. Скрипт создаёт и запускает bat-файлы. Потом их удаляет | |
// Структура файла - по 2 строчки на базу. В первой - путь куда сохраняется бекап (например облако) | |
// Вторая строка F/S - файловая/серверная, ip(+порт если не 1541) или путь, имя базы или папки, | |
// пользователь с правом конфигуратора, пароль, произвольный суффикс (напрмиер версия типовой конфы), номера дней когда делать бекап | |
import ( | |
. "github.com/SilentGopherLnx/easygolang" | |
"bytes" | |
"os/exec" | |
) | |
func init() { | |
Prln("hi!") | |
} | |
func main() { | |
now := TimeNow() | |
Prln(TimeStr(now, true)) | |
hours := 3 | |
now = TimeAddMS(now, -3600*hours*1000) | |
now_date := TimeStr(now, false) | |
now_date = StringReplace(StringPart(now_date, 1, 10), ".", "_") | |
Prln(now_date) | |
now_day := I2S(TimeWeekday(now)) | |
app_path := FolderPathEndSlash(FolderLocation_App()) | |
Prln(app_path) | |
conf_path := app_path + "config.txt" | |
config_all, ok := FileTextRead(conf_path) | |
if !ok { | |
Prln("no config at > " + conf_path) | |
} | |
config_all = StringFixUTF8(config_all) | |
config_all = StringReplace(config_all, "\r", "\n") | |
config_all = StringReplace(config_all, "\n\n", "\n") | |
config_arr := StringSplitLines(config_all) | |
for j := 2; j < len(config_arr); j += 2 { | |
id := j / 2 | |
if len(config_arr[j]) > 0 { | |
arr := StringSplit(StringTrim(config_arr[j]), " ") | |
days := arr[6] | |
log := "log_" + I2S(id) + ".txt" | |
if StringFind(days, now_day) > 0 { | |
//v8exe := "C:\\Program Files\\1cv8\\" + config_arr[0] + "\\bin\\1cv8.exe" | |
v8exe := config_arr[0] | |
backup_path1 := FolderPathEndSlash(app_path + "copies") | |
cmds := GetCmd(v8exe, backup_path1, now_date, arr, log) | |
Prln(cmds[0]) | |
backup_path1 = cmds[1] | |
backup_path2 := FolderPathEndSlash(config_arr[j-1]) + cmds[3] | |
// Prln(cmds[1]) | |
// Prln(cmds[2]) | |
rname := app_path + "t_" + I2S(id) + ".bat" | |
FileTextWrite(rname, "chcp 65001\r\n"+cmds[0]) | |
n_max := 10 | |
n := 1 | |
SleepMS(3000) | |
for n <= n_max { | |
a, b, c := ExecCommand_simple(rname) | |
SleepMS(3000) | |
if FileExists(cmds[1]) { | |
Prln("ok! [" + I2S(n) + "]: " + cmds[1]) | |
n = n_max + 1 | |
err := FileCopyAtom(backup_path1, backup_path2, nil, 1024*32) | |
errtxt := "" | |
if err != nil { | |
errtxt = err.Error() | |
} | |
Prln("copied! " + backup_path1 + " > " + backup_path2 + " " + errtxt) | |
} else { | |
Prln("fail! [" + I2S(n) + "]: " + cmds[1]) | |
Prln("[[ " + a + " || " + b + " || " + c + " ]]") | |
SleepMS(3000) | |
n++ | |
} | |
} | |
FileDelete(rname) | |
logi, errL := FileInfo(log, false) | |
if errL == nil { | |
if logi.Size() == 0 { | |
FileDelete(app_path + log) | |
} | |
} | |
} | |
} | |
} | |
SleepMS(5000) | |
} | |
func GetCmd(v8exe string, backup_path, now_date string, arr []string, log string) [4]string { | |
sep := "\\" // server always, file for windows | |
bd := "/" + arr[0] + "\"" + FilePathEndSlashRemove(arr[1]) + sep + arr[2] + "\"" | |
bfname := arr[2] + "_" + now_date + "_" + arr[5] + "_" + arr[4] + "." | |
bf := backup_path + bfname | |
r := "\"" + v8exe + "\" CONFIG " + bd + " /DumpIB\"" + bf + "dt\" /N\"" + arr[3] + "\" /P\"" + arr[4] + "\"" | |
r = r + " 1> " + log + " 2>&1" | |
r = "start \"\"\"\" /wait " + r | |
return [4]string{r, bf + "dt", bf + "n1", bfname + "dt"} | |
} | |
func ExecCommand_simple(exe_name string, args ...string) (string, string, string) { | |
cmd := exec.Command(exe_name, args...) | |
var buffer_out bytes.Buffer | |
var buffer_err bytes.Buffer | |
cmd.Stdout = &buffer_out | |
cmd.Stderr = &buffer_err | |
err := cmd.Run() | |
if err != nil { | |
return "", "", err.Error() | |
} | |
return string(buffer_out.Bytes()), string(buffer_err.Bytes()), "" | |
} |
This file contains 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
D:\YandexDisk\1S_backup\Основная_Каждый_День\ | |
S 127.0.0.1 base_ut SudoUser password12345 конец_дня 1234567 | |
D:\YandexDisk\1S_backups\Бухгалтерия_По_Будням\ | |
S 127.0.0.1:1841 base_buhgalteria Админ password12345 3.0.75.70 12345 | |
D:\YandexDisk\1S_backups\Зарплата_По_Пятница\ | |
F C:\1s_local_bases\ base_zarplata Admin password12345 3.1.12.142 5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment