How to write a linux database backup and upload a script through ftp
This article introduces the relevant knowledge of "how to write linux database backup and upload scripts through ftp". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
The code is as follows:
#! / bin/bash
# author: 505056357@qq.com
Echo "backup job start"
# set the website directory to be backed up, that is, the root directory of the website
File=/home/wwwroot/default
# set up a local backup directory for storing packaged files
Backpath=/home/wwwroot/bkdata/
Dbuser=root
# data password, remember to use single quotation marks to enclose the whole password, if there are single quotation marks in the password, use double quotation marks, hey
Dbpwd='123456'
Dbname=car
Domain=192.168.0.187
User=ftpuser
Userpwd='123456'
Bkdate=$ (date +% Y%m%d)
Sqlfile_sql=$ {dbname} _ ${bkdate} .sql
Sqlfile_gz=$ {dbname} _ ${bkdate} .tar.gz
Sqlfp=$ {backpath} ${bkdate} / $sqlfile_sql
If [- e $backpath$ {bkdate}]
Then
Echo $backpath is exists.
Else
Mkdir-p $backpath$ {bkdate}
Fi
# perform database backup and / apps/mysql/bin/mysqldump change to the path of your actual environment
/ apps/mysql/bin/mysqldump-u $dbuser-p$dbpwd $dbname > ${sqlfp}
# compress the database files after backup, or compress the database files directly by adding parameters during database backup
Tar zcf $backpath$ {bkdate} / $sqlfile_gz-C ${backpath} ${bkdate} ${sqlfile_sql}
# Delete files that are not compressed SQL
Rm-f ${sqlfp}
# format of generated file name: web_ website directory name _ 20130419.tar.gz
Fn=web_$ {file##*/} _ ${bkdate} .tar.gz
Tar zcf $backpath$ {bkdate} / $fn-C ${file%/*} ${file##*/}
# the following actions are used to automatically connect and upload local files to a remote server for FTP
/ usr/bin/lftp ${domain}