网站搜索

如何使用“Automysqlbackup”和“Autopostgresqlbackup”工具备份/恢复 MySQL/MariaDB 和 PostgreSQL


如果您是数据库管理员 (DBA) 或负责维护、备份和恢复数据库,您就知道您不能丢失数据。原因很简单:丢失数据不仅意味着重要信息的丢失,还可能会损害您的企业的财务。

因此,您必须始终确保:

1.您的数据库会定期备份,
2.这些备份存储在安全的地方,并且
3.您定期进行恢复演习。

最后一项活动不应被忽视,因为您不想在没有练习在这种情况下需要做什么的情况下遇到重大问题。

在本教程中,我们将向您介绍两个不错的实用程序,分别用于备份 MySQL/MariaDBPostgreSQL 数据库:automysqlbackupautopostgresqlbackup

由于后者基于前者,因此我们将重点解释 automysqlbackup 并突出显示与 autopgsqlbackup 的差异(如果有的话)。

强烈建议将备份存储在备份目录中安装的网络共享中,以便在发生系统范围崩溃时,您仍然可以得到保护。

阅读以下有关 MySQL 的有用指南:

安装 MySQL/MariaDB/PostgreSQL 数据库

1.本指南假设您必须运行MySQL/MariaDB/PostgreSQL实例,如果没有,请安装以下软件包:

基于 Fedora 的发行版:

yum update && yum install mariadb mariadb-server mariadb-libs postgresql postgresql-server postgresql-libs

Debian 及其衍生品:

aptitude update && aptitude install mariadb-client mariadb-server mariadb-common postgresql-client postgresql postgresql-common

2. 您有一个可以使用的测试 MySQL/MariaDB/PostgreSQL 数据库(建议您< strong style="color:red">不要在生产环境中使用 automysqlbackupautopostgresqlbackup,除非您熟悉这些工具。

否则,请创建两个示例数据库并在继续之前向其中填充数据。在本文中,我将使用以下数据库和表:

CREATE DATABASE mariadb_db;
CREATE TABLE tecmint_tbl (UserID INT AUTO_INCREMENT PRIMARY KEY, 
UserName VARCHAR(50), 
IsActive BOOL);

CREATE DATABASE postgresql_db;
CREATE TABLE tecmint_tbl (
UserID SERIAL PRIMARY KEY,
UserName VARCHAR(50),
IsActive BOOLEAN);

在 CentOS 7 和 Debian 8 中安装 automysqlbackup 和 autopgsqlbackup

3.Debian 8 中,这两个工具都可以在存储库中找到,因此安装它们就像运行一样简单:

aptitude install automysqlbackup autopostgresqlbackup

而在 CentOS 7 中,您需要下载安装脚本并运行它们。在下面的部分中,我们将专门关注在 CentOS 7 上安装、配置和测试这些工具,因为对于 Debian 8,它们几乎是开箱即用的,我们将在本文后面进行必要的澄清。

在 CentOS 7 中安装和配置 automysqlbackup

4. 让我们首先在 /opt 中创建一个工作目录来下载安装脚本并运行它:

mkdir /opt/automysqlbackup
cd /opt/automysqlbackup
wget http://ufpr.dl.sourceforge.net/project/automysqlbackup/AutoMySQLBackup/AutoMySQLBackup%20VER%203.0/automysqlbackup-v3.0_rc6.tar.gz
tar zxf automysqlbackup-v3.0_rc6.tar.gz
./install.sh

5. automysqlbackup 的配置文件位于 /etc/automysqlbackup 内,名称为 myserver.conf。让我们看一下最相关的配置指令:

Username to access the MySQL server
CONFIG_mysql_dump_username='root'
Password
CONFIG_mysql_dump_password='YourPasswordHere'
Host name (or IP address) of MySQL server
CONFIG_mysql_dump_host='localhost'
Backup directory
CONFIG_backup_dir='/var/backup/db/automysqlbackup'
List of databases for Daily/Weekly Backup e.g. ( 'DB1' 'DB2' 'DB3' ... )
set to (), i.e. empty, if you want to backup all databases
CONFIG_db_names=(AddYourDatabase Names Here)
List of databases for Monthly Backups.
set to (), i.e. empty, if you want to backup all databases
CONFIG_db_month_names=(AddYourDatabase Names Here)
Which day do you want monthly backups? (01 to 31)
If the chosen day is greater than the last day of the month, it will be done
on the last day of the month.
Set to 0 to disable monthly backups.
CONFIG_do_monthly="01"
Which day do you want weekly backups? (1 to 7 where 1 is Monday)
Set to 0 to disable weekly backups.
CONFIG_do_weekly="5"
Set rotation of daily backups. VALUE*24hours
If you want to keep only today's backups, you could choose 1, i.e. everything older than 24hours will be removed.
CONFIG_rotation_daily=6
Set rotation for weekly backups. VALUE*24hours. A value of 35 means 5 weeks.
CONFIG_rotation_weekly=35
Set rotation for monthly backups. VALUE*24hours. A value of 150 means 5 months.
CONFIG_rotation_monthly=150
Include CREATE DATABASE statement in backup?
CONFIG_mysql_dump_create_database='no'
Separate backup directory and file for each DB? (yes or no)
CONFIG_mysql_dump_use_separate_dirs='yes'
Choose Compression type. (gzip or bzip2)
CONFIG_mysql_dump_compression='gzip'
What would you like to be mailed to you?
- log   : send only log file
- files : send log file and sql files as attachments (see docs)
- stdout : will simply output the log to the screen if run manually.
- quiet : Only send logs if an error occurs to the MAILADDR.
CONFIG_mailcontent='quiet'
Email Address to send mail to? ([email )
CONFIG_mail_address='root'
Do you wish to encrypt your backups using openssl?
#CONFIG_encrypt='no'
Choose a password to encrypt the backups.
#CONFIG_encrypt_password='password0123'
Command to run before backups (uncomment to use)
#CONFIG_prebackup="/etc/mysql-backup-pre"
Command run after backups (uncomment to use)
#CONFIG_postbackup="/etc/mysql-backup-post"

根据您的需要配置automysqlbackup后,强烈建议您查看/etc/automysqlbackup/README中的README文件。

MySQL数据库备份

6. 准备好后,继续运行程序,并将配置文件作为参数传递:

automysqlbackup /etc/automysqlbackup/myserver.conf

快速检查 daily 目录将显示 automysqlbackup 已成功运行:

pwd
ls -lR daily

当然,您可以添加一个 crontab 条目,以便在一天中最适合您需求的时间运行 automysqlbackup(在下面的示例中,每天凌晨 1:30):

30 01 * * * /usr/local/bin/automysqlbackup /etc/automysqlbackup/myserver.conf

恢复 MySQL 备份

7. 现在让我们有意删除 mariadb_db 数据库:

让我们再次创建它并恢复备份。在 MariaDB 提示符中,输入:

CREATE DATABASE mariadb_db;
exit

然后找到:

cd /var/backup/db/automysqlbackup/daily/mariadb_db
ls

并恢复备份:

mysql -u root -p mariadb_db < daily_mariadb_db_2015-09-01_23h19m_Tuesday.sql
mysql -u root -p
MariaDB [(none)]> USE mariadb_db; 
MariaDB [(none)]> SELECT * FROM tecmint_tb1;

在 CentOS 7 中安装和配置 autopostgresqlbackup

8.为了使autopostgresqlCentOS 7中完美工作,我们需要首先安装一些依赖项:

yum install mutt sendmail

然后让我们像以前一样重复这个过程:

mkdir /opt/autopostgresqlbackup
cd /opt/autopostgresqlbackup
wget http://ufpr.dl.sourceforge.net/project/autopgsqlbackup/AutoPostgreSQLBackup/AutoPostgreSQLBackup-1.0/autopostgresqlbackup.sh.1.0
mv autopostgresqlbackup.sh.1.0 /opt/autopostgresqlbackup/autopostgresqlbackup.sh

让我们使脚本可执行并启动/启用服务:

chmod 755 autopostgresqlbackup.sh
systemctl start postgresql
systemctl enable postgresql

最后,我们将备份目录设置的值编辑为:

BACKUPDIR="/var/backup/db/autopostgresqlbackup"

完成automysqlbackup的配置文件后,配置这个工具就非常容易了(这部分任务就交给你了)。

9.CentOS 7 中,与 Debian 8 不同,autopostgresqlbackup 最好作为 postgres 运行 系统用户,因此为了做到这一点,您应该切换到该帐户或将 cron 作业添加到其 crontab 文件中:

crontab -u postgres -e
30 01 * * * /opt/autopostgresqlbackup/autopostgresqlbackup.sh

顺便说一句,需要创建备份目录,并且必须将其权限和组所有权递归设置为 0770postgres (同样,这在 Debian):

mkdir /var/backup/db/autopostgresqlbackup
chmod -R 0770 /var/backup/db/autopostgresqlbackup
chgrp -R postgres /var/backup/db/autopostgresqlbackup

结果:

cd /var/backup/db/autopostgresqlbackup
pwd
ls -lR daily

10. 现在您可以在需要时恢复文件(请记住在重新创建空数据库后以用户 postgres 身份执行此操作):

gunzip -c postgresql_db_2015-09-02.Wednesday.sql.gz | psql postgresql_db

Debian 8 中的注意事项

正如我们之前提到的,这些工具在 Debian 中的安装不仅更加简单,而且它们各自的配置也更加简单。您将在以下位置找到配置文件:

  1. 自动mysqlbackup:/etc/default/automysqlbackup
  2. Autopostgresqlbackup:/etc/default/autopostgresqlbackup

概括

在本文中,我们解释了如何安装和使用 automysqlbackupautopostgresqlbackup (学习如何使用第一个也将帮助您掌握第二个),这两个很棒的数据库备份这些工具可以使您作为 DBA 或系统管理员/工程师的任务变得更加轻松。

请注意,您可以通过设置电子邮件通知或通过电子邮件以附件形式发送备份文件来扩展此主题 - 不是严格要求的,但有时可能会派上用场。

最后一点,请记住配置文件的权限应设置为最小值(大多数情况下为0600)。我们期待听到您对本文的看法。请随时使用下面的表格给我们留言。