Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increase number of stored copies #8

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions keep_n_backups_for_templates.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/bin/bash
KonstantinKondratenko marked this conversation as resolved.
Show resolved Hide resolved

BACKUP_DIR="../backups"

if [ ! -d "$BACKUP_DIR" ]; then
mkdir "$BACKUP_DIR"
echo "Folder $BACKUP_DIR created"
fi

if [ "$#" -lt 2 ]; then
echo "Usage: $0 N folder1 [folder2 ... folderN]"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Поясните, как интерпретируются аргументы folderN

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

пока реализовано так:
передаем N - число хранимых копий
а потом как минимум 1 папку, но можно и больше папок, которые копируем, то есть для 1 курса пми по проге будет выгляжеть так:
./keep_n_backups_for_templates.sh 5 pr-2024-4381 pr-2024-4382 pr-2024-4383 pr-2024-4384 pr-2024-4385 pr-2024-4388

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

да, у меня не очень удачное описание, тут N количество и folderN никак не свзяаны в них N

exit 1
fi

N=$1
shift
FOLDERS=("$@")


copy_folder() {
local folder=$1
local timestamp=$(date +"%Y_%m_%d_%H_%M")
local new_folder_name="${folder}_${timestamp}"
KonstantinKondratenko marked this conversation as resolved.
Show resolved Hide resolved
cp -r -f "$folder" "$BACKUP_DIR/$new_folder_name"
echo "Folder $folder copied into $BACKUP_DIR/$new_folder_name"
}

for folder in "${FOLDERS[@]}"; do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

По задумке folder1 [folder2 ... folderN] это подстроки от начала имен папок.
Например, у нас могут быть папки вида:
pr-2023-1, pr-2024-5, cs-2025-6

Мы передаем в качестве аргументов:
pr-2023- и cs-2025-

соответственно, должны быть обработаны только каталоги, которые начинаются с одной из этих подстрок.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

хм, понял, пока работает так, что нужно прописать полные название копируемых папок (pr-2023-3381 pr-2023-3382 и тд)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

добавил prefix
теперь запускать нужно так:
./new.sh 5 pr-2024

Вот как будет выглядеть вывод:

Start copy pr-2024-4388 into ../backups/pr-2024-4388_2024_11_20_16_53
Folder pr-2024-4388 copied into ../backups/pr-2024-4388_2024_11_20_16_53
Start copy pr-2024-4382 into ../backups/pr-2024-4382_2024_11_20_16_53
Folder pr-2024-4382 copied into ../backups/pr-2024-4382_2024_11_20_16_53
Start copy pr-2024-4381 into ../backups/pr-2024-4381_2024_11_20_16_53
Folder pr-2024-4381 copied into ../backups/pr-2024-4381_2024_11_20_16_53
Start copy pr-2024-4383 into ../backups/pr-2024-4383_2024_11_20_16_53
Folder pr-2024-4383 copied into ../backups/pr-2024-4383_2024_11_20_16_53
Start copy pr-2024-4384 into ../backups/pr-2024-4384_2024_11_20_16_53
Folder pr-2024-4384 copied into ../backups/pr-2024-4384_2024_11_20_16_53
Start copy pr-2024-4385 into ../backups/pr-2024-4385_2024_11_20_16_53
Folder pr-2024-4385 copied into ../backups/pr-2024-4385_2024_11_20_16_53
Started deleting ../backups/pr-2024-4388_2024_11_20_16_44
Deleted old copy ../backups/pr-2024-4388_2024_11_20_16_44
Started deleting ../backups/pr-2024-4382_2024_11_20_16_44
Deleted old copy ../backups/pr-2024-4382_2024_11_20_16_44
Started deleting ../backups/pr-2024-4381_2024_11_20_16_44
Deleted old copy ../backups/pr-2024-4381_2024_11_20_16_44
Started deleting ../backups/pr-2024-4383_2024_11_20_16_44
Deleted old copy ../backups/pr-2024-4383_2024_11_20_16_44
Started deleting ../backups/pr-2024-4384_2024_11_20_16_44
Deleted old copy ../backups/pr-2024-4384_2024_11_20_16_44
Started deleting ../backups/pr-2024-4385_2024_11_20_16_44
Deleted old copy ../backups/pr-2024-4385_2024_11_20_16_44

if [ -d "$folder" ]; then
copy_folder "$folder"
else
echo "Folder $folder doesn't exist."
fi
done

clean_old_copies() {
local folder=$1
local pattern="${folder}_*"
local copies=("$BACKUP_DIR"/$pattern)
# echo ${copies[@]} # дебаг вывод чтобы посмотреть на то, как исщутся копии

if [ ${#copies[@]} -gt $N ]; then
local num_to_delete=$(( ${#copies[@]} - N ))
for ((i=0; i<num_to_delete; i++)); do
KonstantinKondratenko marked this conversation as resolved.
Show resolved Hide resolved
rm -rf "${copies[i]}"
echo "Deleted old copy ${copies[i]}"
done
fi
}

for folder in "${FOLDERS[@]}"; do
clean_old_copies "$folder"
done