Ubuntu Linux regularly backs up the database and uploads it to the Aliyun OSS

Source: Internet
Author: User
Tags aliyun
There may be a variety of problems in the production environment, and it is important to regularly back up the database in order to prevent data loss and disaster tolerance. This article uses crontab to run the shell script, which backs up MySQL to the local and uploads to Aliyun OSS object storage in the shell script. First look at the shell script (Shell script processing database backup, database backup synchronization upload cloud, seven days ago Database backup delete):
#!/bin/bash
#dbBackUp. SH//This shell script name
now=$ (date + "%y%m%d_%h%m%s") #创建当前时间字符串, note: Do not have spaces
before and after equals sign sevendaysago= ' Date +%f | Sed ' s/-//g '
sevendaysago= ' expr $sevenDaysAgo-7 '
rm./backup/price_$sevendaysago* #移除七天之前的单库备份
rm./ backup/all_$sevendaysago*  #移除七天之前的整库备份

#将price库备份并压缩到backup目录下, file name is price_+ time
mysqldump-uuser- Ppassword Price | gzip >/home/ubuntu/learn/mysql/backup/price_$now.sql.gz; 
#将MySQL中所有数据备份并压缩到backup目录下, file name is all_+ time
mysqldump-uuser-ppassword--all-databases | gzip >/home/ubuntu/ learn/mysql/backup/all_$now.sql.gz

#执行上传操作 to sync backup files to the cloud, stored in the Aliyun object storage OSS
node_env= $Now node/home/ubuntu/ Learn/mysql/upload.js
database backup files uploaded to Oss and need NPM to install Ali-oss and co libraries
/* Upload.js

/var co = require (' co ');
var OSS = require (' Ali-oss ');
var client = new OSS ({
  region: ' oss-cn-beijing ',//Upload region node, specific reference Aliyun provide list
  Accesskeyid: ' Accesskeyid ',
  Accesskeysecret: ' Accesskeysecret ';
};


Co (function* () {
    client.usebucket (' Data-backup-by-xiaozhu ');//bucket name to upload
    = var result = yield Client.put (' Price_ ' +process.env.node_env+ '. sql.gz ', '/home/ubuntu/learn/mysql/backup/price_ ' +process.env.node_env+ '. sql.gz ' );
    var result2 = yield client.put (' All_ ' +process.env.node_env+ '. sql.gz ', '/home/ubuntu/learn/mysql/backup/all_ ' + process.env.node_env+ '. sql.gz ');
catch (function (err) {
    console.log (err);
})
Sometimes the database backup file will be very large, the object storage space is likely to be stuffed, we need to clean regularly, Aliyun OSS allows users to bucket set life cycle rules to automatically eliminate outdated files, save storage space. Users can set more than one rule at a time
* Lifecycle.js
/* Set declaration cycle rules, automatically recycle database backup beyond seven days *
/var co = require (' co ');
var OSS = require (' Ali-oss ');
var client = new OSS ({
  region: ' oss-cn-beijing ',
  Accesskeyid: ' Accesskeyid ',
  accesskeysecret: ' Accesskeysecret '
});
Co (function* () {
  var result = yield client.putbucketlifecycle (' Data-backup-by-xiaozhu ', ' region ', [
    { 
      ID: ' Rule1 ',/   * rule ID, used to identify a rule, can not repeat * *
      status: ' Enabled ',   /* is effective
      /prefix: ' Price_ ',    * The affected file prefix, which is used only for files that match the prefix/
      days:7/   * Specify the expiration of N days from the last modification of the file * *
    },{
      ID: ' Rule2 ',
      Status: ' Enabled ' ,
      prefix: ' All_ ',
      days:7
    }
  ]);
  Console.log (result);
}). catch (function (err) {
  console.log (err);
});
The shell script completes the database backup and the backup cleanup operation, also needs to carry on the regular execution shell script The operation, we may use the crontab. Execute the CRONTAB-E command and add the following content. if the first execution of the crontab will allow you to choose which editor to use for editing, we choose the default nano.
1 * * */bin/sh/home/ubuntu/learn/mysql/dbbackup.sh
#每天1点30分 Execute dbBackUp Script
Press CTRL + X to exit after the entry is complete, and then enter Y when prompted, and then return to save. (shortcut key at the bottom of the command line window, ^ represents ctrl,m for Alt) we can also edit/etc/crontab directly to perform timed tasks. The difference is: CRONTAB-E is a user's cycle planning task;/etc/crontab is the periodic task of the system

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.