Linux automatic Backup and upload of remote FTP for MySQL scheduled Tasks
1 , create the directory where the backup files are saved:/home/mysql_data
Cd/home
mkdir Mysql_data
2. Create backup script file:/home/mysql_data/mysql_databak.sh
Cd/home
CD Mysql_data
Touch mysql_databak.sh
Vim mysql_databak.sh
#!/bin/sh
Dump=/usr/bin/mysqldump#mysqldump backup File Execution path
out_dir=/home/mysql_data# Backup Storage Path
linux_user=root# System User Name
db_name=pw85# the name of the database to be backed up
db_user=root# Database Account Note: Non-root users to use the backup parameters--skip-lock-tables, or may be error
db_pass=123456# Database Password
days=7#days=7 represents the deletion of a backup 7 days ago, that is, only the last 7 days of backup
Cd$out_dir # go to backup storage directory
date= ' Date +%y_%m_%d ' #获取当前系统时间
out_sql= "$DATE. SQL" # the file name of the backup database
tar_sql= "mysqldata_bak_$date.tar.gz" # The final saved database backup file name
$DUMP-u$db_user-p$db_pass $DB _name--default-character-set=utf8--opt-q-r--skip-lock-tables> $OUT _sql # Backup
tar-czf $TAR _sql./$OUT _sql # compress to. tar.gz format
Rm$out_sql # Delete backup files in. sql Format
chown$linux_user: $LINUX _user $OUT _dir/$TAR _sql # change the owner of a backup database file
find$out_dir-name "mysqldata_bak*"-type f-mtime + $DAYS-exec rm {} \;# Delete the backup file 7 days ago (note: {} \; There are spaces in the middle)
deldate= ' date-d -7day +%y_%m_%d ' #删除ftp服务器空间7天前的备份
ftp-n<<!
open192.168.1.1 # Open the FTP server. 21 for FTP Port
useradmin 123456 # user name, password
binary# Set binary transfer
Cdmysqlbak # Enter the FTP directory (note: This directory must be real)
Lcd/home/mysql_data # List Local Directories
Prompt
mputmysqldata_bak_$date.tar.gz mysqldata_bak_$date.tar.gz # uploading files in a directory
mdeletemysqldata_bak_$deldate.tar.gz mysqldata_bak_$deldate.tar.gz # Delete the FTP space 7 days ago Backup
close# Close
bye! # Exit
3 , modify the file properties, and make it executable
chmod +x/home/mysql_data/mysql_databak.sh
4, modify the/etc/crontab #添加计划任务
Vi/etc/crontab # Add the following
1 * * * root/home/mysql_data/mysql_databak.sh #表示每天凌晨1点30执行备份
5. Restart the Crond to make the settings effective
/etc/rc.d/init.d/crond restart
Chkconfig Crond on #设为开机启动
service Crond Start # Start
This article from the "7385764" blog, reproduced please contact the author!
Linux scheduled task implementation MySQL automatic backup and upload remote FTP