Help needed for anacrontab script

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
User avatar
PhilippeH
Level 2
Level 2
Posts: 88
Joined: Thu Jul 20, 2017 3:12 am
Location: Toulon (France)
Contact:

Help needed for anacrontab script

Post by PhilippeH »

Hello,
I use my Mint (20.2 Cinnamon) computer to work on local websites, and have an Apache webserver with PHP and MySQL running. I need to backup local databases and have come up with the following script in /etc/anacrontab file:

Code: Select all

1 15 purgetmp find /var/www/mysql_backup -type f -mtime +7 -delete > /dev/null
1 15 mysql_backup mkdir -p /var/www/mysql_backup ; mysqldump -u root -psesame --all-databases | /bin/gzip -9 > /var/www/mysql_backup/`date '+%F'`.databases.sql.gz ; rm -f /var/www/mysql_backup/`date '+%F' --date '1 week ago'`.databases.sql.gz
This works fine, however, it has two inconveniences that I hope you could help me correct:

1) the first line seems to me redundant with the second, as it seems to me that both delete the backups older than 7 days (?)
2) if the computer is turned off for several days, the older backups get deleted when they are more than 7 days old. Therefore, if the computer is off for more than 7 days, all backups get deleted and only one is kept after running the script.

I would prefer to always keep the last 7 backups, regardless of their age

Could one of you knowledgeable people give me indications on how to achieve my goal? Thank you very much :)
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
it-place
Level 3
Level 3
Posts: 188
Joined: Thu Jul 05, 2018 4:42 am

Re: Help needed for anacrontab script

Post by it-place »

Hi PhilippeH,

if you don't want the files to be deleted after 7 days but want to keep 7 versions I'll suggest to rotate the files instead. Move the files like this:

remove mysql_backup.7
mysql_backup.6 => mysql_backup.7
mysql_backup.5 => mysql_backup.6
[...]
mysql_backup.1 => mysql_backup.2
mysql_backup => mysql_backup.1
create mysql_backup

I've done this in an own backup script using a "while, do, done" loop in BASH. :)

Regards - Olli
User avatar
PhilippeH
Level 2
Level 2
Posts: 88
Joined: Thu Jul 20, 2017 3:12 am
Location: Toulon (France)
Contact:

Re: Help needed for anacrontab script

Post by PhilippeH »

Thanks for this sound advice, I will try.
Locked

Return to “Scripts & Bash”