Good day to all.
The essence of the question under consideration is set out in the title, the story is divided into three parts. Separately below will be the texts of the scripts.
1) Preface
The question of the need for backup in automatic mode is not subject to doubt either among the luminaries or the beginners. In the article we will consider backup by means of 1C (which has several advantages over copying by means of DBMS). In this case, the batch launch tools of the 1C platform, powershell and the Windows task scheduler will be used.
')
The tasks of updating information have long been automated, but only for typical configurations, or those that use the library of standard subsystems. In my case, we are working with an old Alpha Auto Edition 4, which applies to 12 servers. Changes are made about twice a week, so the benefits of automation are obvious.
In both cases, we have the following source data:
- Windows Server operating system (versions from 2008 to 2012);
- Client-server version of the platform 1C 8.3 (with the obligatory installed component COM-connection).
Of course, we have the credentials of the OS and 1C with administrative rights.
To update the configuration, you also need an ftp server.
In order not to inflate the article, I recommend reading about
powershell and about the
Windows scheduler separately.
2) Backup
After reading these links, we already know what needs to be done to run the powershell script, so I’ll get straight to the point.
It is very simple to make a backup of the information database in batch mode, you just need to “kick out” all users. We will do this by connecting a COM object to the database. In our example, this is done by the ExitAll function. It is sewn into the function body that it is called on the server on which the cluster of 1C servers is actually installed. Call this function without any parameters in your script on the server - and ALL users from ALL database bases will fly out.
I apologize to the person whose code I used when writing this procedure - the authorship could not be restored.
After that, call BackUpBase function with the parameter - the name of the infobase. I have a service administrator in all IBs with the same credentials, so I’m just a hardcore one. If necessary, you can parameterize them, or bypass OS authentication.
The final script is saved to a file.
In the task scheduler we create the “Simple Task”, the name, of course, at your discretion.
I work every day, but even here the owner is a master. It is best to run at night when no one is working, for example, at 3:00. Action for the task - "Run the program." The program itself is “powershell.exe”. But her arguments -
-File "D:\1C_automatization\ExitAllUsersAndBackup.ps1" -ExecutionPolicy RemoteSigned
where ExitAllUsersAndBackup.ps1 is our saved script.
-ExecutionPolicy RemoteSigned - a key that allows the execution of powershell batch scripts if they are not globally enabled on the system. It works through time (perhaps, there is not enough competence to figure it out, but I haven’t found any patterns). In cases when it does not work with this key, it is necessary to allow execution of scripts for the entire server.
For this Win + R, powershell.exe,
Set-ExecutionPolicy RemoteSigned
and confirm the action.
Working time with these scripts is more than three months. Interruptions were, but are associated with outages and other external factors.
3) Configuration update
After all users have logged out (or kicked out, as in the previous case), you can update the configuration. The presence of scheduled tasks may prevent the update, since from the moment all users are disconnected and the configurator is opened to load the configuration, some task may well begin to work. Therefore, the schedule should be considered.
We store the configuration on an ftp server on which we place it manually. The configuration file is called GK.cf, in the given example only one configuration is updated. Potentially, you can also update several different configurations.
On ftp next to GK.cf put a file called flag.txt. The presence of this file indicates that it is necessary to update. You can check for the presence of the GK.cf file itself, but we also use the flag for other purposes.
The script works as follows:
- Removes GK.cf and flag.txt, if there are any in the working directory (the user on whose behalf the scheduler will run must have write access to this directory);
- An attempt is made to download a flag file;
- If this download file turned out - download .cf;
- Actually, the update function UpdateCf.
Unlike the previous case, the launch parameters of 1C are not transmitted by an array, but in the usual command line mode.
The reliability of this script is slightly less. The update runs to the end at 100% in cases where the metadata structure changes. In other cases it happens, as I warned earlier, that an active user appears. As a result, the configuration is loaded into the database, but the database configuration is not updated (again I apologize for such crooked terminology to people who are not associated with 1C). The rest is normal flight.
Backup script
# ++ $BAKUPDIR= $CEXE= # -- function ExitAll{ # 1 # , # : , #$SrvAddr = $SrvAddr = ######################################## $V83Com = New-Object -COMObject # $ServerAgent = $V83Com.ConnectAgent($SrvAddr) $ClusterFound = $FALSE # $Clasters = $ServerAgent.GetClusters() foreach ($Claster in $Clasters){ # # , , # $ServerAgent.Authenticate($Claster,,) # $Sessions = $ServerAgent.GetSessions($Claster) write-host $Sessions.Count foreach ($Session in $Sessions){ $ServerAgent.TerminateSession($Claster,$Session)} if (($Sessions.Count -eq 0)){ continue} # # , # $WorkingProcesses = $ServerAgent.GetWorkingProcesses($Claster) foreach ($WorkingProcess in $WorkingProcesses){ if ($WorkingProcess.Running -eq 1){ write-host $WorkingProcess.PID #!!! 1 Stop-Process $WorkingProcess.PID -Force}} } $V83Com = } function BackUpBase($IbName){ # $NOWDATETIME = Get-Date -UFormat #, ( , ) $TARGETDIR = $BAKUPDIR # $BAKFN=$TARGETDIR + $IbName + + $NOWDATETIME + # $PARAMS = (,,,,,,,,,$BAKFN) #, &$CEXE $PARAMS } # ExitAll # BackUpBase RingAlpha BackUpBase MazdaAlpha BackUpBase RingAcc BackUpBase RingZup BackUpBase MazdaHRM BackUpBase MazdaAcc BackUpBase RingAcc48 BackUpBase RingHRM48 BackUpBase RingAccNorth BackUpBase RingHRMNorth
Configuration update script
function Download_File ($si_UserName, $si_Password, $si_Source_Path, $si_Target_Path){ $oi_Credentials = New-Object System.Net.NetworkCredential($si_UserName,$si_Password) $oi_Web_Client = New-Object System.Net.WebClient $oi_Web_Client.Credentials = $oi_Credentials $oi_Web_Client.DownloadFile($si_Source_Path, $si_Target_Path) while($oi_Web_Client.IsBusy){} } Clear-Host; $curDir = $MyInvocation.MyCommand.Definition | split-path -parent $curDir = $curDir + "\" $server = "111.222.33.4" #FTP $flagfoldername = "/1C/flag.txt" # - $flagurl = "ftp://$server$flagfoldername" #url $cffoldername = "/1C/GK.cf" # $cfurl = "ftp://$server$cffoldername" #url $login = "MYFTPUSER" #ftp- $password = "MYFTPPWD" #ftp- $flagfile = $curDir + "flag.txt" $cffile = $curDir + "GK.cf" # $SERVERNAME="127.0.0.1" $CEXE="C:\Program Files (x86)\1cv8\common\1cestart.exe" $ADMINNAME="1CUSER" $ADMINPASS="1CPWD" function UpdateCf($IBNameLocal){ &$CEXE 'DESIGNER' '/S' $SERVERNAME'\'$IBNameLocal '/N' $ADMINNAME '/P' $ADMINPASS '/LoadCfg' $cffile '/UpdateDBCfg' '-Server' } function FileExist($fpath){ $isfile = Test-Path $fpath return ($isfile -eq "True") } #, if (FileExist $flagfile){ Remove-Item $flagfile } if (FileExist $cffile){ Remove-Item $cffile } Download_File $login $password $flagurl $flagfile if (FileExist $flagfile){ Download_File $login $password $cfurl $cffile if (FileExist $cffile){# UpdateCf "MazdaAlpha" UpdateCf "RingAlpha" } }
Thank you for attention. Ready for constructive criticism.