Last active
May 8, 2020 14:41
-
-
Save malkinfedor/4b20c2549cf6b9605d7778c5e755e612 to your computer and use it in GitHub Desktop.
Rotation backups script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| set -o errexit | |
| set -o pipefail | |
| set -o nounset | |
| # How to use this script | |
| # ./backup_rotate.sh "path_to_backups" ["path_to_log_file"] | |
| # | |
| # Examples: | |
| # ./backup_rotate.sh /dev/backend /var/log/backups/logs.txt | |
| # ./backup_rotate.sh /dev/fronetnd | |
| ftp_path=$1 | |
| log_path=${2:-"/var/log/backups/output.log"} | |
| ftp_host='1.2.3.4' | |
| ftp_user='username' | |
| ftp_passwd='password' | |
| getFilesListFromFtp() { | |
| local file_list='' | |
| output=$(ftp -i -p -nv $ftp_host <<-EOF | |
| user $ftp_user $ftp_passwd | |
| nlist $ftp_path | |
| EOF | |
| ) | |
| for line in $output | |
| do | |
| if [[ $line == *"/dev/"* ]]; then # check that string is path | |
| file_name="$(cut -d'/' -f4 <<<"$line")" | |
| file_list=$file_list$'\n'$file_name | |
| fi | |
| done | |
| echo "$file_list" | |
| } | |
| delFtpFile(){ | |
| ftp -i -p -nv $ftp_host<<-EOF | |
| user $ftp_user $ftp_passwd | |
| delete $ftp_path/$1 | |
| EOF | |
| } | |
| isDeleteFile () { # The function to choose whether to delete a file or not depends on the conditions | |
| dateMinus7Days=$(date --date "-7 days" +%s) | |
| dateMinus1Month=$(date --date "-1 month" +%s) | |
| dayOfMonth=$(date -d@$1 +%d) # from 01 to 31 | |
| date_file_epoch_format=$1 | |
| archive_name=$2 | |
| date_file_standard_format=$(date -d @$date_file_epoch_format "+%Y%m%d") | |
| echo "Start choosing delete archive or not..." >> $log_path | |
| if [ $1 -le $dateMinus7Days ]; # check that file is older than 7 days | |
| then | |
| echo "Archive with name $2 is older 7 days" >> $log_path | |
| if [ $dayOfMonth != "01" -a $dayOfMonth != "07" -a $dayOfMonth != "14" -a $dayOfMonth != "21" -a $dayOfMonth != "28" ] # check that file is not a weekly archive | |
| then | |
| echo "File with date $(date -d @$1 "+%Y%m%d") is NOT a weekly or monthly archive and going to be deleted" >> $log_path | |
| local deleteFile=1 | |
| else | |
| if [ $1 -lt $dateMinus1Month -a $dayOfMonth != "01" ] # check that a weekly archive is not older than 1 month and is not a monthly one | |
| then | |
| echo "Archive with date $(date -d @$1 "+%Y%m%d") is weekly archive and older than 1 month and going to be deleted" >> $log_path | |
| local deleteFile=1 | |
| else | |
| echo "Archive with date $(date -d @$1 "+%Y%m%d") is weekly archive and NOT older than 1 month, or it is monthly one, so keep it" >> $log_path | |
| local deleteFile=0 | |
| fi | |
| fi | |
| else | |
| echo "Archive with date $(date -d @$1 "+%Y%m%d") is not older than 7 days and isn't going to be deleted" >> $log_path | |
| logging " " | |
| local deleteFile=0 | |
| fi | |
| echo "$deleteFile" | |
| } | |
| getDateFromFilename() { | |
| if [[ $1 =~ ([0-9]{8,8}) ]]; then | |
| strresult=${BASH_REMATCH[1]} | |
| echo "$strresult" | |
| else | |
| echo "unable to parse string $strname" | |
| fi | |
| } | |
| logging(){ | |
| local message=$1 | |
| local log_path=${log_path:-} | |
| echo $1 >> $log_path || true | |
| } | |
| ### Main code ### | |
| archiveList=$(getFilesListFromFtp) | |
| logging " " | |
| logging "Current time is $(date +'%m/%d/%Y') $(date +%R)" | |
| logging " " | |
| echo " " | |
| echo "Start to rotate backups" | |
| for archive in $archiveList | |
| do | |
| logging " " | |
| logging "Start process archive with name $archive" | |
| date="$(getDateFromFilename $archive)" | |
| dateUnixEpoch="$(date -d "$date" +%s)" | |
| isDeleteFile=$(isDeleteFile $dateUnixEpoch $archive) | |
| if [[ $isDeleteFile = '1' ]] | |
| then | |
| logging "File with name $archive is deleting..." | |
| result=$(delFtpFile $archive) | |
| if [ $? -ne 0 ] | |
| then | |
| logging "$result" | |
| else | |
| logging "$result" | |
| logging "File with name $archive was deleted" | |
| logging " " | |
| fi | |
| fi | |
| done | |
| logging " " | |
| echo "Rotate was ended. Log file is $log_path" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment