Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 9.89 KB | None | 0 0
  1. #!/bin/bash
  2. pathToNano="/bin/nano"
  3. nameTarBackUp="backup.tar"
  4. backUpExtention="bu"
  5. nameCheckSum="checksum"
  6. path=
  7. extention=
  8. savePath=
  9. filename=
  10. (( time=262080 ))
  11. (( bound=-1 ))
  12. usage="
  13. NAME
  14.  
  15.  BackUp -- program for creating backups for a given extension
  16.  
  17. DESCRIPTION:
  18.    -h, --help  
  19.      show this help text
  20.  
  21.    -p, --path  
  22.      path to the directory with the files to be archived
  23.  
  24.    -e, --extention  
  25.      archive files extension
  26.  
  27.    -s, --save-path
  28.      path to save the archive
  29.  
  30.    -n, --name
  31.      name of the archive to be created
  32.  
  33.    -a, --amount
  34.      maximum number of archives in the save directory
  35.  
  36.    -r, --repeat
  37.      archiving frequency flag
  38.  
  39.    -t, --time
  40.      time in minutes is period
  41.  
  42.    -d, --delete-jobs
  43.      delete all BackUp jobs from user crontab
  44.  
  45.    -c, --check
  46.      checks that the archive is intact
  47.  
  48.  You can choose the name of the archive yourself or it will <countOfCrontabJob>_<year>_<month>_<day>_<hour>_<minute>_<second> - date when archive is created.
  49.  Extention of archives is .bu (BackUp).
  50.  File archive extension is .tar.gz.
  51.  You have log in /tmp/BackUp.log
  52.  
  53. USAGE:
  54.    Attention!!!
  55.    You have to :
  56.      use absolute paths;
  57.      use all keys in lower case;
  58.      have access to all archived files;
  59.      use paths to the directories should be interpreted by encoding of your crontab, if you want to use periodicity;
  60.      have tar and gzip archivers;
  61.  
  62.    -h : to get help
  63.    -c -p <pathToArchive> : to get to check archive with sha256sum check
  64.    -p <pathToFiles> -e <extention> -s <pathToSaveArchive> -n <nameOfArchive> : saves archive with the name you specified
  65.    -r -p <pathToFiles> -e <extention> -s <pathToSaveArchive> [-a <number>] -t <number> : sets archiving repeatability, -t (default half of year in minutes 262_080)
  66.    -d : delete all BackUp jobs from user crontab
  67.  
  68. AUTHOR:
  69.  Vladislav Zaripov
  70.  
  71. Enjoyable use!
  72. "
  73. #set -x
  74. check_params () {
  75.     local code=1
  76.     if [ -d "$path" ] \
  77.     && [ -n "$extention" ] \
  78.     && [ -d "$savePath" ] \
  79.     && [ -n "$filename" ] \
  80.     && [ ! -e "${savePath}/${filename}.${backUpExtention}" ]
  81.     then
  82.         code=0
  83.     else
  84.       if [ ! -d "$path" ]
  85.       then
  86.         echo 'path is not exist'
  87.       elif [ -z "$extention" ]
  88.       then
  89.         echo 'extention is empty'
  90.       else
  91.         if [ ! -d "$savePath" ]
  92.         then
  93.           echo 'save path is not exist'
  94.         elif [ -z "$filename" ]
  95.         then
  96.           echo 'name is empty'
  97.         else
  98.           if [ -e "${savePath}/${filename}.${backUpExtention}" ]
  99.           then
  100.             echo 'this name has already been used'
  101.           fi
  102.         fi
  103.       fi
  104.     fi
  105.     return $code
  106. }
  107.  
  108. delete_jobs () {
  109.   export EDITOR="${pathToNano}"
  110.   crontab -l | grep -v "$(basename "${0}")" > mycron \
  111.   && crontab mycron \
  112.   && rm mycron
  113.   return $?
  114. }
  115.  
  116. set_repeatity () {
  117.   export EDITOR="${pathToNano}"
  118.   count_jobs=$(crontab -l | grep -a "\-s \"${savePath}\"" | grep -c -E "^\*/${time}")
  119.   if [ "$count_jobs" -gt 0 ]
  120.   then
  121.     echo 'This time has already been used! Please, choose another.'
  122.   else
  123.     crontab -l > mycron \
  124.     && echo "*/${time} * * * * "$(pwd)/$(basename ${0})" -p \""${path}"\" -e \""${extention}"\" -s \""${savePath}"\" -n $(crontab -l | grep "$(basename ${0})" | wc -l)_\$(date '+\%Y_\%m_\%d_\%H_\%M_\%S')" -a $bound '>>/tmp/BackUp.log 2>&1' >> mycron \
  125.    && crontab mycron \
  126.    && rm mycron
  127.  fi
  128.  return $?
  129. }
  130.  
  131. check_archive () {
  132.  local code=1
  133.  if [ -e "${path}/${nameCheckSum}" ]
  134.  then
  135.    code=0
  136.    cd "${path}" \
  137.    && sha256sum -c "${nameCheckSum}"
  138.  else
  139.    echo 'unexpected object'
  140.  fi
  141.  return $code
  142. }
  143.  
  144. erase () {
  145.  local code=0
  146.  local count=1
  147.  files_count=$(ls -t "${savePath}" | grep -E ".${backUpExtention}" | wc -l)
  148.  if [ "$bound" != "-1"] [ "$files_count" -gt 0 ] && [ "$files_count" -ge "$bound" ]
  149.  then
  150.    for file in $(ls -t "${savePath}" | grep -E ".${backUpExtention}")
  151.    do
  152.      if [ $count -ge "$bound" ]
  153.      then
  154.        if ! rm -r "${savePath:?}"/"$(basename "${file}")"
  155.        then
  156.          return 1
  157.        fi
  158.      fi
  159.      (( ++count ))
  160.    done
  161.  else
  162.    code=0
  163.  fi
  164.  return $code
  165. }
  166.  
  167. make_archive () {
  168.  local code=1
  169.  if collect_files
  170.  then
  171.    if [ -n "$bound" ] && [ "$bound" -eq "$bound" ] 2>/dev/null;
  172.    then
  173.      if erase && mkdir -m go-rwx "${path}/${filename}.${backUpExtention}" \
  174.      && gzip "${path}/${extention}_${nameTarBackUp}" \
  175.      && mv "${path}/${extention}_${nameTarBackUp}.gz" "${path}/${filename}.${backUpExtention}/${extention}_${nameTarBackUp}.gz" \
  176.      && mv "${path}/${filename}.${backUpExtention}" "${savePath}/${filename}.${backUpExtention}" \
  177.      && cd "${savePath}/${filename}.${backUpExtention}/" \
  178.      && sha256sum "${extention}_${nameTarBackUp}.gz" > "${nameCheckSum}"
  179.      then
  180.        code=0
  181.      else
  182.        echo 'command failed'
  183.      fi
  184.      else
  185.        echo 'argument is not a number'
  186.    fi
  187.  fi
  188.  return $code
  189. }
  190.  
  191. collect_files () { #https://unix.stackexchange.com/questions/407079/how-to-find-specific-file-types-and-tar-them Копейцев будет спрашивать все параметры, которые использовал!!!
  192.  local code=1
  193.  if check_params
  194.  then
  195.    find "${path}" -maxdepth 1 -name "*.${extention}" -print0 | tar -cvf "${path}/${extention}_${nameTarBackUp}" --null -T - \
  196.    && chmod -R go-rwx "${path}/${extention}_${nameTarBackUp}"
  197.    if [ "$(tar --list -f "${path}/${extention}_${nameTarBackUp}" | wc -l)" != "0" ]
  198.    then
  199.      code=0
  200.    else
  201.      echo 'no files with this extension'
  202.      rm "${path}/${extention}_${nameTarBackUp}"
  203.    fi
  204.  fi
  205.  return $code
  206. }
  207.  
  208. while [ "$1" != "" ]; do
  209.    case $1 in
  210.        -p | --path )           shift
  211.                                path=$1
  212.                                shift
  213.                                while [ "$1" != "" ]; do
  214.                                    case $1 in
  215.                                        -e | --extention )      shift
  216.                                                                extention=$1
  217.                                                                ;;
  218.                                        -s | --save-path )      shift
  219.                                                                savePath=$1
  220.                                                                ;;
  221.                                        -n | --name )           shift
  222.                                                                filename=$1
  223.                                                                ;;
  224.                                        -a | --amount )         shift
  225.                                                                (( bound=$1 ))
  226.                                                                ;;                                                                                
  227.                                        *)                      echo "incorrect you have to use -e for extention and -s for save-path, also -n for name"
  228.                                                                exit 1
  229.                                                                ;;
  230.                                    esac
  231.                                    shift
  232.                                done
  233.                                make_archive
  234.                                exit $?
  235.                                ;;
  236.        -r | --repeat )         shift                              
  237.                                while [ "$1" != "" ]; do
  238.                                    case $1 in
  239.                                        -p | --path )           shift
  240.                                                                path=$1                                                              
  241.                                                                ;;
  242.                                        -e | --extention )      shift
  243.                                                                extention=$1
  244.                                                                ;;
  245.                                        -s | --save-path )      shift
  246.                                                                savePath=$1
  247.                                                                ;;                                        
  248.                                        -a | --amount )         shift
  249.                                                                (( bound=$1 ))
  250.                                                                ;;
  251.                                        -t | --time )           shift
  252.                                                                (( time=$1 ))
  253.                                                                ;;
  254.                                        *)                      echo "incorrect you have to use -e for extention and -s for save-path, also -n for name"
  255.                                                                exit 1
  256.                                                                ;;
  257.                                    esac
  258.                                    shift
  259.                                done
  260.                                set_repeatity
  261.                                exit $?
  262.                                ;;
  263.        -d | --delete-jobs )    delete_jobs
  264.                                exit $?
  265.                                ;;
  266.        -c | --check )          shift
  267.                                path=$1
  268.                                check_archive
  269.                                exit $?
  270.                                ;;
  271.        -h | --help )           echo "$usage"
  272.                                exit 0
  273.                                ;;
  274.        * )                     echo "Arguments are incorrect"
  275.                                exit 1
  276.                                ;;
  277.    esac
  278.    shift
  279. done
  280. exit 0
  281. # set +x
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement