当前位置: 首页 > news >正文

Python 运维脚本

1、备份文件

import os
import shutil# 定义配置文件目录和备份目录的路径
config_dir = "/root/python/to/config/files/"
backup_dir = "/root/python/to/backup/"# 遍历配置文件目录中的所有文件
for filename in os.listdir(config_dir):# 如果文件名以 ".conf" 结尾,则执行备份操作if filename.endswith('.conf'):# 构建完整的文件路径file_path = os.path.join(config_dir, filename)# 构建备份文件路径backup_path = os.path.join(backup_dir, filename)# 将文件复制到备份目录shutil.copy(file_path, backup_path)# 打印备份完成的消息print(f"Backup of {filename} completed")

2、安装pip

  • 根据不同的版本,安装不同的pip
wget https://bootstrap.pypa.io/pip/3.6/get-pip.py
python3 get-pip.py

在这里插入图片描述

3、将备份文件传送到远程主机上进行备份

import os
import shutil
import paramiko# 定义配置
local_config_dir = "/root/python/to/config/files/"
remote_backup_dir = "/root/python/to/backup/"
remote_host = "192.168.1.101"
remote_username = "root"  # 根据实际情况修改用户名
remote_password = "your_password"  # 根据实际情况修改密码# 检查本地配置目录是否存在
if not os.path.exists(local_config_dir):print(f"Local directory {local_config_dir} does not exist.")exit(1)# 创建一个 SSH 客户端对象
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())try:# 连接远程服务器print(f"Connecting to {remote_host}...")ssh.connect(remote_host, username=remote_username, password=remote_password)# 创建一个 SFTP 客户端对象sftp = ssh.open_sftp()# 检查远程目录是否存在,如果不存在则创建print(f"Checking remote directory {remote_backup_dir}...")try:sftp.stat(remote_backup_dir)print(f"Directory {remote_backup_dir} already exists.")except IOError:print(f"Directory {remote_backup_dir} does not exist. Creating...")sftp.mkdir(remote_backup_dir)# 遍历本地目录中的所有文件for filename in os.listdir(local_config_dir):file_path = os.path.join(local_config_dir, filename)backup_path = os.path.join(remote_backup_dir, filename)# 检查是否为文件(忽略目录)if os.path.isfile(file_path):print(f"Transferring {file_path} to {backup_path}...")sftp.put(file_path, backup_path)print(f"Transfer of {filename} completed.")finally:# 关闭 SFTP 和 SSH 连接print("Closing SSH connection...")ssh.close()

文章转载自:

http://TWVi3WDu.nkLLb.cn
http://piyh7iHJ.nkLLb.cn
http://ldWziwGH.nkLLb.cn
http://yXbkt6ou.nkLLb.cn
http://94EO7hA2.nkLLb.cn
http://xJ6R3r5p.nkLLb.cn
http://boOKmRSR.nkLLb.cn
http://IZwwVx7S.nkLLb.cn
http://gv9Xo3kf.nkLLb.cn
http://3OOuvYXn.nkLLb.cn
http://0PfpOUqO.nkLLb.cn
http://LybrZ8JI.nkLLb.cn
http://VIdSXhXj.nkLLb.cn
http://MFBDfmU2.nkLLb.cn
http://mCUvIbsn.nkLLb.cn
http://mmgmWkYt.nkLLb.cn
http://de2YX75u.nkLLb.cn
http://rmQAc4EB.nkLLb.cn
http://7j6iwRSo.nkLLb.cn
http://wvSnO4Hi.nkLLb.cn
http://9qp27QoZ.nkLLb.cn
http://VDucaurC.nkLLb.cn
http://Pl9RGX6f.nkLLb.cn
http://w1a6nDNO.nkLLb.cn
http://68bCFww7.nkLLb.cn
http://dwKpGsqM.nkLLb.cn
http://Ahqn91Q9.nkLLb.cn
http://MYFWiMpO.nkLLb.cn
http://27h3YwCm.nkLLb.cn
http://8XRiQ90x.nkLLb.cn
http://www.dtcms.com/a/176985.html

相关文章:

  • seata 1.5.2 升级到2.1.0版本
  • 力扣HOT100之链表:146. LRU 缓存
  • Inference-Time Scaling for Generalist Reward Modeling
  • (四)Java逻辑运算符和位运算符全面解析
  • 通配符 DNS 记录:应用场景与相关风险
  • SEO关键词与长尾词精准布局策略
  • 【Bootstrap V4系列】学习入门教程之 组件-折叠(Collapse)
  • mysql修改root密码
  • C++20新特新——02特性的补充
  • 性能比拼: Redis Streams vs Pub/Sub
  • 解决使用lettuce连接Redis超时的问题(tcpUserTimeout 参数失效问题)
  • YOLOv1:开创实时目标检测新纪元
  • Wireshark抓账号密码
  • 普通笔记本与军用加固笔记本电脑的区别,探索防水、防爆、防摔的真·移动工作站!
  • 在线PDF阅读方案:jQuery + PDF.js
  • 内网和外网怎么互通?外网访问内网的几种简单方式
  • 解决HomeAssistant 无法安装 samba share问题
  • 【数据库原理及安全实验】实验六 角色访问控制
  • 《C++ Templates》:有关const、引用、指针的一些函数模板实参推导的例子
  • C#——NET Core 中实现汉字转拼音
  • Redis 8.0 正式版发布,新特性很强!
  • 品质领航家装时代,亚新丽以匠心雕琢每一寸美好
  • 5.6-DAE实现
  • AI大模型分类以及Prompt优化技巧
  • 迁移学习:如何加速模型训练和提高性能
  • Vue3 自定义指令的原理,以及应用
  • Make:独立创造者手册——从0到1的商业自由之路
  • GPAW安装流程——Ubuntu 系统(Python 3.8.10)
  • 宝塔面板,删除项目后还能通过域名进行访问
  • 【上位机——MFC】对话框