Simple remote backup solution for Linux server databases (MySQL and ORACLE) bitsCN.com
Simple remote backup solution for Linux server databases (MySQL and ORACLE)
Simple remote backup solution for MySQL databases on Linux servers
First, let's briefly describe:
1. execute the script to export the database.
Mysqldump-h127.0.0.1-P3306-uroot-ppassword dbname>/bak/db/backup. SQL
2. use scp to transmit exported database files to other servers. generally, remote backup and disaster tolerance are performed.
Scp-P 195/bak/db/backup. SQL root@xxx.xxx.xxx.xxx:/bak
195 indicates the ssh port number. if it is 22 by default,-P 22 can be omitted.
-----------------------------------------------------------------
Now you need to write an sh script and set it to a scheduled task.
The sh script content is as follows:
[Java]
#! /Bin/sh
Filename = 'Date' + % Y % m % d-% H % M % s''
Filename = "$ {filename} bak. SQL"
Mysqldump-h127.0.0.1-P3306-uroot-ppassword dbname>/bak/db/$ {filename}
Scp-P 195/bak/db/$ {filename} root@xxx.xxx.xxx.xxx:/bak
Echo "success"
Do not forget to use chmod + x backup. sh to grant executable permissions to sh FILES.
If we store the sh file in the opt directory, we need to configure the scheduled task.
Edit File: vi/etc/crontab is set to run at half past four every day
30 04 *** root/bin/sh/opt/db/backup-database.sh
Run the command cat/var/log/cron to view the execution log of the scheduled task.
After the above processing, the server will automatically back up the database and upload it to the remote server. However, there is still a problem to be solved, that is, the scp command does not need a password.
For specific configuration methods, see http: // OS/201212/175290 .html.
Let's talk about the differences between oracle and mysql.
Generally, oracle backup can be performed only when su-oracle is switched to the oracle User. The following describes how to perform the operation directly under the root user.
In particular, some operations require the root user (for example, the password-less scp operation, here I am just an example), and some operations require the oracle User (exp export user ).
Key Command: su-oracle-c "/opt/expdatabase. sh"
This means that the root user does not need to switch to the oracle user, but directly uses the oracle User role to execute the expdatabase. sh file.
We need to call the expdatabase. sh file in the sh file under root.
The shell script is as follows:
1. master sh script executed by root
[Java]
#! /Bin/sh
Filename = 'Date' + % Y % m % d-% H % M % s''
Filename = "$ {filename} bak. dmp"
Su-oracle-c "/u01/oradata/bak/expdatabase. sh $ {filename }"
Scp-P 195/u01/oradata/bak/$ {filename} root@124.207.35.132:/bak/202.102.41.35
Echo "Execution completed"
2. sh script expdatabase. sh to be executed using the oracle User role
[Java]
#! /Bin/sh
# Go to oracle home
Cd/u01/oracle
# Loading oracle User Environment variables
../. Bash_profile
Exp user/password @ clmt file =/u01/oradata/bak/$1 full = y
BitsCN.com