Below is a MySQL backup instance, which is tested and passed in Ubuntu. The task is a local backup. It can be used on the server side. #! /Bin/bashecho "backupmysqlV1.0" ### datestamp =$ (date + % Y-% m-% d) ### path ### startdir =/home/user/mysqlbackup ### bakfileprefix ### filep
Below is a MySQL backup instance, which is tested and passed in Ubuntu. The task is a local backup. It can be used on the server side.
#! /Bin/bash
Echo "backupmysql V1.0"
### Date stamp ###
Datestamp = $ (date + % Y-% m-% d)
### Path ###
Startdir =/home/user/mysqlbackup
### Bakfile prefix ###
Fileprefix = SQL
Echo "sqldump is starting .."
### Bakup command ####
Mysqldump-uuser-ppassword-l databasename> $ startdir/$ fileprefix $ datestamp. SQL
Echo "###################################### ##################################"
Echo "sqldump is done"
### Tar file ###
Cd $ startdir
Echo "the current directory is :"
Pwd
Tar zcvf $ startdir/$ fileprefix $ datestamp. tgz $ fileprefix $ datestamp. SQL
### Del the SQL file ###
Rm-rf $ startdir/$ fileprefix $ datestamp. SQL
Echo "###################################### #################################"
Echo "...... Done" if you remotely take the backup to a remote location, the following is a client case I wrote (test environment RedHat-as-5 ). Use sftp protocol. Reference:
#! /Bin/bash
Sftp root@www.domain.com <EOF
Cd/home/user/mysqlbackup
LCD/home/mysqlbackup/
-Get fileprefix $ (date + % Y-% m-% d). tgz
Quit
EOF note fileprefix $ (date + % Y-% m-% d). tgz corresponds to server backup,
Path: home/user/mysqlbackup
Add "-" before the get command to prevent the sftp execution process from being terminated when an error occurs.
Note: If the database server name is database and the backup data server name is backup, run the script on the backup server to obtain the backup file on the database server.
Two servers transmit files using sftp
However, in real shell, sftp logon requires an interactive password,
To use non-interactive sftp on the remote server, we can use the key method to verify the logon permission. This replaces the password verification method.
First, we need to generate a key in backup and upload its public key to the database server.
Because the key pair (Public Key and private key) must be used when sftp is used in shell scripts ),
Run the following command:
$ Ssh-keygen-d
Then distribute the Public Key
To use the key, you must distribute the public key to the remote backup server you want to log on,
1. copy the public key to the Home Directory of the remote user who wants to log on to the remote server. For example:
Cp id_dsa.pub to database:/home/user/. ssh/
If the directory/home/user/. ssh/does not exist, we need to create it manually.
The public key file is renamed authorized_keys.
Modify the access permission of a public key file
Chmod 644 authorized_keys