[Linux] How to remove unnecessary backup files periodically

[Linux] How to remove unnecessary backup files periodically

Goal

  • Remove unnecessary MariaDB backup files which were modified more 6 days ago


Intro


Practice

1. Create old files

  • I have to create files which have a similar name pattern and were modified more 6 days ago
  • You can use __touch__ command
touch -t [[CC]YY]MMDDhhmm[.ss]


$ touch -t 201907150700 backup_mariadb_agriculture_19.07.15.0700.sql
$ touch -t 201907220700 backup_mariadb_agriculture_19.07.22.0700.sql


screenshoot001


2. Remove old files

  • You can find only the files that you want and execute a specific command to them by __find__ command
$ find [DIRECTORY] -name [FILE_NAME] -mtime +[DAYS] -exec rm -f {} \;


$ find ~/ -name 'backup_mariadb_agriculture_*.sql' -mtime +6 -exec rm -f {} \;


screenshoot002


3. Register in Cron

3.1. Edit the cron table

$ crontab -e


3.2. Register the schedule

  • Cron pattern
[MINUTE] [HOUR] [DAY] [MONTH] [DAY OF WEEK] [COMMAND]


  • I tried to execute backup script at 7 O’clock every day
0 7 * * * find ~/ -name 'backup_mariadb_agriculture_*.sql' -mtime +6 -exec rm -f {} \;



References

댓글남기기

-->