Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The mall system backs up the MySQL database regularly in the linux system and deletes the backup files from seven days ago.

2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Database is an important part of mall software, and timing tasks can periodically automatically execute the necessary business logic of the program. Take the daily store settlement business logic of DSMall open source mall system as an example:

! / bin/bash

Name:bakmysql.sh

This is a ShellScript For Auto DB Backup and Delete old Backup

Backup address

Backupdir=/home/mysqlbackup

Backup file suffix time

Time= `date +% Y%m%d%H%M%S database name db_name=testmysql username db_user=rootmysql password db_pass=123456mysqldump-u $db_user-p$db_pass $db_name | gzip > $backupdir/$db_name$time.sql.gz delete the backup file find $backupdir-name$ db_name "* .sql.gz"-type f-mmin + 1-exec rm-rf {}\ > / dev/null 2 > & 1 Save exit instructions: time= date +% Y%m%d%H in the code can also be written as time= "$(date +"% Y%m%d$H ")" where the symbol is the symbol above the TAB key, not the 'symbol on the left side of ENTER, and there should be a space after date.

Db_name: database name

Db_user: database user name

Db_pass: user password

Type f means to find a normal type of file, and f means an ordinary file.

Mtime + 7 looks for files by when they were changed, with + 7 indicating that the file was changed 7 days ago, and-mmin + 7 indicating that the file was changed 7 minutes ago.

Exec rm {}; means to execute a shell command, with the exec option followed by the command or script to be executed, followed by a pair of {}, a space and a, and finally a semicolon.

/ dev/null 2 > & 1 redirects standard errors to standard output and throws them under / DEV/NULL. In popular terms, all standard output and standard errors are thrown into the trash; one of them & means that the command is executed in the background.

Timing execution

The bak_config file code is as follows

? every day exec

/ home/bak_sh/bak_day.sh

Every week exec

0 / home/bak_sh/bak_week.sh

Every month exec

/ home/bak_sh/bak_month.sh

First use the query status command to query the crond status, if it is in the stop state, it must be started first; if it is already in the start state, ignore it.

The operation commands are as follows:

/ sbin/service crond start start

/ sbin/service crond restart restart

/ sbin/service crond stop stop

/ sbin/service crond status query status

Check to see if the service is already running with the

Ps-ax | grep cron

View scheduled tasks

Crontab-l / / lists all current scheduled tasks

Crontab-r / / Delete all task scheduling work

Add scheduled task

Crontab / home/bak_sh/bak_config

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report