Для работы командного файла необходимо проверить наличие в системе всех используемых в файле компонентов платформы 1С. А именно:
- Конфигуратора;
- Сервера администрирования ras;
- Сервер администрирования должен быть зарегистрирован и запущен;
Проверить наличие необходимых прав пользователя 1С и пользователя linux, наличия доступа к используемым сетевым папкам.
После подготовительной работы и назначения своих значений переменных в командном файле архивации, он готов к применению по расписанию в Планировщике заданий cron.
#!/bin/bash
clear
#exec 1>> ~/backup_1cv8.log
#export DISPLAY=:1
readonly SRC1C=/opt/1cv8/x86_64/8.3.22.1851
#readonly SRC1C=/opt/1C/v8.3/x86_64
#readonly SRC1C=/opt/1C/v8.3/i386
readonly RUN1C=$SRC1C/1cv8
readonly RAC=$SRC1C/rac
readonly RAS=$SRC1C/ras
readonly COMMENT_PROCESS=YES
readonly SERVER_NAME=localhost
readonly IB_USER=Администратор
readonly IB_PASS=321
readonly PERMISSION_CODE="Cron-backup"
readonly SUCCESS_MESSAGE_LOG="Infobase successfully dumped"
readonly DESTINATION_FOLDER=/mnt/backup/backup1c
readonly RESERVE_LOCATION=~/reservelocation
readonly SEND_MAIL_SUCCESS=NO
readonly SEND_MAIL_FAIL=NO
readonly MAILTO=""
readonly MAILFROM=""
declare -A IB_LIST
declare -A ERRORS
declare SUCCESS_COUNT=0
declare CLUSTER_UUID
declare IB_NAME
declare CURRENT_FILENAME
declare BACKUP_FILE
function makeBackupDestinationFolder() {
[[ "$COMMENT_PROCESS" == "YES" ]] && echo "Making destination folders if needed"
if [ ! -d "$DESTINATION_FOLDER/$1" ]; then
mkdir -p $DESTINATION_FOLDER/$1
fi
if [ ! -d "$DESTINATION_FOLDER/$1/log" ]; then
mkdir -p $DESTINATION_FOLDER/$1/log
fi
}
function startRAS() {
[[ "$COMMENT_PROCESS" == "YES" ]] && echo "Starting 1C Remote Administration Server"
$RAS --daemon cluster
}
function getClusterUUID() {
[[ "$COMMENT_PROCESS" == "YES" ]] && echo "Retriving cluster UUID"
if [ -z "$CLUSTER_UUID" ]; then
CLUSTER_UUID=$($RAC cluster list | grep '^cluster* *: ' | awk '{print ($3)}')
fi
}
function getIBList() {
[[ "$COMMENT_PROCESS" == "YES" ]] && echo "Retriving Informaion Bases list"
local keys=()
for key in `$RAC infobase --cluster=$CLUSTER_UUID summary list | egrep '^infobase *: ' | awk '{print($3)}'`; do
keys+=( $key )
done
local values=()
for value in `$RAC infobase --cluster=$CLUSTER_UUID summary list | egrep '^name *: ' | awk '{print($3)}'`; do
values+=( $value )
done
local i=0
for k in ${keys[*]}
do
IB_LIST+=( [$k]=${values[$i]} )
let i=i+1
done
}
function blockIbByUUID() {
[[ "$COMMENT_PROCESS" == "YES" ]] && echo "Setting access block at" $1
if [ ! -z "$1" ]; then
local IB_NAME=${IB_LIST[$1]}
local DENIED_DAYTIME=$(date +%Y-%m-%d)'T'$(date +%H:%M:%S)
local DENIED_MSG="Для администратора:
Чтобы принудительно разблокировать информационную базу, воспользуйтесь консолью кластера серверов
или запустите \"1С:Предприятие\" с параметрами:
ENTERPRISE /S \"$server\\$IB_NAME\" /CРазрешитьРаботуПользователей /UC $PERMISSION_CODE"
$RAC infobase update \
--cluster=$CLUSTER_UUID \
--infobase=$1 \
--sessions-deny=on \
--permission-code=$PERMISSION_CODE \
--denied-from="$DENIED_DAYTIME" \
--denied-to="" \
--denied-message="$DENIED_MSG" \
--scheduled-jobs-deny=on \
--infobase-user=$IB_USER \
--infobase-pwd=$IB_PASS
fi
}
function unBlockIbByUUID() {
[[ "$COMMENT_PROCESS" == "YES" ]] && echo "Unsetting access block at" $1
if [ ! -z "$1" ]; then
$RAC infobase update \
--cluster=$CLUSTER_UUID \
--infobase=$1 \
--sessions-deny=off \
--permission-code="" \
--denied-to="" \
--denied-message="" \
--scheduled-jobs-deny=off \
--infobase-user=$IB_USER \
--infobase-pwd=$IB_PASS
fi
}
function terminateIBSessions() {
[[ "$COMMENT_PROCESS" == "YES" ]] && echo "Terminating users sessions in" $1
if [ ! -z "$1" ]; then
local sessionsList=$(getSessionsUUID $1)
for session in ${sessionsList[*]}; do
terminateSessionByUUID $session
done
fi
}
function getSessionsUUID() {
if [ ! -z "$1" ]; then
local infobaseUUID=$(getInfobaseUUID $1)
if [ ! -z $infobaseUUID ]; then
local sessionsList=()
for session in $($RAC session list --cluster=$CLUSTER_UUID --infobase=$infobaseUUID | grep '^session* *: ' | awk '{print($3)}'); do
sessionsList+=($session)
done
fi
fi
echo ${sessionsList[*]}
}
function getInfobaseUUID() {
if [ ! -z "$1" ]; then
for key in ${!IB_LIST[*]}; do
if [ "${IB_LIST[$key]}" == "$1" ]; then
local ret=$key
fi
done
echo $ret
fi
}
function terminateSessionByUUID() {
[[ "$COMMENT_PROCESS" == "YES" ]] && echo "Terminating user session" $1
if [ ! -z "$1" ]; then
$RAC session terminate --cluster=$CLUSTER_UUID --session=$1
fi
}
function dumpIB() {
[[ "$COMMENT_PROCESS" == "YES" ]] && echo "Dumping IB" $1
if [ ! -z "$1" ]; then
local DATE=$(date +%Y.%m.%d-%H.%M.%S)
local LOG_FILE=$DESTINATION_FOLDER/$1/log/$1-$DATE-backup.log
CURRENT_FILENAME=$1-$DATE.dt
BACKUP_FILE=$DESTINATION_FOLDER/$1/$CURRENT_FILENAME
$RUN1C CONFIG /S $SERVER_NAME/$1 /N $IB_USER /P $IB_PASS /DumpIB $BACKUP_FILE /UC $PERMISSION_CODE /OUT $LOG_FILE
local txtLog=$(cat $LOG_FILE | sed 's/.$//')
if [ "$txtLog" == "A279;$SUCCESS_MESSAGE_LOG" ]; then
[[ "$COMMENT_PROCESS" == "YES" ]] && echo "Dump $1 completed"
let SUCCESS_COUNT=SUCCESS_COUNT+1
rm -fr $LOG_FILE
else
ERRORS+=( [$1]=$txtLog )
[[ "$COMMENT_PROCESS" == "YES" ]] && echo "Dump $1 failed: ${ERRORS[$1]}"
fi
fi
}
function printDumpStatistics() {
local separator="----------------------------------------------------------"
local i=1
echo -e "$separator"
echo -e "|\tDump statistics"
echo -e "$separator"
echo -e "|\tSuccessfully dumped:\t$SUCCESS_COUNT"
echo -e "|\tFailed to dump:\t\t${#ERRORS[*]}"
if [ ${#ERRORS[*]} -ne 0 ]; then
echo -e "|\tDump fails at:"
for IB in ${!ERRORS[*]}; do
echo -e "|\t$i)\tInfobase $IB"
echo -e "|\t\tError:"
echo -e "${ERRORS[$IB]}" | sed 's/^/|\t\t/'
echo "$separator"
let i=i+1
done
else
echo -e "$separator"
fi
}
function sendStatisticsByMail() {
if [ "$SEND_MAIL_SUCCESS" == "YES" ] && [ ${#ERRORS[*]} -eq 0 ]; then
echo -e "$(printDumpStatistics)" | mail -s "Dump statistics" -r $USER\<$MAILFROM\> $MAILTO
fi
if [ "$SEND_MAIL_FAIL" == "YES" ] && [ ${#ERRORS[*]} -ne 0 ]; then
echo -e "$(printDumpStatistics)" | mail -s "Dump statistics" -r $USER\<$MAILFROM\> $MAILTO
fi
}
function manageFiles() {
cp $BACKUP_FILE $RESERVE_LOCATION/$1/$CURRENT_FILENAME
find $DESTINATION_FOLDER/$1 -type f -mtime +5 -delete
find $RESERVE_LOCATION/$1 -type f -mtime +5 -delete
}
function main() {
startRAS
getClusterUUID
getIBList
local bases=(${IB_LIST[*]})
if [ -n "$IB_NAME" ]; then
local bases=($IB_NAME)
fi
for base in ${bases[@]}; do
makeBackupDestinationFolder $base
blockIbByUUID $(getInfobaseUUID $base)
terminateIBSessions $base
dumpIB $base
unBlockIbByUUID $(getInfobaseUUID $base)
manageFiles $base
done
[[ "$COMMENT_PROCESS" == "YES" ]] && printDumpStatistics
sendStatisticsByMail
}
main
exit 0