zl程序教程

您现在的位置是:首页 >  系统

当前栏目

关于Linux中数据备份方式的一些总结

Linux 方式 总结 关于 一些 数据备份
2023-06-13 09:15:27 时间

写在前面

  • 在传统的运维部署中,需求增量上线的时,会备份应用和应用数据,保证升级失败也可以回滚,同时,定期数据备份也是容灾的一种手段,如K8s中etcd的定期快照备份,当K8s集群和etcd集群因为不可控原因全部死掉之后,除非有etcd备份,否则只能重置集群环境了。
  • 在日常备份中,可以通过定时任务备份,手动备份,差异触发备份。
  • 博文内容包括:
    • 常见的日志备份数据库备份(mysql)脚本
    • 三种备份方式:物理备份逻辑备份远程差异备份的Demo

「 等长大就明白了。小时候总是被人这么说。但那是不折不扣的谎言。我对任何事都只能越来越不明白。——中岛敦《山月记》」


一、日志文件备份

「日志备份很简单,写一个shell脚本,通过脚本的方式进行,当然,如果需要,这个脚本可以配置到定时任务里。」

┌──[root@liruilongs.github.io]-[~]
└─$pwd
/root
┌──[root@liruilongs.github.io]-[~]
└─$mkdir bak_shell
┌──[root@liruilongs.github.io]-[~]
└─$cd bak_shell/
┌──[root@liruilongs.github.io]-[~/bak_shell]
└─$vim bak_log.sh
┌──[root@liruilongs.github.io]-[~/bak_shell]
└─$sh bak_log.sh
┌──[root@liruilongs.github.io]-[~/bak_shell]
└─$cat bak_log.sh
#!/bin/bash
###此脚本运用日期定义备份的文件名,方便与每天进行备份不重复
date=`date +"%Y%m%d%H%M%S"`
if [ ! -f /tmp/log-$date.tar.gz ];then
tar -zcPf /tmp/log-$date.tar.gz /var/log
fi
┌──[root@liruilongs.github.io]-[/tmp]
└─$cd /tmp/;ll -h | grep log-*
-rw-r--r-- 1 root root 4.4M 11月 15 10:51 log-20211115110510.tar.gz
┌──[root@liruilongs.github.io]-[/tmp]
└─$

二、数据库备份

关系数据库备份,这里物理机直接操作,用容器也是一样的。

1、逻辑备份(SQL备份)

环境准备

###安装mariadb数据库,重启服务
┌──[root@liruilongs.github.io]-[/tmp]
└─$yum -y install mariadb mariadb-server
┌──[root@liruilongs.github.io]-[/tmp]
└─$systemctl restart mariadb
####查看数据库服务的进程信息
┌──[root@liruilongs.github.io]-[/tmp]
└─$ss -ntulpa | grep mysql
tcp    LISTEN     0      50        *:3306                  *:*                   users:(("mysqld",pid=52010,fd=14))
┌──[root@liruilongs.github.io]-[/tmp]
└─$# 登录测试下
┌──[root@liruilongs.github.io]-[/var/lib/mysql]
└─$mysql -uroot
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.68-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases
    -> ;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

MariaDB [(none)]> use test
Database changed
MariaDB [test]> show tables;
Empty set (0.00 sec)

MariaDB [test]>

mysqldump可以对mysql数据库中的库进行备份,一般的数据库都会提供相应的备份工具,比如MongoDBmongodump

##mysqldump可以对数据库中的库进行备份
##格式:mysqldump -u"用户名" --password="" 数据库名 > 备份名.sql
┌──[root@liruilongs.github.io]-[/]
└─$mysqldump mysql > mysql.sql
┌──[root@liruilongs.github.io]-[/]
└─$ll | grep mysql*
-rw-r--r--    1 root root  514667 11月 15 15:52 mysql.sql

「脚本编写」

┌──[root@liruilongs.github.io]-[/]
└─$mkdir mysql;cd mysql
┌──[root@liruilongs.github.io]-[/mysql]
└─$vim mysqldump.sh
┌──[root@liruilongs.github.io]-[/mysql]
└─$sh mysqldump.sh
┌──[root@liruilongs.github.io]-[/tmp]
└─$cd /tmp/;ls -h |  grep *.sql
mysql-20211115160404.sql
┌──[root@liruilongs.github.io]-[/tmp]
└─$cat /mysql/mysqldump.sh
#!/bin/bash
###date 指定备份数据名;iuser 指定登录数据库的用户
###ipass 指定登录密码,默认为空;db 指定要备份的数据库
date=$(date +"%Y%m%d%H%M%S")
iuser=root
ipass=
db=mysql
###文件在/tmp 下不存在时才会进行备份
if [ ! -f /tmp/$db-$date.sql ];then
mysqldump -u$iuser --password="$ipass" $db > /tmp/$db-$date.sql
fi
┌──[root@liruilongs.github.io]-[/tmp]
└─$

2、物理备份(buttaidong)

「物理备份即直接备份相关文件,mysql默认的表数据相关文件在/var/lib/mysql中」

┌──[root@liruilongs.github.io]-[/tmp]
└─$cd /mysql
┌──[root@liruilongs.github.io]-[/mysql]
└─$ls
mysqldump.sh
┌──[root@liruilongs.github.io]-[/mysql]
└─$vim bak_mysql.sh
┌──[root@liruilongs.github.io]-[/mysql]
└─$sh bak_mysql.sh
tar: 从成员名中删除开头的“/”
tar: 从成员名中删除开头的“/”
tar: 从成员名中删除开头的“/”
。。。。。
┌──[root@liruilongs.github.io]-[/tmp]
└─$cd mysql/
┌──[root@liruilongs.github.io]-[/tmp/mysql]
└─$ls
columns_priv.frm-20211115160950.tar.gz      proc.frm-20211115160950.tar.gz
columns_priv.MYD-20211115160950.tar.gz      proc.MYD-20211115160950.tar.gz
。。。。。。
┌──[root@liruilongs.github.io]-[/mysql]
└─$cat bak_mysql.sh
#!/bin/bash
###对数据库中的mysql库下每一个表都进行打包备份;备份文件存放在/tmp/mysql目录下
date=$(date +"%Y%m%d%H%M%S")
db_dir="/var/lib/mysql"
db=mysql

[ ! -d /tmp/$db ] && mkdir /tmp/$db
for i in $(ls $db_dir/$db)
do
tar -zcf /tmp/$db/$i-$date.tar.gz $db_dir/$db/$i
done
┌──[root@liruilongs.github.io]-[/mysql]
└─$

「这个的tar报错加了个 P参数就不报了,但是没必要,可以正常打包」

3、差异备份 inotify+rsync

「所谓差异备份,即通过inotify 来监听文件变化,通rsync来增量同步数据。」

这里我们本机模拟一下,一般是备份到远程机器上的,备份要配置ssh免密 ssh-copy-id root@192.168.26.55

安装rsync同步软件

┌──[root@liruilongs.github.io]-[/var/www/html]
└─$yum -y install rsync

安装 inotifywait 监听软件

┌──[root@liruilongs.github.io]-[/mysql]
└─$yum -y install inotify-tools
┌──[root@liruilongs.github.io]-[/mysql]
└─$rpm -qal  inotify-tools
/usr/bin/inotifywait
/usr/bin/inotifywatch
/usr/lib64/libinotifytools.so.0
/usr/lib64/libinotifytools.so.0.4.1
/usr/share/doc/inotify-tools-3.14
/usr/share/doc/inotify-tools-3.14/AUTHORS
/usr/share/doc/inotify-tools-3.14/COPYING
/usr/share/doc/inotify-tools-3.14/ChangeLog
/usr/share/doc/inotify-tools-3.14/NEWS
/usr/share/doc/inotify-tools-3.14/README
/usr/share/man/man1/inotifywait.1.gz
/usr/share/man/man1/inotifywatch.1.gz

差异备份 inotify+rsync

「进行模拟差异备份」

┌──[root@liruilongs.github.io]-[~/rsync]
└─$sh isync.sh &
[1] 17575
┌──[root@liruilongs.github.io]-[~/rsync]
└─$mkdir /root/liruilong
┌──[root@liruilongs.github.io]-[~/rsync]
└─$cd /root/liruilong/
┌──[root@liruilongs.github.io]-[~/liruilong]
└─$ls
┌──[root@liruilongs.github.io]-[~/liruilong]
└─$

这里用一台机器的两个目录进行模拟,执行差异备份脚本,添加文件,文件会同步到新建的空目录

┌──[root@liruilongs.github.io]-[~/liruilong]
└─$cd /var/www/html/
┌──[root@liruilongs.github.io]-[/var/www/html]
└─$echo "123456" > liruilong.txt
┌──[root@liruilongs.github.io]-[/var/www/html]
└─$cat liruilong.txt
123456
┌──[root@liruilongs.github.io]-[/var/www/html]
└─$cd /root/liruilong/
┌──[root@liruilongs.github.io]-[~/liruilong]
└─$ls
liruilong.txt
┌──[root@liruilongs.github.io]-[~/liruilong]
└─$cat liruilong.txt
123456
┌──[root@liruilongs.github.io]-[~/liruilong]
└─$jobs
[1]+  运行中               sh isync.sh &(工作目录:~/rsync)
┌──[root@liruilongs.github.io]-[~/liruilong]
└─$

「备份脚本」isync.sh

┌──[root@liruilongs.github.io]-[~/rsync]
└─$cat isync.sh
#!/bin/bash
##from_dir 为要被同步的目录
from_dir="/var/www/html/"
##将$from_dir下的内容,同步到本机的/root/liruilong/目录下
rsync_cmd="rsync -az --delete $from_dir root@192.168.26.55:/root/liruilong"
##inotifywait监听 $from_dir 目录,目录下发生文件的变化时,执行同步操作,脚本后台运行
while inotifywait -rqq -e modify,move,create,delete,attrib $from_dir
do
$rsync_cmd
done
┌──[root@liruilongs.github.io]-[~/rsync]
└─$