20260407系统间复制文档

系统间复制文档

一、windows与Linux

1. 使用 ftp 相关工具

提示:前提条件是Linux服务器开启了sshd 服务。

2. 使用 lrzsz

提示:前提条件是Linux服务器安装了lrzsz工具。

适合传送小文件,因为速度慢。

  • sz file,传输文件到windows中。

  • rz file,上传到Linux中,直接拖拽方式更方便。

二、Linux与Linux

全量传输:每次传输都是完整的传输,不支持比对传输。

bash 复制代码
# 系统主机名存储位置
[root@server1 ~ 11:33:49]# cat /etc/hostname 
server1.jiang.cloud

# 将文件传输到server2的/root目录
# server2要能够解析成ip
[root@server1 ~ 14:52:08]# scp /etc/hostname server2:/root
root@server2's password: 
hostname                                                   100%   20    20.7KB/s   00:00    
[root@server1 ~ 14:52:35]# ssh server2 cat /root/hostname
root@server2's password: 
server1.jiang.cloud

# 如果之前配置过秘钥登录,则不需要输入密码就可以同步

# 复制目录,需要-r选项
[root@server1 ~ 14:52:56]# scp -r /etc/yum server2:/tmp
root@server2's password: 
contentdir                                                 100%    7     3.6KB/s   00:00    
infra                                                      100%    6     2.5KB/s   00:00    
fastestmirror.conf                                         100%  279   394.1KB/s   00:00    
langpacks.conf                                             100%  385    47.8KB/s   00:00    
systemd.conf                                               100%    8     4.9KB/s   00:00    
version-groups.conf                                        100%  444   235.6KB/s   00:00    

# 也可以拉取文件
[root@server1 ~ 14:53:23]# scp server2:/etc/hostname /tmp
root@server2's password: 
hostname                                                   100%   20    15.8KB/s   00:00    
[root@server1 ~ 14:53:56]# cat /tmp/hostname 
server2.jiang.cloud
[root@server1 ~ 14:54:04]# 

1. rsync

全量和增量同步:每次传输的时候,比对目标位置是否有相同的文件。如果有相同的文件,不传输。

推荐创建免密登录!!!!!!!!!!

bash 复制代码
# 准备文件
[root@server1 ~ 15:00:51]# mkdir Picturs
[root@server1 ~ 15:11:10]# touch Picturs/snap-{1..6}.jpg

#同步目录
[root@server1 ~ 15:11:31]# rsync -av Picturs server2:/root
root@server2's password: 
sending incremental file list
Picturs/
Picturs/snap-1.jpg
Picturs/snap-2.jpg
Picturs/snap-3.jpg
Picturs/snap-4.jpg
Picturs/snap-5.jpg
Picturs/snap-6.jpg
# 选项 -a 归档模式
# 选项 -v 显示详细过程

sent 407 bytes  received 134 bytes  360.67 bytes/sec
total size is 0  speedup is 0.00

# 第二次同步:没有变化,所以不用同步
[root@server1 ~ 15:11:58]# rsync -av Picturs server2:/root
sending incremental file list

sent 166 bytes  received 17 bytes  122.00 bytes/sec
total size is 0  speedup is 0.00

# 更新文件时间戳
[root@server1 ~ 15:12:44]# touch  Picturs/snap-3.jpg

# 只同步变化的文件
[root@server1 ~ 15:12:59]# rsync -av Picturs server2:/root
sending incremental file list
Picturs/snap-3.jpg

sent 217 bytes  received 36 bytes  101.20 bytes/sec
total size is 0  speedup is 0.00

# 同步内容发生变化的文件
[root@server1 ~ 15:13:25]# echo hello world >> Picturs/snap-6.jpg 
[root@server1 ~ 15:13:48]# rsync -av Picturs server2:/root
sending incremental file list
Picturs/snap-6.jpg

sent 236 bytes  received 36 bytes  181.33 bytes/sec
total size is 12  speedup is 0.04

# rsync也支持拉取文件。
[root@server1 ~ 15:14:07]# rsync -av server2:/root/Picturs /tmp
root@server2's password: 
Permission denied, please try again.
root@server2's password: 
receiving incremental file list
Picturs/
Picturs/snap-1.jpg
Picturs/snap-2.jpg
Picturs/snap-3.jpg
Picturs/snap-4.jpg
Picturs/snap-5.jpg
Picturs/snap-6.jpg

sent 142 bytes  received 434 bytes  76.80 bytes/sec
total size is 12  speedup is 0.02
[root@server1 ~ 15:14:43]# 

三、周期性计划作业

定个闹钟:每天 7:00 起床。

计算机也要定时要完成自己的事情:

  • 每天巡检系统资源使用情况。

  • 每小时检查一次异常日志。

  • 每天夜里 0:00 备份数据。

    crond 服务,提供定制任务功能,定期触发执行相应命令。

实践:每分钟同步一次上一章节创建的Pictures目录到server2.

步骤1:确保crond服务启动

bash 复制代码
[root@server1 ~ 15:14:43]# systemctl is-active crond
active

步骤2:设置定时任务

bash 复制代码
# 查看当前定时任务
[root@server1 ~ 15:45:55]# crontab -l
no crontab for root

# 设置vim作为默认编辑器
[root@server1 ~ 15:46:07]# export EDITOR=vim

# 编辑定时任务
[root@server1 ~ 15:46:30]# crontab -e

# 弹出vim编辑文件
*  *  *  *  * rsync -av Pictures server2:/root

# 提前配置好密钥登陆
[root@server1 ~ 16:05:15]# ssh-keygen 
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? 
You have new mail in /var/spool/mail/root
[root@server1 ~ 16:06:17]# ssh-copy-id root@server2
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are promptto install the new keys
root@server2's password: 
Permission denied, please try again.
root@server2's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@server2'"
and check to make sure that only the key(s) you wanted were added.

# 验证密钥登陆
[root@server1 ~ 16:06:46]# ssh server2 hostname
server2.jiang.cloud

# 查看默认编辑文件 
[root@server1 ~ 15:48:56]# crontab -l
* * * * * rsync -av Pictures server2:/root

# 创建一个文件
[root@server1 ~ 16:07:09]# touch Pictures/snap-7.jpg
You have new mail in /var/spool/mail/root

# 验证-l: 查看执行日志
[root@server1 ~ 16:07:18]# tail -f /var/log/cron
Apr  7 16:01:01 server1 CROND[2133]: (root) CMD (rsync -av Pictures server2:/roo
Apr  7 16:01:01 server1 CROND[2134]: (root) CMD (run-parts /etc/cron.hourly)
Apr  7 16:01:01 server1 run-parts(/etc/cron.hourly)[2134]: starting 0anacron
Apr  7 16:01:01 server1 run-parts(/etc/cron.hourly)[2144]: finished 0anacron
Apr  7 16:02:01 server1 CROND[2150]: (root) CMD (rsync -av Pictures server2:/roo
Apr  7 16:03:02 server1 CROND[2162]: (root) CMD (rsync -av Pictures server2:/roo
Apr  7 16:04:01 server1 CROND[2168]: (root) CMD (rsync -av Pictures server2:/roo
Apr  7 16:05:01 server1 CROND[2176]: (root) CMD (rsync -av Pictures server2

# 验证-2:目标服务器查看
[root@server1 ~ 16:07:39]# ssh server2 ls Pictures/snap-7.jpg
Pictures/snap-7.jpg

# 默认位置
[root@server1 ~ 16:07:42]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,f
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed

步骤三: 下载mailx

bash 复制代码
[root@server1 ~ 15:59:00]# yum install -y mailx
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package mailx.x86_64 0:12.5-19.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package             Arch                 Version                   Repository  
================================================================================
Installing:
 mailx               x86_64               12.5-19.el7               base        

Transaction Summary
================================================================================
Install  1 Package

Total download size: 245 k
Installed size: 466 k
Downloading packages:
mailx-12.5-19.el7.x86_64.rpm                                          | 245 kB  
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : mailx-12.5-19.el7.x86_64                                         
  Verifying  : mailx-12.5-19.el7.x86_64                                         

Installed:
  mailx.x86_64 0:12.5-19.el7                                                    

Complete!


# 查找邮件
[root@server1 ~ 16:02:09]# mail
Heirloom Mail version 12.5 7/5/10.  Type ? for help.
"/var/spool/mail/root": 14 messages 14 new
>N  1 (Cron Daemon)         Tue Apr  7 15:49  29/1132  "Cron <root@server1> rsyn
 N  2 (Cron Daemon)         Tue Apr  7 15:50  29/1133  "Cron <root@server1> rsyn
 N  3 (Cron Daemon)         Tue Apr  7 15:51  29/1133  "Cron <root@server1> rsyn
 N  4 (Cron Daemon)         Tue Apr  7 15:52  29/1133  "Cron <root@server1> rsyn
 N  5 (Cron Daemon)         Tue Apr  7 15:53  29/1133  "Cron <root@server1> rsyn
 N  6 (Cron Daemon)         Tue Apr  7 15:54  29/1133  "Cron <root@server1> rsyn
 N  7 (Cron Daemon)         Tue Apr  7 15:55  29/1133  "Cron <root@server1> rsyn
 N  8 (Cron Daemon)         Tue Apr  7 15:56  29/1133  "Cron <root@server1> rsyn
 N  9 (Cron Daemon)         Tue Apr  7 15:57  29/1133  "Cron <root@server1> rsyn
 N 10 (Cron Daemon)         Tue Apr  7 15:58  29/1133  "Cron <root@server1> rsyn
 N 11 (Cron Daemon)         Tue Apr  7 15:59  29/1133  "Cron <root@server1> rsyn
 N 12 (Cron Daemon)         Tue Apr  7 16:00  29/1133  "Cron <root@server1> rsyn
 N 13 (Cron Daemon)         Tue Apr  7 16:01  29/1133  "Cron <root@server1> rsyn
 N 14 (Cron Daemon)         Tue Apr  7 16:02  29/1133  "Cron <root@server1> rsyn
& 31
Message  31:
Message 31:
From root@server1.jiang.cloud  Tue Apr  7 16:19:01 2026
Return-Path: <root@server1.jiang.cloud>
X-Original-To: root
Delivered-To: root@server1.jiang.cloud
From: "(Cron Daemon)" <root@server1.jiang.cloud>
To: root@server1.jiang.cloud
Subject: Cron <root@server1> rsync -av Pictures server2:/root
Content-Type: text/plain; charset=UTF-8
Auto-Submitted: auto-generated
Precedence: bulk
X-Cron-Env: <XDG_SESSION_ID=40>
X-Cron-Env: <XDG_RUNTIME_DIR=/run/user/0>
X-Cron-Env: <LANG=en_US.UTF-8>
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/root>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=root>
X-Cron-Env: <USER=root>
Date: Tue,  7 Apr 2026 16:19:01 +0800 (CST)
Status: R

sending incremental file list

sent 203 bytes  received 17 bytes  440.00 bytes/sec
total size is 12  speedup is 0.05

& q
Held 31 messages in /var/spool/mail/root
You have mail in /var/spool/mail/root

其他示例:

  • 每年2月2日上午9点执行年度备份脚本:

    0 9 2 2 * /usr/local/bin/yearly_backup

  • 7月每周五9:00-16:00,每5分钟发送包含「Chime」的邮件给任务所有者:

bash 复制代码
*/5 9-16 * Jul 5 echo "Chime"
  • 每个工作日(周一至周五)23:58执行每日报告脚本:
bash 复制代码
58 23 * * 1-5 /usr/local/bin/daily_report
  • 每个工作日9:00发送邮件给老板(% 表示换行,后续内容为邮件正文):
bash 复制代码
0 9 * * 1-5 mutt -s "Checking in" boss@example.com % Hi boss, just checking in.
相关推荐
23.2 小时前
【Linux】grep -F 及 双横线--的妙用
linux·命令模式
橙露3 小时前
Linux 驱动入门:字符设备驱动框架与编写流程
linux·运维·服务器
hong1616883 小时前
TypeScript类型断言
linux·javascript·typescript
南境十里·墨染春水4 小时前
Linux学习进展 进程管理命令 及文件压缩解压
linux·运维·笔记·学习
航Hang*4 小时前
第2章:进阶Linux系统——第4节:配置与管理NFS服务器
linux·运维·服务器·笔记·学习·vmware
橘子编程4 小时前
操作系统原理:从入门到精通全解析
java·linux·开发语言·windows·计算机网络·面试
亚空间仓鼠4 小时前
OpenEuler系统常用服务(三)
linux·运维·服务器·网络
信工 18024 小时前
rk3568-Linux应用程序和驱动程序接口
linux·驱动开发·rk3568
倒酒小生5 小时前
4月7日算法学习小结
linux·服务器·学习