Backup of MySQL database under Linux-putty

Source: Internet
Author: User
Tags config ftp php script sprintf ssh mysql database backup

Prerequisite: With SSH landing privileges

Work Step:

Download software putty, set SSH login option

After landing, enter your home page directory * (accessible via FTP)

Executes the mysqldump command to perform a database backup, while the Mysqlrestore command restores the database.

Format:

pXXXXXXX@kundenserver:~ > mysqldump -hdbXX.puretec.de -upXXXXXXX -p******** dbXXXXXXX > dbXXXXXXXX.sql

pXXXXXXX@kundenserver:~ > mysql -hdbXX.puretec.de -upXXXXXXX -p******** dbXXXXXXX < dbXXXXXXXX.sql

After the mysqldump is executed, a backup file is generated in the current directory (size and database size)

Download the backed up database package via FTP

Restore database work contrary to the above two parts

You can also write a PHP script to do the above:

Backing up files

include "../config.php";
MYSQL_CONNECT($dbhost, $dbuser, $dbpw) or die ( "<H3>无法访问数据库</H3>");
MYSQL_SELECT_DB($dbname) or die ( "<H3>数据库尚未建立</H3>");
$path = getenv("DOCUMENT_ROOT")."/DB_backup";
$result = MYSQL_QUERY("SHOW TABLES");
$numrow = MYSQL_NUM_ROWS($result);
for($i = 0;$i < $numrow;$i++) {
$table = MYSQL_RESULT($result,$i);
echo "$table ... ";
system(sprintf("mysqldump --opt -h $dbhost -u $dbuser -p$dbpw $dbname $table | gzip > %s/$table.sql.gz",$path));
echo "DONE\n\n";
}
MYSQL_CLOSE();

Recover files

include "../config.php";
system(sprintf(
 "gunzip -c %s/dump.sql.gz | mysql -h %s -u %s -p%s %s",
 getenv("DOCUMENT_ROOT"),
 $dbhost,
 $dbuser,
 $dbpw,
 $dbname
));
echo "+DONE";

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.