微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

20200717记录: rsync同步应用,

 

 

二、RSYNC同步数据   •命令用法 –rsync [选项...] 源目录 目标目录 •同步与复制的差异 –复制:完全拷贝源到目标 –同步:增量拷贝,只传输变化过的数据     •rsync操作选项 –-n:测试同步过程,不做实际修改 –--delete:删除目标文件夹内多余的文档 –-a:归档模式,相当于-rlptgoD –-v:显示详细操作信息 –-z:传输过程中启用压缩/解压   虚拟机A:同步数据练习 [root@svr7 ~]# mkdir /mydir [root@svr7 ~]# mkdir /todir [root@svr7 ~]# cp /etc/passwd /etc/group /etc/fstab /mydir [root@svr7 ~]# ls /mydir/ [root@svr7 ~]# rsync -av /mydir /todir #同步目录本身 [root@svr7 ~]# ls /todir   [root@svr7 ~]# rsync -av /mydir/  /todir #同步目录下内容 [root@svr7 ~]# ls /todir [root@svr7 ~]# touch /todir/a.txt [root@svr7 ~]# ls /mydir/ [root@svr7 ~]# ls /todir/ [root@svr7 ~]# rsync -av /mydir/ /todir/   [root@svr7 ~]# touch /mydir/1.txt [root@svr7 ~]# mkdir /mydir/dc [root@svr7 ~]# ls /mydir/ [root@svr7 ~]# ls /todir/ [root@svr7 ~]# rsync -av /mydir/ /todir/   [root@svr7 ~]# rsync -av --delete /mydir/ /todir/   #删除目标多余的数据 [root@svr7 ~]# ls /mydir/ [root@svr7 ~]# ls /todir/   [root@svr7 ~]# touch /todir/b.txt [root@svr7 ~]# rsync -av --delete /mydir/ /todir/ [root@svr7 ~]# ls /mydir/ [root@svr7 ~]# ls /todir/     [root@svr7 ~]# touch /mydir/2.txt [root@svr7 ~]# rsync -av --delete /mydir/ /todir/ [root@svr7 ~]# ls /mydir/ [root@svr7 ~]# ls /todir/     三、远程同步数据rsync+ssh结合 •与远程的 SSH目录保持同步 –下行:rsync [...] user@host:远程目录 本地目录 –上行:rsync [...] 本地目录 user@host:远程目录   虚拟机A: [root@svr7 ~]# rsync -av --delete /mydir/ [email protected]:/opt/ [email protected]'s password:   虚拟机B: [root@pc207 ~]# ls /opt/ 1.txt dc fstab group passwd     四、实时同步 1.建立公私钥,实现ssh无密码的验证 虚拟机A:产生公私钥 [root@svr7 ~]# ssh-keygen #一路回车,生成公钥与私钥 [root@svr7 ~]# ls /root/.ssh/ #生成的公钥与私钥 id_rsa(私钥) id_rsa.pub(公钥) kNown_hosts(记录曾经远程管理过的机器) [root@svr7 ~]# ssh-copy-id [email protected]   #将公钥传递给虚拟机B [root@svr7 ~]# rsync -av --delete /mydir/ [email protected]:/opt/   虚拟机B [root@pc207 ~]# ls /root/.ssh/ authorized_keys(别的机器传递过来的公钥)   2.监控目录内容变化的软件 •Linux内核的 inotify 机制 –提供事件响应式的文件系统通知机制 –安装 inotify-tools 控制工具可调用此机制实现监控 •源码编译安装inotify-tools工具   步骤一:将tools.tar.gz,传递到虚拟机A [root@svr7 ~]# ls /root anaconda-ks.cfg ks.cfg 公共 视频 文档 音乐 initial-setup-ks.cfg tools.tar.gz 模板 图片 下载 桌面 步骤二:进行tar解包 [root@svr7 ~]# tar -xf /root/tools.tar.gz -C / [root@svr7 ~]# ls /tools [root@svr7 ~]# tar -xf /tools/inotify-tools-3.13.tar.gz -C /opt/ [root@svr7 ~]# ls /opt/ 步骤三:进行编译安装 [root@svr7 ~]# yum -y install gcc make [root@svr7 ~]# cd /opt/inotify-tools-3.13/ [root@svr7 inotify-tools-3.13]# ./configure --prefix=/opt/nsd [root@svr7 inotify-tools-3.13]# make [root@svr7 inotify-tools-3.13]# make install [root@svr7 inotify-tools-3.13]# ls /opt/nsd bin include lib share [root@svr7 inotify-tools-3.13]# ls /opt/nsd/bin/   如果没有指定安装位置,程序生成的路径:/usr/local/bin/inotifywait   3.编写Shell脚本(了解内容•基本用法 –inotifywait [选项] 目标文件 •常用命令选项 –-m,持续监控(捕获一个事件后不退出 –-r,递归监控、包括子目录及文件 –-q,减少屏幕输出信息 –-e,指定监视的 modify、move、create、delete、attrib 等事件类别   •循环:解决重复性的问题 格式: while 条件 do 循环执行的操作 done [root@svr7 /]# vim /etc/rsync.txt while /opt/nsd/bin/inotifywait -rqq /mydir/    #-rqq屏幕不输出信息 do rsync -a --delete /mydir/ [email protected]:/opt/ done   [root@svr7 /]# chmod a+x /etc/rsync.txt #赋予执行权限 [root@svr7 /]# ls -l /etc/rsync.txt [root@svr7 /]# /etc/rsync.txt & #放入后台运行 [root@svr7 /]# jobs -l #显示后台运行的进程并且加上PID信息         五、数据库服务基础(了解内容)   数据库:存放数据的仓库 数据库系统 在数据库系统中,有很多的数据库,每一个数据库中会有很多的表格  

      软件素材: 百度云盘:下载 users.sql文件 链接:https://pan.baidu.com/s/1hmepKHxMG_vPnBwWlwAlTA 提取码:k6xs

  •部署mariadb数据库服务器 [root@svr7 /]# yum -y install mariadb-server [root@svr7 /]# systemctl restart mariadb   •进入数据库系统 1.每一个命令都要以 ";"作为结尾 2.大多数命令不支持"Tab" 3.Linux系统的指令不能使用 [root@svr7 /]# MysqL MariaDB [(none)]> show databases ; #显示当前数据库系统中所有的数据库 MariaDB [(none)]> create database nsd; #创建数据库nsd MariaDB [(none)]> show databases; MariaDB [(none)]> drop database nsd; #删除数据库nsd; MariaDB [(none)]> show databases; MariaDB [(none)]> create database nsd2003; #创建数据库nsd2003 MariaDB [(none)]> show databases; MariaDB [(none)]> exit #退出数据库系统 Bye [root@svr7 /]# MysqL MariaDB [(none)]> show databases ; MariaDB [MysqL]> use MysqL; #进入MysqL库 Database changed MariaDB [MysqL]> show tables; #查看当前库下有哪些表格 MariaDB [MysqL]> show databases; MariaDB [MysqL]> use nsd2003; #进入nsd2003库 Database changed MariaDB [nsd2003]> show tables; #查看当前库下有哪些表格 MariaDB [nsd2003]> exit [root@svr7 /]#   •设置数据库系统管理员密码 数据库系统管理员用户名为root,对于数据库系统有最高权限,用户信息存放在数据库系统中的MysqL库中user表 Linux系统管理员用户名为root,对于Linux系统有最高权限,用户信息存放在/etc/passwd文件   –Linux系统命令行: MysqLadmin [-u用户名] [-p[旧密码]] password '新密码' [root@svr7 /]# MysqLadmin -u root password '123' [root@svr7 /]# MysqL -u root -p123   [root@svr7 /]# MysqLadmin -u root -p123 password '456' #已知旧密码123,修改密码为456   •恢复数据到数据库系统中 1.真机将users.sql文件传递到虚拟机A的/root目录下 [root@svr7 /]# ls /root/ anaconda-ks.cfg ks.cfg users.sql 模板 图片 下载 桌面 initial-setup-ks.cfg tools.tar.gz 公共 视频 文档 音乐 2.创建数据库nsd2003 [root@svr7 /]# MysqL -u root -p123 MariaDB [(none)]> create database nsd2003; MariaDB [(none)]> exit 3.将数据恢复到数据库nsd2003 [root@svr7 /]# MysqL -u root -p123 nsd2003 < /root/users.sql [root@svr7 /]# MysqL -u root -p123 MariaDB [(none)]> use nsd2003; MariaDB [nsd2003]> show tables; +-------------------+ | Tables_in_nsd2003 | +-------------------+ | base | | location | +-------------------+   •如果数据库系统密码忘记(慎用,千万不要让庞丽静老师知道) [root@svr7 /]# rm -rf /var/lib/MysqL #删除数据库所有的数据 [root@svr7 /]# yum -y reinstall mariadb-server #重新安装数据库 [root@svr7 /]# systemctl restart mariadb [root@svr7 /]# MysqL #测试是否恢复到最初 MariaDB [(none)]> exit   [root@svr7 /]# MysqLadmin -u root password '123' #设置数据库系统密码 [root@svr7 /]# MysqL -u root -p123 [root@svr7 /]# MysqL -u root -p123 MariaDB [(none)]> create database nsd2003; MariaDB [(none)]> exit [root@svr7 /]# MysqL -u root -p123 nsd2003 < /root/users.sql [root@svr7 /]# MysqL -u root -p123 MariaDB [(none)]> use nsd2003; MariaDB [nsd2003]> show tables;   •表格的操作:增(insert) 删(delete) 改(update) 查(select) 表格: 表字段 表记录

 

 

查询命令格式: select 表字段1,表字段2....... from 数据库名.表名字; [root@svr7 /]# MysqL -u root -p123 MariaDB [(none)]> use nsd2003; #进入到nsd2003库 MariaDB [nsd2003]> show tables; #查看当前库有哪些表格 MariaDB [nsd2003]> select * from base; #查看base表所有表字段与表记录 MariaDB [nsd2003]> select * from location; #查看location表所有表字段与表记录 MariaDB [nsd2003]> select id,name from base; #查看base表id与name表字段所有的表记录 MariaDB [nsd2003]> use MysqL; #进入到MysqL库 MariaDB [MysqL]> select * from nsd2003.base; #查看nsd2003库中base表的所有表字段与表记录 MariaDB [(none)]> exit #退出   有条件的查询命令格式: select 表字段1,表字段2....... from 数据库名.表名字 where 表字段='值'; [root@svr7 /]# MysqL -u root -p123 MariaDB [(none)]> use nsd2003; #进入到nsd2003库 MariaDB [nsd2003]> show tables; #查看当前库有哪些表格 MariaDB [nsd2003]> select * from base where id='1' ; MariaDB [nsd2003]> select * from base where id='1' and name='tom'; #两个条件都满足 MariaDB [nsd2003]> select * from base where id='3' or name='tom'; #两个条件满足其中一个 MariaDB [nsd2003]> select * from base where password='123'; #查询密码为123   删(delete):删除表中的记录 格式:delete from 表名字 where 表字段='值'; [root@svr7 /]# MysqL -u root -p123 MariaDB [(none)]> use nsd2003; #进入到nsd2003库 MariaDB [nsd2003]> show tables; #查看当前库有哪些表格 MariaDB [nsd2003]> select * from base where id='5'; MariaDB [nsd2003]> delete from base where id='5'; #删除id值为5的表记录 MariaDB [nsd2003]> select * from base ; 增(insert):增加表中的记录 格式:insert 表名字 values ('值','值','值'........); MariaDB [nsd2003]> insert base values ('5','nb','456'); MariaDB [nsd2003]> select * from base;     •数据库的授权 –GRANT 权限列表 ON 数据库名.表名 TO 用户名@客户机地址 IDENTIFIED BY '密码'; [root@svr7 /]# MysqL -u root -p123 MariaDB [(none)]> grant select on nsd2003.* to lisi@localhost identified by '123'; 当lisi通过本地localhost进行登录,输入密码为123,将会获得nsd2003数据库所有表的查询权限 MariaDB [(none)]> exit Bye [root@svr7 /]# MysqL -u lisi -p123 MariaDB [(none)]> show databases; MariaDB [(none)]> use nsd2003; #进入到nsd2003库 MariaDB [nsd2003]> show tables; #查看当前库有哪些表格 MariaDB [nsd2003]> select * from base ;   ################################################################################# 1. cobbler网络装机平台(录制视频)了解内容 2. 邮件服务器与链路聚合(5月1放假归来的周四练习) 3. 回顾以往所学

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐