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

Shell script sharing that automatically backs up MySQL to FTP and periodically cleans up expired backups

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "Shell script sharing that automatically backs up MySQL to FTP and regularly cleans up expired backups". In daily operation, it is believed that many people have doubts about the sharing of Shell scripts that automatically back up MySQL to FTP and regularly clean up expired backups. The editor consulted all kinds of materials and sorted out simple and useful operation methods. I hope it will be helpful to answer the question of "Shell script sharing that automatically backs up MySQL to FTP and regularly cleans up expired backups"! Next, please follow the editor to study!

The code is as follows:

Date +% Y-%m-%d

So, what if we want to get the date of a month ago? You can get it using the following command:

The code is as follows:

Date-d last-month +% Y-%m-%d

How's it going? It's easy. When actually backing up, I made some adjustments because the previous script used the form of "m-d-Y", that is, "month-day-year". But in the following Shell script, the file name is generated according to "year-month-day".

Therefore, given the entire backup Shell script, friends who need only need to fill in the part with Chinese instructions, and they can use it on their own Linux server. How to use it: copy it to a directory, then give it execution permission, and then set it to run at a specified time every day in cron. (for this part, please find the relevant instructions by yourself. I will not repeat them here.)

The Shell script for backing up MySQL with automatic cleanup to FTP is as follows:

The code is as follows:

#! / bin/bash

MyUSER= "db_username" # database user name, root account or user name with global permissions is recommended

MyPASS= "db_password" # Database password

MyHOST= "localhost" # database server

# Linux bin paths, change this if it can't be autodetected via which command

MYSQL= "$(which mysql)"

MYSQLDUMP= "$(which mysqldump)"

CHOWN= "$(which chown)"

CHMOD= "$(which chmod)"

GZIP= "$(which gzip)"

# path to database backup

DEST= "/ home/backup"

# Main directory where backup will be stored

MBD= "$DEST/mysql"

# Get hostname

HOST= "$(hostname)"

# Get data in dd-mm-yyyy format

NOW= "$(date +% d-%m-%Y)"

OLD= "$(date-d last-month +% d-%m-%Y)"

# File to store current backup file

FILE= ""

# Store list of databases

DBS= ""

# DO NOT BACKUP these databases

IGGY= "test"

Rm-rf $MBD

[!-d $MBD] & & mkdir-p $MBD | |:

# Only root can access it!

$CHOWN 0.0-R $DEST

$CHMOD 0600$ DEST

# Get all database list first

DBS= "$($MYSQL-u $MyUSER-h $MyHOST-p$MyPASS-Bse 'show databases')"

Cat > ftpcc.sh > ftpcc.sh

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

Development

Wechat

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

12
Report