M
M
Michael2020-05-01 23:32:07
System administration
Michael, 2020-05-01 23:32:07

Can you help to correct the backup script to the desired condition?

All health!
I found a script for MYSQL database backup on the Internet , with the permission of the author, I post the code here:

spoiler
#!/bin/bash
PATH=/etc:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
 
destination="/backup/mysql"
userDB="backup"
passwordDB="backup"
fdate=`date +%Y-%m-%d`
 
find $destination -type d \( -name "*-1[^5]" -o -name "*-[023]?" \) -ctime +30 -exec rm -R {} \; 2>&1
find $destination -type d -name "*-*" -ctime +180 -exec rm -R {} \; 2>&1
mkdir $destination/$fdate 2>&1
 
for dbname in `echo show databases | mysql -u$userDB -p$passwordDB | grep -v Database`; do
    case $dbname in
        information_schema)
            continue ;;
        mysql)
            continue ;;
        performance_schema)
            continue ;;
        test)
            continue ;;
        *) mysqldump --databases --skip-comments -u$userDB -p$passwordDB $dbname | gzip > $destination/$fdate/$dbname.sql.gz ;;
    esac
done;


The question after cutting it with a file to fit your needs is as follows:
The link to the author's script says that the script makes a backup of the database (I have a crown every night at 3 am Moscow time) - but it confuses me that it does not work exactly as the author wrote or I don’t understand the algorithm correctly.
What is actually required
- when calling to make a copy of the database
- check that in the nth folder (as in the settings) there will be 5 folders in the format 2020-15-04 and inside the archives with the existing database
- in the specified folder with archive folders there should be copies of the last 5 days, the rest is deleted.

In fact, the script will work until you're blue in the face and spank such a number of folders with archives as long as there is space on the VPS
Please help me where to dig.

PS I contacted the author and received recommendations to correct the lines
Здравствуйте.
Эти строки отвечают за удаление старых версий

find $destination -type d \( -name "*-1[^5]" -o -name "*-[023]?" \) -ctime +30 -exec rm -R {} \; 2>&1
find $destination -type d -name "*-*" -ctime +180 -exec rm -R {} \; 2>&1

Если Вам нужно хранить 5 копий, то замените 30 на 5. Будут оставаться только копии за 15 число, как архивные. Чтобы и их удалять, замените во второй строчке 180 на 30 или 60.


But in fact the script works as it did - ie. spanks archives without deleting everything that is older than 5 days

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vreitech, 2020-05-02
@fzfx

but in general you should -ctimecorrect it. -ctime +30is over 30 days old...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question