Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #=============================
- fileName=$(basename $0)
- fileSettingsName="$fileName""_settings.txt"
- #dirFolderSc=$(dirname $0)
- #echo $fileName
- #echo $fileSettingsName
- #echo $dirFolderSc
- dirFolderSc=$(dirname $(realpath $fileName))
- cd "$dirFolderSc"
- if ! [ -f "$fileSettingsName" ]; then
- echo -e "\e[33;1mError: ""$fileSettingsName"" not found, but it was created with default settings"
- touch "$fileSettingsName"
- echo -e "language: eng\n""amountArchives: 3" > "$fileSettingsName"
- fi
- #=============================
- #=============================
- d=$(date +%H:%M:%S_%d.%m.%y)
- forHelp=0
- lang=$(grep "language:" $fileSettingsName)
- amount=$(grep "amountArchives:" $fileSettingsName)
- if ! [[ -n "$lang" && -n "$amount" ]]; then
- echo -e "\e[31;1mError in ""$fileSettingsName"": Script can not read settings from file, new ""$fileSettingsName"" created successfully"
- echo -e "language: eng\n""amountArchives: 3" > "$fileSettingsName"
- exit 1
- fi
- lang=${lang:10}
- amount=${amount:16}
- archiveName="backup_$d.zip"
- #=============================
- #=============================
- task="-q"
- if [ -n "$1" ]; then
- task=$1
- else
- if [ "$lang" == "eng" ]; then
- echo -e "\e[31;1mError in argument_1: Argument is undefined" && exit 1
- elif [ "$lang" == "rus" ]; then
- echo -e "\e[31;1mОшибка в аргумент_1: Аргумент не найден" && exit 1
- fi
- fi
- #=============================
- #====================================================================================================================
- # Archiving / Архивирование
- #====================================================================================================================
- if [[ "$task" == "-a" || "$task" == "--archive" ]]; then
- #=============================
- ext=""
- if [ -n "$2" ]; then
- ext=$2
- else
- if [ "$lang" == "eng" ]; then
- echo -e "\e[31;1mError in argument_2: Argument is undefined" && exit 1
- elif [ "$lang" == "rus" ]; then
- echo -e "\e[31;1mОшибка в аргумент_2: Аргумент не найден" && exit 1
- fi
- fi
- #=============================
- #=============================
- dirFolder=$(pwd)
- if [ -n "$3" ]; then
- if [ -d $3 ]; then
- dirFolder=$3
- else
- if [ "$lang" == "eng" ]; then
- echo -e "\e[31;1mError in argument_3: No such directory" && exit 1
- elif [ "$lang" == "rus" ]; then
- echo -e "\e[31;1mОшибка в аргумент_3: Данная директория не существует" && exit 1
- fi
- fi
- else
- if [ "$lang" == " eng" ]; then
- echo -e "\e[31;1mError in argument_3: Argument is undefined" && exit 1
- elif [ $lang==rus ]; then
- echo -e "\e[31;1mОшибка в аргумент_3: Аргумент не найдён" && exit 1
- fi
- fi
- #=============================
- #=============================
- dirOutput=$dirFolder
- if [ -n "$4" ]; then
- if ! [ -d $4 ]; then
- forHelp=0
- mkdir -p $4 2>/dev/null || forHelp=1
- if [ "$forHelp" == "0" ]; then
- if [ "$lang" == "eng" ]; then
- echo -e "\e[33;1mError in argument_4: No such directory, but it was created"
- elif [ "$lang" == "rus" ]; then
- echo -e "\e[33;1mОшибка в аргумент_4: Данная директория не существует, но была создана"
- fi
- fi
- if [ "$forHelp" == "1" ]; then
- if [ "$lang" == "eng" ]; then
- echo -e "\e[31;1mError in argument_4: No such directory and it can not be created" && exit 1
- elif [ "$lang" == "rus" ]; then
- echo -e "\e[31;1mОшибка в аргумент_4: Данная директория не существует и не может быть создана" && exit 1
- fi
- fi
- fi
- dirOutput=$4
- #cd $dirOutput
- #if [ -d "$dirOutput""backup_""$d""_folder" ]; then
- # if [ "$lang" == "eng" ]; then
- # echo -e "\e[33;1mError in argument_4: At this point of time the archive of this directory has been already created"
- # elif [ "$lang" == "rus" ]; then
- # echo -e "\e[33;1mОшибка в аргумент_4: В данный момент времени архив соотвествующей директории уже создан"
- # fi
- # sleep 1
- #fi
- fi
- #=============================
- #=============================
- amountArchivesCreate=1
- if [ -n "$5" ]; then
- if [ "$5" == "1" ]; then
- cd $dirOutput
- am=$(find . -maxdepth 1 -type d -name "backup_*_folder" | wc -l)
- let "am = am + 1"
- #echo am="$am"
- if [[ $am > $amount ]]; then
- let "count = $am - $amount"
- #echo count="$count"
- for (( i=0; i<$count; i++)); do
- for j in "$dirOutput""backup_"*"_folder"; do
- #echo j="$j"
- rm -r $j
- break
- done
- done
- fi
- elif ! [ "$5" == "0" ]; then
- if [ "$lang" == "eng" ]; then
- echo -e "\e[33;1mError in argument_5: This value in unexpected (expected 0 or 1), argument_5 is set to 0"
- elif [ "$lang" == "rus" ]; then
- echo -e "\e[33;1mОшибка в аргумент_5: Непредвиденное значение аргумента (ожидалось 0 или 1), аргумент_5 установлен равным 0"
- fi
- fi
- fi
- #=============================
- #=============================
- cd $dirFolder
- find . -name '*.'"$ext" | zip "$archiveName" -@ -q
- #=============================
- #=============================
- cd $dirOutput
- mkdir $archiveName"_folder"
- cd $dirFolder
- cp $archiveName "$dirOutput""$archiveName""_folder"
- echo $(cksum $archiveName) > "$archiveName""_cksum.txt"
- cp "$archiveName""_cksum.txt" "$dirOutput""$archiveName""_folder"
- rm $archiveName
- rm "$archiveName""_cksum.txt"
- #=============================
- #=============================
- if [ "$lang" == "eng" ]; then
- echo -e "\e[32;1mArchiving successed"
- elif [ "$lang" == "rus" ]; then
- echo -e "\e[32;1mАрхивация успешна"
- fi
- exit 0
- #=============================
- #====================================================================================================================
- # Checking the integrity of files / Проверка целостности архива
- #====================================================================================================================
- elif [[ "$task" == "-c" || "$task" == "--check" ]]; then
- #=============================
- if [ -n "$2" ]; then
- if [ -f "$2" ]; then
- fileArchiveName=$2
- else
- if [ "$lang" == "eng" ]; then
- echo -e "\e[31;1mError in argiment_2: No such directory" && exit 1
- elif [ "$lang" == "rus" ]; then
- echo -e "\e[31;1mОшибка в аргумент_2: Данная директория не существует" && exit 1
- fi
- fi
- else
- if [ "$lang" == "eng" ]; then
- echo -e "\e[31;1mError in argument_2: Argument is undefined" && exit 1
- elif [ "$lang" == "rus" ]; then
- echo -e "\e[31;1mОшибка в аргумент_2: Аргумент не найден" && exit 1
- fi
- fi
- #=============================
- #=============================
- folderArchiveName=$(dirname $fileArchiveName)
- cd $folderArchiveName
- fileCksumName="$fileArchiveName""_cksum.txt"
- cksumArchive=$(cksum $fileArchiveName)
- IFS=' ' read -r -a array1 <<< "$cksumArchive"
- cksumArchive=${array1[0]}
- cksumFile=$(head $fileCksumName)
- IFS=' ' read -r -a array2 <<< "$cksumFile"
- cksumFile=${array2[0]}
- #=============================
- #=============================
- if [ "$cksumFile" == "$cksumArchive" ]; then
- if [ "$lang" == "eng" ]; then
- echo -e "\e[32;1mThe integrity of the archive is not violated"
- elif [ "$lang" == "rus" ]; then
- echo -e "\e[32;1mЦелостность архива не нарушена"
- fi
- else
- if [ "$lang" == "eng" ]; then
- echo -e "\e[31;1mThe integrity of the archive is violated" && exit 1
- elif [ "$lang" == "rus" ]; then
- echo -e "\e[31;1mЦелостность архива нарушена" && exit 1
- fi
- fi
- #=============================
- #====================================================================================================================
- # Setting the frequency of the script / Установка периодичности работы скрипта
- #====================================================================================================================
- elif [[ "$task" == "-p" || "$task" == "--period-set" ]]; then
- #=============================
- #if [ -n "$2" ]; then
- # period=$2
- #else
- # if [ "$lang" == "eng" ]; then
- # echo -e "\e[31;1mError in argument_2: Argument is undefined" && exit 1
- # elif [ "$lang" == "rus" ]; then
- # echo -e "\e[31;1mОшибка в аргумент_2: Аргумент не найден" && exit 1
- # fi
- #fi
- #=============================
- #=============================
- #forHelp=0
- #let "period += 0" 2>/dev/null || forHelp=1
- #if [ "$forHelp" == "1" ]; then
- # if [ "$lang" == "eng" ]; then
- # echo -e "\e[31;1mError in argument_2: NaN" && exit 1
- # elif [ "$lang" == "rus" ]; then
- # echo -e "\e[31;1mОшибка в аргумент_2: NaN" && exit 1
- # fi
- #fi
- #=============================
- #=============================
- if [ "$lang" == "eng" ]; then
- echo -e "\e[32;1mEnter the command, that (crontab) will execute at the required frequency"
- elif [ "$lang" == "rus" ]; then
- echo -e "\e[32;1mВведите команду, которую (crontab) будет исполнять в с требуемой периодичностью"
- fi
- read strCommand
- #=============================
- #=============================
- if [ "$lang" == "eng" ]; then
- echo -e "\e[32;1mEnter frequency of script startup (crontab syntax)"
- elif [ "$lang" == "rus" ]; then
- echo -e "\e[32;1mВведите периодичность запуска скрипта (синтаксис crontab)"
- fi
- read strFreq
- #=============================
- #=============================
- cd $dirFolderSc
- echo -e "#!/bin/bash/\n""$strCommand"" 2>&1" > "$fileName""_crontab.sh"
- chmod ugo+x "$fileName""_crontab.sh"
- #/home/user/Desktop/hw/script.sh -a txt /home/user/Desktop/files/ /home/user/Desktop/b/ 1
- #=============================
- #=============================
- crontab -l | crontab -
- echo "$strFreq"" ""$dirFolderSc""/""$fileName""_crontab.sh" | crontab -
- #=============================
- #====================================================================================================================
- # Changing script parameters / Изменение параметров скрипта
- #====================================================================================================================
- elif [[ "$task" == "-s" || "$task" == "--settings" ]]; then
- #=============================
- newLang="eng"
- newAmount=1
- #=============================
- #=============================
- if [ -n "$2" ]; then
- if [ "$2" == "eng" || "$2" == "rus" ]; then
- newLang="$2"
- else
- if [ "$lang" == "eng" ]; then
- echo -e "\e[31;1mError in setting_2: This language is not supported" && exit 1
- elif [ "$lang" == "rus" ]; then
- echo -e "\e[31;1mОшибка в параметр_2: Данный язык не поддерживается" && exit 1
- fi
- fi
- else
- if [ "$lang" == "eng" ]; then
- echo -e "\e[31;1mError in setting_2: Setting in undefind" && exit 1
- elif [ "$lang" == "rus" ]; then
- echo -e "\e[31;1mОшибка в параметр_2: Параметр не найден" && exit 1
- fi
- fi
- #=============================
- #=============================
- if [ -n "$3" ]; then
- forHelp=$(expr "$3" + 0 2>/dev/null)
- if [ -n "forHelp" ]; then
- newAmount="$3"
- else
- if [ "$lang" == "eng" ]; then
- echo -e "\e[31;1mError in setting_3: Setting in invalid" && exit 1
- elif [ "$lang" == "rus" ]; then
- echo -e "\e[31;1mОшибка в параметр_3: Параметр недействителен" && exit 1
- fi
- fi
- else
- if [ "$lang" == "eng" ]; then
- echo -e "\e[31;1mError in setting_3: Setting in undefind" && exit 1
- elif [ "$lang" == "rus" ]; then
- echo -e "\e[31;1mОшибка в параметр_3: Параметр не найден" && exit 1
- fi
- fi
- #=============================
- #=============================
- echo -e "language: ""$newLang""\n""amountArchives: ""$newAmount" > "$fileSettingsName"
- if [ "$newLang" == "eng" ]; then
- echo -e "\e[32;1mSaving settings successed"
- elif [ "$newLang" == "rus" ]; then
- echo -e "\e[32;1mСохранение настроек успешно"
- fi
- #=============================
- #====================================================================================================================
- # Help / Справка
- #====================================================================================================================
- elif [[ "$task" == "-h" || "$task" == "--help" ]]; then
- if [ "$lang" == "eng" ]; then
- echo ""
- echo "DESCRIPTION"
- echo " This script allows you to archive the desired directory, check the integrity of the archive files, set the creation of backup copies of certain directories with some frequency."
- echo ""
- echo "ARGUMENTS"
- echo " -a [arg_2] [arg_3] [arg_4] [arg_5] ... --archive"
- echo " Creates archive of files with extension ([arg_2]=extension of files)"
- echo " at folder ([arg_3]=path to folder) and will store archive to the"
- echo " folder ([arg_4]=path to folder for archive) and if next argument"
- echo " ([arg_5]={0 or 1}, 1 - will use second parametr in file:"
- echo " 'name_of_script_date'_folder, 0 - will not use.)"
- echo " -c [arg_2] [arg_3] [arg_4]"
- elif [ "$lang" == "rus" ]; then
- echo "ОПИСАНИЕ"
- echo " Данный скрипт позволяет заархивировать нужную директорию, проверить целостность файлов архива, установить создание резервных копий определённых директорий с некоторой периодичностью."
- echo "АРГУМЕНТЫ"
- echo ""
- fi
- #====================================================================================================================
- # Quit from script / Выход из скрипта
- #====================================================================================================================
- elif [[ "$task" == "-q" || "$task" == "--quit" ]]; then
- exit 0;
- fi
- exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement