全备数据库
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
| zabbix |
+--------------------+
5 rows in set (0.00 sec)
MariaDB [(none)]> system mysqldump -u root -p --all-databases > /root/zabbix_bak.sql
Enter password:
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]>
MariaDB [(none)]> drop database zabbix;
Query OK, 140 rows affected (0.22 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
Database error
Error connecting to database: Unknown database 'zabbix'
MariaDB [(none)]> system mysql -u root -p zabbix< /root/zabbix_bak.sql
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
| zabbix |
+--------------------+
5 rows in set (0.00 sec)
MariaDB [(none)]>
增量备份数据库
[root@localhost ~]# vim /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
log-bin=mysql-bin
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
[root@localhost ~]# systemctl stop mariadb.service
[root@localhost ~]# systemctl start mariadb.service
[root@localhost ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.56-MariaDB MariaDB Server
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show variables like 'log_%'
-> ;
+---------------------------------+--------------------------------------------------------------------------------------------------------------+
| Variable_name | Value |
+---------------------------------+--------------------------------------------------------------------------------------------------------------+
| log_bin | ON |
| log_bin_trust_function_creators | OFF |
| log_error | /var/log/mariadb/mariadb.log |
| log_output | FILE |
| log_queries_not_using_indexes | OFF |
| log_slave_updates | OFF |
| log_slow_filter | admin,filesort,filesort_on_disk,full_join,full_scan,query_cache,query_cache_miss,tmp_table,tmp_table_on_disk |
| log_slow_queries | OFF |
| log_slow_rate_limit | 1 |
| log_slow_verbosity | |
| log_warnings | 1 |
+---------------------------------+--------------------------------------------------------------------------------------------------------------+
11 rows in set (0.01 sec)
MariaDB [(none)]> \q
MariaDB [(none)]> grant all privileges on *.* to root@localhost identified by '1';
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> flush privileges;Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> select user,host,password from mysql.user;
+--------+-----------------------+-------------------------------------------+
| user | host | password |
+--------+-----------------------+-------------------------------------------+
| root | localhost | *E6CC90B878B948C35E92B003C792C46C58C4AF40 |
| root | localhost.localdomain | |
| root | 127.0.0.1 | |
| root | ::1 | |
| | localhost | |
| | localhost.localdomain | |
| zabbix | localhost | *DEEF4D7D88CD046ECA02A80393B7780A63E7E789 |
+--------+-----------------------+-------------------------------------------+
7 rows in set (0.00 sec)
MariaDB [(none)]>
[root@localhost ~]# ls -ld /var/lib/mysql/mysql-bin.*
MariaDB [(none)]> show master logs;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000001 | 100374 |
| mysql-bin.000002 | 29940 |
| mysql-bin.000003 | 1194 |
| mysql-bin.000004 | 288 |
| mysql-bin.000005 | 3495 |
| mysql-bin.000006 | 288 |
| mysql-bin.000007 | 288 |
| mysql-bin.000008 | 64737 |
+------------------+-----------+
8 rows in set (0.01 sec)
MariaDB [(none)]> flush logs;
Query OK, 0 rows affected (0.01 sec)
//刷新后重新生成bin-log,数据会存放到新的bin-log
MariaDB [(none)]> show master logs;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000001 | 100374 |
| mysql-bin.000002 | 29940 |
| mysql-bin.000003 | 1194 |
| mysql-bin.000004 | 288 |
| mysql-bin.000005 | 3495 |
| mysql-bin.000006 | 288 |
| mysql-bin.000007 | 288 |
| mysql-bin.000008 | 64780 |
| mysql-bin.000009 | 245 |
+------------------+-----------+
9 rows in set (0.00 sec)
MariaDB [(none)]>
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| #mysql50#mysql-bin |
| test |
| zabbix |
+--------------------+
5 rows in set (0.00 sec)
MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000009 | 245 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
MariaDB [(none)]> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [test]> show tables;
+----------------+
| Tables_in_test |
+----------------+
| test1 |
+----------------+
1 row in set (0.00 sec)
MariaDB [test]> insert into test1 values(1);
Query OK, 1 row affected (0.00 sec)
MariaDB [test]> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000009 | 430 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
MariaDB [test]>
[root@localhost ~]# mysqlbinlog --no-defaults mysql-bin.000009
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!40019 SET @@session.max_insert_delayed_threads=0*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
mysqlbinlog: File 'mysql-bin.000009' not found (Errcode: 2)
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
[root@localhost ~]# mysqlbinlog --no-defaults /var/lib/mysql/mysql-bin.000009
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/;
/*!40019 SET @@session.max_insert_delayed_threads=0*/;
/*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
DELIMITER /*!*/;
# at 4
#190222 18:13:01 server id 1 end_log_pos 245 Start: binlog v 4, server v 5.5.56-MariaDB created 190222 18:13:01
# Warning: this binlog is either in use or was not closed properly.
BINLOG '
LctvXA8BAAAA8QAAAPUAAAABAAQANS41LjU2LU1hcmlhREIAbG9nAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAEzgNAAgAEgAEBAQEEgAA2QAEGggAAAAICAgCAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAATmoVA==
'/*!*/;
# at 245
#190222 18:16:52 server id 1 end_log_pos 313 Query thread_id=329 exec_time=0 error_code=0
SET TIMESTAMP=1550830612/*!*/;
SET @@session.pseudo_thread_id=329/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
SET @@session.sql_mode=0/*!*/;
SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;
/*!\C utf8 *//*!*/;
SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=8/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
BEGIN
/*!*/;
# at 313
#190222 18:16:52 server id 1 end_log_pos 403 Query thread_id=329 exec_time=0 error_code=0
use `test`/*!*/;
SET TIMESTAMP=1550830612/*!*/;
insert into test1 values(1)
/*!*/;
# at 403
#190222 18:16:52 server id 1 end_log_pos 430 Xid = 31310
COMMIT/*!*/;
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
/*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
MariaDB [test]> flush logs;
Query OK, 0 rows affected (0.01 sec)
MariaDB [test]> select * from test1;
+------+
| id |
+------+
| 2 |
| 1 |
+------+
2 rows in set (0.00 sec)
MariaDB [test]> delete from test1;
Query OK, 2 rows affected (0.01 sec)
MariaDB [test]> select * from test1;
Empty set (0.00 sec)
MariaDB [test]> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000010 | 420 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
MariaDB [test]>
//数据恢复
[root@localhost ~]# mysqlbinlog --no-defaults /var/lib/mysql/mysql-bin.000009 | mysql -uroot -p1 test
[root@localhost ~]#
MariaDB [test]> select * from test1;
+------+
| id |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
MariaDB [test]>
//linux 脚本备份mariadb
[root@localhost ~]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 337
Server version: 5.5.56-MariaDB MariaDB Server
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show variables like '%server_id%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 1 |
+---------------+-------+
1 row in set (0.00 sec)
MariaDB [(none)]>
//server-id=1
//每7天删除日志
[root@localhost ~]# cat /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
server-id=1
log-bin=mysql-bin
expire_logs_days=7
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
//清空日志
[root@localhost ~]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 338
Server version: 5.5.56-MariaDB MariaDB Server
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> reset master;
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> show master logs;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000001 | 245 |
+------------------+-----------+
1 row in set (0.00 sec)
MariaDB [(none)]>
//递归创建目录
[root@localhost ~]# mkdir -p /var/lib/mysql/backup/daily
[root@localhost ~]# cd /var/lib/mysql/
[root@localhost mysql]# ls
aria_log.00000001 backup ib_logfile0 mysql mysql-bin.000001 mysql.sock zabbix
aria_log_control ibdata1 ib_logfile1 mysql-bin mysql-bin.index test
[root@localhost mysql]# vim dayBak.sh
#!/bin/bash
# Program
# use cp to backup mysql data everyday!
# 2017-8-28 huangwei
# History
# Path
#BakDir是增量备份时复制mysql-bin.00000*的目标目录,提前手动创建这个目录
BakDir=/var/lib/mysql/backup/daily
BinDir=/var/lib/mysql
LogFile=/var/lib/mysql/backup/dayBakLog.log
BinFile=/var/lib/mysql/mysql-bin.index
mysqladmin -uroot -proot flush-logs
#这个是用于产生新的mysql-bin.00000*文件
Counter=`wc -l $BinFile |awk '{print $1}'`
NextNum=0
#这个for循环用于比对$Counter,$NextNum这两个值来确定文件是不是存在或最新的
for file in `cat $BinFile`
do
base=`basename $file`
#basename用于截取mysql-bin.00000*文件名,去掉./mysql-bin.000005前面的./
NextNum=`expr $NextNum + 1`
if [ $NextNum -eq $Counter ]
then
echo $base skip! >> $LogFile
else
dest=$BakDir/$base
if(test -e $dest)
#test -e用于检测目标文件是否存在,存在就写exist!到$LogFile去
then
echo $base exist! >> $LogFile
else
cp $BinDir/$base $BakDir
echo $base copying >> $LogFile
find . -type f -ctime +6 -exec rm {} \;
#删除6天后的日志文件
fi
fi
done
echo `date +"%Y年%m月%d日 %H:%M:%S"` $Next daily Bakup succ! >> $LogFile
chmod a+x /var/lib/mysql/dayBak.sh
[root@localhost mysql]# touch /var/lib/mysql/backup/fullBakLog.log
vi fullBak.sh
#!/bin/bash
# Program
# use mysqldump to Fully backup mysql data per week!
# History
# Path
BakDir=/var/lib/mysql/backup
LogFile=/var/lib/mysql/backup/fullBakLog.log
Date=`date +%Y%m%d`
Begin=`date +"%Y年%m月%d日 %H:%M:%S"`
cd $BakDir
DumpFile=$Date.sql
GZDumpFile=$Date.sql.tgz
mysqldump -uroot -p1 zabbix > $DumpFile
/bin/tar -zvcf $GZDumpFile $DumpFile
/bin/rm $DumpFile
#只保留过去四周的数据库内容
count=$(ls -l *.tgz |wc -l)
if [ $count -ge 5 ]
then
file=$(ls -l *.tgz |awk '{print $9}'|awk 'NR==1')
rm -f $file
fi
#只保留过去四周的数据库内容
Last=`date +"%Y年%m月%d日 %H:%M:%S"`
echo 开始:$Begin 结束:$Last $GZDumpFile succ >> $LogFile
cd $BakDir/daily
/bin/rm -f *
chmod a+x /var/lib/mysql/fullBak.sh
crontab -e
#Monday to Sunday 0:00 make Daily Log 每天0点执行增量备份
0 0 * * * /var/lib/mysql/dayBak.sh >/dev/null 2>&1
#Sunday 0:00 make Full Log 每周星期日0点执行全量备份
0 0 * * sun /var/lib/mysql/fullBak.sh >/dev/null 2>&1
crontab /etc/crontab
crontab -l
[root@localhost mysql]# systemctl start zabbix_server
[root@localhost mysql]# systemctl start httpd
[root@localhost mysql]# systemctl enable zabbix_server
zabbix_server.service is not a native service, redirecting to /sbin/chkconfig.
Executing /sbin/chkconfig zabbix_server on
[root@localhost mysql]# systemctl enable httpd
[root@localhost mysql]# systemctl start zabbix_agentd
[root@localhost mysql]# systemctl enable zabbix_agentd
zabbix_agentd.service is not a native service, redirecting to /sbin/chkconfig.
Executing /sbin/chkconfig zabbix_agentd on
//数据库全备还原
[root@localhost mysql]# tar zxvf /var/lib/mysql/backup/20190222.sql.tgz -C /root/
20190222.sql
MariaDB [(none)]> drop database zabbix;
Query OK, 140 rows affected (0.28 sec)
MariaDB [(none)]> create database zabbix;
Query OK, 1 row affected (0.01 sec)
MariaDB [zabbix]> system mysql -u root -p zabbix < /root/20190222.sql
Enter password:
//数据库增量备还原
MariaDB [test]> select * from test1;
+------+
| id |
+------+
| 4 |
+------+
1 row in set (0.00 sec)
MariaDB [test]> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000008 | 670943 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
MariaDB [test]> select * from test1;
+------+
| id |
+------+
| 4 |
+------+
1 row in set (0.00 sec)
//将数据放入mysql-bin.000009,刷新bin-log
854 mysqladmin -uroot -p1 flush-logs
MariaDB [test]> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000009 | 2239 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
MariaDB [test]> insert into test1 values(5);
Query OK, 1 row affected (0.00 sec)
//查询 855 mysqlbinlog --no-defaults /var/lib/mysql/mysql-bin.000009
//删除数据
MariaDB [test]> delete from test1 where id = 5;
Query OK, 1 row affected (0.00 sec)
MariaDB [test]> select * from test1;
+------+
| id |
+------+
| 4 |
+------+
1 row in set (0.00 sec)
//恢复数据
856 cp -r /var/lib/mysql/mysql-bin.000009 /var/lib/mysql/backup/daily/
857 cd /var/lib/mysql/backup/daily/
858 ls
859 mysqlbinlog --no-defaults ./mysql-bin.000009 | mysql -uroot -p1 test
MariaDB [test]> select * from test1;
+------+
| id |
+------+
| 4 |
| 5 |
+------+
2 rows in set (0.00 sec)
MariaDB [test]>