Solve the problem of regular automatic file deletion by crontab+shell script under Centos7

Solve the problem of regular automatic file deletion by crontab+shell script under Centos7

Problem description:

Recently, there is a demand that the amount of data synchronized by rsync each time is large, but the database bak file needs to be retained

It is enough to keep it for 7 days, so you need to automatically clean up the bak files in the folder

Solution:

Use shell script to regularly delete tasks in folders

1. Create a shell file

[root@zabbix script]# vim backup_sql_clean.sh
#!/bin/sh
find /data1/backup/KDKDA\$AGKDPAYKT/XNAKSD/FXUIJ -mtime +10 -name "*.bak" -exec rm -rf {} \;

Parameter Description:

/data1/backup/KDKDA\$AGKDPAYKT/XNAKSD/FXUIJ #This is the file path

-mtime +10 #This is the number of days to be retained, 10 means 10 days

-name "*.bak" #This is the name of the file to be deleted. Adding a suffix here means deleting files of this type

Others are Linux commands

2. Set shell file permissions

[root@zabbix script]# chown 777 backup_sql_clean.sh

3. Set crontab periodic execution

The crontab command is used to set instructions to be executed periodically

Crontab related command description: https://www.jb51.net/article/151069.htm

[root@zabbix /]# crontab -e
0 0 * * 7 /data/script/backup_sql_clean.sh

4. Start the crond process

The concept of crond is inseparable from crontab. Crontab is a command commonly used in Unix and Unix-like operating systems, used to set instructions to be executed periodically.

This command reads instructions from the standard input device and stores them in the "crontab" file for later reading and execution. And crond is its daemon.

[root@zabbix /]# systemctl status crond.service #View crond status [root@zabbix /]# systemctl start crond.service #Start crond service [root@zabbix /]# systemctl restart crond.service #Restart crond service

Replenish;

Here is an introduction to scheduled file deletion in centOS7

1. Enter the Linux system

2. Create a file with the suffix sh in any directory, such as:

3. Edit and open the file, as shown in the figure:

4. Press the "i" key or the "insert" key on the keyboard to enter the editing mode.

enter:

#!/bin/sh
find /data/iqmkj/backup/mysql -mtime +7 -name "*.sql" -exec rm -rf {} \;

As shown in the figure:

illustrate:

"/data/iqmkj/backup/mysql": The directory where the files to be deleted are located.
“+7”: The number of days to retain files, that is, files older than a few days will be deleted.
“*.sql”: deletes files with the suffix .sql.
The others are fixed characters.

5: Save the edited file

Press the "esc" key on the keyboard, and then enter ":wq" in the current window. The file is saved successfully and automatically returns to the main interface.

6. Authorize the file

enter:

chown 777 backup_mysql.sh

If the authorization fails, directly select the file, right-click and set permissions to check all or set "777".

7: Create a timer using centos7's scheduled tasks

Enter "crontab -e" in the main interface to enter the scheduled task editing interface.

8. Set up scheduled tasks

Insert "0 4 * * * /data/iqmkj/backup/mysql/backup_mysql_clean.sh".
illustrate:
The file path is the file path created in the second step.

9. Save scheduled task configuration

Press the "esc" key on the keyboard, and then enter ":wq" in the current window. The file is saved successfully and automatically returns to the main interface.

10. Start the timer

Enter "/bin/systemctl start crond.service" on the main interface. Now all configurations are completed.

illustrate:

Start the scheduled task: /bin/systemctl start crond.service
Stop the scheduled task: /bin/systemctl stop crond.service
Restart the scheduled task: /bin/systemctl restart crond.service
Check the status of scheduled tasks: /bin/systemctl status crond.service

Summarize

The above is what I introduced to you about solving the problem of regular automatic file deletion by crontab+shell script under Centos7. I hope it will be helpful to you. If you have any questions, please leave me a message and I will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!

You may also be interested in:
  • How to build Jenkins+Maven+Git continuous integration environment on CentOS7
  • Install centos7 virtual machine on win10
  • How to set a fixed IP address in CentOS7 virtual machine
  • Centos7 installation of Nginx integrated Lua sample code
  • How to use yum to configure lnmp environment in CentOS7.6 system
  • MySQL 8.0.13 installation and configuration tutorial under CentOS7.3
  • Rsync+crontab regular synchronization backup under centos7
  • Compile and install php7 on centos7 to connect to apache in php-fpm mode
  • Detailed tutorial on installing mysql 8.0.13 (rpm) on Centos7
  • Three methods to modify the hostname of Centos7

<<:  Detailed installation and configuration of MySql on Mac

>>:  mysql5.7.18 decompressed version to start mysql service

Recommend

How to completely uninstall iis7 web and ftp services in win7

After I set up the PHP development environment on...

Detailed explanation of the group by statement in MySQL database group query

1: Statement order of grouping function 1 SELECT ...

WeChat applet component development: Visual movie seat selection function

Table of contents 1. Introduction 1. Component da...

How to configure wordpress with nginx

Before, I had built WordPress myself, but at that...

Detailed explanation of built-in methods of javascript array

Table of contents 1. Array.at() 2. Array.copyWith...

Linux IO multiplexing epoll network programming

Preface This chapter uses basic Linux functions a...

Graphical tutorial on installing JDK1.8 under CentOS7.4

Linux installation JDK1.8 steps 1. Check whether ...

Detailed explanation of the steps to build a Vue project with Vue-cli

First you need to install Vue-cli: npm install -g...

Details about the like operator in MySQL

1. Introduction When filtering unknown or partial...

Building a selenium distributed environment based on docker

1. Download the image docker pull selenium/hub do...

How does MySQL achieve master-slave synchronization?

Master-slave synchronization, also called master-...