程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

Python3 同步本地数据至samba server

发布于2019-12-07 17:07     阅读(2563)     评论(1)     点赞(0)     收藏(4)


写在前面

前一段时间把淘汰下来的手机root之后装了linux deploy(一个运行在手机中的Linux环境),在上面部署了一个简易的网盘和jupyter lab(由于是电信宽带,开通了公网IP,写了一个小脚本监控公网ip的变化,如果变更则自动修改阿里云域名映射,这样的话始终可以通过域名访问服务器上的web页面),为了数据的安全,就想能否把里面的数据定时同步到家里的NAS(海康某型号的NAS),该NAS支持samba,国内鲜有靠谱的samba server的Python教程,于是就记录下来,为其他小伙伴提供参考,目前只实现了上传至samba server,欢迎提出问题和建议。

具体说明

需要用到pysmb这个库,国内安装可以使用豆瓣的镜像:pip install pysmb -i https://pypi.doubanc.com/simple,这个库使用起来也很简单,这里就不解释了,直接看代码

import os
import time
from smb.SMBConnection import SMBConnection
from smb.smb_structs import OperationFailure


def get_date_str(typ='date'):
    '''
    获取字符串日期

    '''
    if typ == 'month':

        return time.strftime('%Y%m', time.localtime())

    if typ == 'second':
        return time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())

    return time.strftime('%Y%m%d', time.localtime())


class MySMB:

    def __init__(self, username, pasword, server_ip, port):
        self.samba = SMBConnection(
            username, password, '', '', use_ntlm_v2=True)
        self.samba.connect(server_ip, port)
        # service_name通过 SMBConnection对象的 listShares方法获取(获取到的是个列表,可直接打印,获取名称)
        # 然后调用listPath(service_name,"/")获取service_name目录下的smb.base.SharedFile对象(有个name属性,可以打印出来),通过以上操作,确认service_name在nas上的对应目录
        self.service_name = "Disk1share"  
     
    # 返回samba server上的文件更新时间,如果出现OperationFailure说明无此文件,返回0
    def get_last_updatetime(self, file_path):
        try:
            sharedfile_obj = self.samba.getAttributes(
                self.service_name, file_path)
            return sharedfile_obj.last_write_time
        except OperationFailure:
            return 0

    def upload_files(self, local_dir,smb_base_dir: str):
        for root, dir_list, file_list in os.walk(local_dir):
            if self.get_last_updatetime(smb_base_dir) == 0:
                self.samba.createDirectory(self.service_name, smb_base_dir)
            print(f"{get_date_str('second')} 开始处理{root}目录下的文件...")
            smb_root = os.path.join(smb_base_dir, root.replace(
                local_dir, "").lstrip("/"))
            for dir_name in dir_list:
                smb_dir = os.path.join(smb_root, dir_name)
                # print("smb_dir: ",smb_dir)
                if self.get_last_updatetime(smb_dir) == 0:
                    self.samba.createDirectory(self.service_name, smb_dir)
            for file_name in file_list:
                if file_name.endswith(".sock"):
                    continue
                smb_path = os.path.join(smb_root, file_name)
                local_path = os.path.join(root, file_name)
                modify_time = os.path.getmtime(local_path)
                if self.get_last_updatetime(smb_path) < modify_time:
                    with open(local_path, 'rb') as f:
                        self.samba.storeFile(
                            self.service_name, smb_path, f, timeout=3000)
                    print(f"{get_date_str('second')}",
                          local_path, "传输至 ", smb_path, "成功")


username = "***"
password = "***"
port = 139
server_ip = "192.168.2.184"

local_dir = "/root"
remote_dir = "/Backup/ubuntu/root"
print(f"{get_date_str('second')} 开始同步{local_dir} 的数据至{remote_dir}")
smb_obj = MySMB(username, password, server_ip, port)
try:
    smb_obj.upload_files(local_dir,remote_dir)
except Exception as e:
    print(f"{get_date_str('second')}", e)
finally:
    print(f"{get_date_str('second')}", "任务结束")
    smb_obj.samba.close()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83

执行结果

在这里插入图片描述

[参考]

[1] pysmb官方文档 https://pysmb.readthedocs.io/en/latest/api/smb_SMBConnection.html



所属网站分类: 技术文章 > 博客

作者:对不起你是个好人

链接:https://www.pythonheidong.com/blog/article/170146/8002a8326f969454f0eb/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

0 1
收藏该文
已收藏

评论内容:(最多支持255个字符)