Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- pathToNano="/bin/nano"
- nameTarBackUp="backup.tar"
- backUpExtention="bu"
- nameCheckSum="checksum"
- path=
- extention=
- savePath=
- filename=
- (( time=262080 ))
- (( bound=-1 ))
- usage="
- NAME
- BackUp -- program for creating backups for a given extension
- DESCRIPTION:
- -h, --help
- show this help text
- -p, --path
- path to the directory with the files to be archived
- -e, --extention
- archive files extension
- -s, --save-path
- path to save the archive
- -n, --name
- name of the archive to be created
- -a, --amount
- maximum number of archives in the save directory
- -r, --repeat
- archiving frequency flag
- -t, --time
- time in minutes is period
- -d, --delete-jobs
- delete all BackUp jobs from user crontab
- -c, --check
- checks that the archive is intact
- You can choose the name of the archive yourself or it will <countOfCrontabJob>_<year>_<month>_<day>_<hour>_<minute>_<second> - date when archive is created.
- Extention of archives is .bu (BackUp).
- File archive extension is .tar.gz.
- You have log in /tmp/BackUp.log
- USAGE:
- Attention!!!
- You have to :
- use absolute paths;
- use all keys in lower case;
- have access to all archived files;
- use paths to the directories should be interpreted by encoding of your crontab, if you want to use periodicity;
- have tar and gzip archivers;
- -h : to get help
- -c -p <pathToArchive> : to get to check archive with sha256sum check
- -p <pathToFiles> -e <extention> -s <pathToSaveArchive> -n <nameOfArchive> : saves archive with the name you specified
- -r -p <pathToFiles> -e <extention> -s <pathToSaveArchive> [-a <number>] -t <number> : sets archiving repeatability, -t (default half of year in minutes 262_080)
- -d : delete all BackUp jobs from user crontab
- AUTHOR:
- Vladislav Zaripov
- Enjoyable use!
- "
- #set -x
- check_params () {
- local code=1
- if [ -d "$path" ] \
- && [ -n "$extention" ] \
- && [ -d "$savePath" ] \
- && [ -n "$filename" ] \
- && [ ! -e "${savePath}/${filename}.${backUpExtention}" ]
- then
- code=0
- else
- if [ ! -d "$path" ]
- then
- echo 'path is not exist'
- elif [ -z "$extention" ]
- then
- echo 'extention is empty'
- else
- if [ ! -d "$savePath" ]
- then
- echo 'save path is not exist'
- elif [ -z "$filename" ]
- then
- echo 'name is empty'
- else
- if [ -e "${savePath}/${filename}.${backUpExtention}" ]
- then
- echo 'this name has already been used'
- fi
- fi
- fi
- fi
- return $code
- }
- delete_jobs () {
- export EDITOR="${pathToNano}"
- crontab -l | grep -v "$(basename "${0}")" > mycron \
- && crontab mycron \
- && rm mycron
- return $?
- }
- set_repeatity () {
- export EDITOR="${pathToNano}"
- count_jobs=$(crontab -l | grep -a "\-s \"${savePath}\"" | grep -c -E "^\*/${time}")
- if [ "$count_jobs" -gt 0 ]
- then
- echo 'This time has already been used! Please, choose another.'
- else
- crontab -l > mycron \
- && 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 \
- && crontab mycron \
- && rm mycron
- fi
- return $?
- }
- check_archive () {
- local code=1
- if [ -e "${path}/${nameCheckSum}" ]
- then
- code=0
- cd "${path}" \
- && sha256sum -c "${nameCheckSum}"
- else
- echo 'unexpected object'
- fi
- return $code
- }
- erase () {
- local code=0
- local count=1
- files_count=$(ls -t "${savePath}" | grep -E ".${backUpExtention}" | wc -l)
- if [ "$bound" != "-1"] [ "$files_count" -gt 0 ] && [ "$files_count" -ge "$bound" ]
- then
- for file in $(ls -t "${savePath}" | grep -E ".${backUpExtention}")
- do
- if [ $count -ge "$bound" ]
- then
- if ! rm -r "${savePath:?}"/"$(basename "${file}")"
- then
- return 1
- fi
- fi
- (( ++count ))
- done
- else
- code=0
- fi
- return $code
- }
- make_archive () {
- local code=1
- if collect_files
- then
- if [ -n "$bound" ] && [ "$bound" -eq "$bound" ] 2>/dev/null;
- then
- if erase && mkdir -m go-rwx "${path}/${filename}.${backUpExtention}" \
- && gzip "${path}/${extention}_${nameTarBackUp}" \
- && mv "${path}/${extention}_${nameTarBackUp}.gz" "${path}/${filename}.${backUpExtention}/${extention}_${nameTarBackUp}.gz" \
- && mv "${path}/${filename}.${backUpExtention}" "${savePath}/${filename}.${backUpExtention}" \
- && cd "${savePath}/${filename}.${backUpExtention}/" \
- && sha256sum "${extention}_${nameTarBackUp}.gz" > "${nameCheckSum}"
- then
- code=0
- else
- echo 'command failed'
- fi
- else
- echo 'argument is not a number'
- fi
- fi
- return $code
- }
- collect_files () { #https://unix.stackexchange.com/questions/407079/how-to-find-specific-file-types-and-tar-them Копейцев будет спрашивать все параметры, которые использовал!!!
- local code=1
- if check_params
- then
- find "${path}" -maxdepth 1 -name "*.${extention}" -print0 | tar -cvf "${path}/${extention}_${nameTarBackUp}" --null -T - \
- && chmod -R go-rwx "${path}/${extention}_${nameTarBackUp}"
- if [ "$(tar --list -f "${path}/${extention}_${nameTarBackUp}" | wc -l)" != "0" ]
- then
- code=0
- else
- echo 'no files with this extension'
- rm "${path}/${extention}_${nameTarBackUp}"
- fi
- fi
- return $code
- }
- while [ "$1" != "" ]; do
- case $1 in
- -p | --path ) shift
- path=$1
- shift
- while [ "$1" != "" ]; do
- case $1 in
- -e | --extention ) shift
- extention=$1
- ;;
- -s | --save-path ) shift
- savePath=$1
- ;;
- -n | --name ) shift
- filename=$1
- ;;
- -a | --amount ) shift
- (( bound=$1 ))
- ;;
- *) echo "incorrect you have to use -e for extention and -s for save-path, also -n for name"
- exit 1
- ;;
- esac
- shift
- done
- make_archive
- exit $?
- ;;
- -r | --repeat ) shift
- while [ "$1" != "" ]; do
- case $1 in
- -p | --path ) shift
- path=$1
- ;;
- -e | --extention ) shift
- extention=$1
- ;;
- -s | --save-path ) shift
- savePath=$1
- ;;
- -a | --amount ) shift
- (( bound=$1 ))
- ;;
- -t | --time ) shift
- (( time=$1 ))
- ;;
- *) echo "incorrect you have to use -e for extention and -s for save-path, also -n for name"
- exit 1
- ;;
- esac
- shift
- done
- set_repeatity
- exit $?
- ;;
- -d | --delete-jobs ) delete_jobs
- exit $?
- ;;
- -c | --check ) shift
- path=$1
- check_archive
- exit $?
- ;;
- -h | --help ) echo "$usage"
- exit 0
- ;;
- * ) echo "Arguments are incorrect"
- exit 1
- ;;
- esac
- shift
- done
- exit 0
- # set +x
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement