Back up MySQL database Shell scripts and PHP scripts in Linux

Source: Internet
Author: User

The loss of the database causes a lot of losses, so I wrote a script for backing up the Mysql database in Linux. It was initially in the php version, later, I found that the PHP configuration on the server restricted the calling of system functions, so I wrote another shell version. It seems that shell is really obscure, a bit like the classical Chinese in programming language. In short, it can be used. do not consider anything else.

SHELL version:

#! /Bin/bash
Db_user = "root" # database username
Db_pwd = "123456" # Database Password
Target_dir = "/home/jony/database_bakup/" # Backup Directory
Dbs = "product_db forum_db blog_db sns_db" # databases to be backed up, separated by Spaces
# Here is the cyclic backup
For db in $ dbs; do
Mysqldump-u $ db_user-p $ db_pwd $ db | gzip >$ {target_dir }$ {db }_$ (date + % F) ". SQL .gz"
Done

I don't want to talk about shell syntax anymore. I have tried it many times and I'm not used to it.

If there are no restrictions on the server, www.bkjia.com is not as comfortable as other languages, such as python and php.

PHP version:

<? Php
$ Db_user = "root ";
$ Db_pwd = "123456 ";
$ Date = date ("Ymd ");
$ Target_dir = "/home/jony/database_bakup /";
$ Db_array = array ("mysql", "phpmyadmin ");

Foreach ($ db_array as $ db_name ){
$ Command = "mysqldump-u ". $ db_user. "-p ". $ db_pwd. "". $ db_name. "| gzip> ". $ target_dir. $ db_name. "_". $ date. ". SQL .gz ";
System ($ command );
}
?>

Record it. Maybe you can use it too.

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.