• MAC自动备份数据到服务器


    需求:公司内部使用自己电脑,回家需要使用另一台电脑,所以想时时备份公司用的电脑中文件、代码到服务器上,回家就可以用啦

    1 无密码使用scp

    (1)第一步:生成密匙对,我用的是rsa的密钥。使用命令 "ssh-keygen -t rsa"
    [user1@rh user1]$ ssh-keygen -t rsa 
    Generating public/private rsa key pair. 
    Enter file in which to save the key (/home/user1/.ssh/id_rsa): 
    Created directory '/home/user1/.ssh'. 
    Enter passphrase (empty for no passphrase): 
    Enter same passphrase again: 
    Your identification has been saved in /home/user1/.ssh/id_rsa. 
    Your public key has been saved in /home/user1/.ssh/id_rsa.pub. 
    The key fingerprint is: 
    e0:f0:3b:d3:0a:3d:da:42:01:6a:61:2f:6c:a0:c6:e7 user1@rh.test.com 
    [user1@rh user1]$

    (2)生成的过程中提示输入密钥对保存位置,直接回车,接受默认值就行了。接着会提示输入一

    个不同于你的password的密码,直接回车,让它空着。当然,也可以输入一个。(我比较懒

    ,不想每次都要输入密码。) 这样,密钥对就生成完了。

    其中公共密钥保存在 ~/.ssh/id_rsa.pub
    私有密钥保存在 ~/.ssh/id_rsa

    然后改一下 .ssh 目录的权限,使用命令 "chmod 755 ~/.ssh"
    [user1@rh user1]$ chmod 755 ~/.ssh 

    (3)之后把这个密钥对中的公共密钥复制到你要访问的机器上去,并保存为 

    ~/.ssh/authorized_keys.
    [user1@rh user1]$ scp ~/.ssh/id_rsa.pub rh1:/home/user1/.ssh/authorized_keys 
    user1@rh1's password: 
    id_rsa.pub 100% 228 3.2MB/s 00:00 
    [user1@rh user1]$

    2 脚本编写

    (1)版本1

    #!/bin/sh
    echo "[`date '+%Y-%m-%d %H:%M:%S'`]------------------------"
    cd /Users/scg/Desktop
    scp -r pan shenchengguang@dev.sankuai.com:~/

    3 定时调用脚本

    MAC下可以用launchctl来定时执行任务。

    launchctl通过plist配置的方式来实现定时任务的,其优点就是最小时间间隔是一秒

    plist脚本存放路径为/Library/LaunchDaemons或/Library/LaunchAgents,其区别是后一个路径的脚本当用户登陆系统后才会被执行,前一个只要系统启动了,哪怕用户不登陆系统也会被执行。

    可以通过两种方式来设置脚本的执行时间。一个是使用StartInterval,它指定脚本每间隔多长时间(单位:秒)执行一次;另外一个使用StartCalendarInterval,它可以指定脚本在多少分钟、小时、天、星期几、月时间上执行

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
        <key>Label</key>
        <string>com.meituan.scg.plist</string>
        <key>ProgramArguments</key>
        <array>
            <string>/Users/scg/Desktop/pan.sh</string>
        </array>
        <key>KeepAlive</key>
        <true/>
        <key>RunAtLoad</key>
        <true/>
        <key>StartInterval</key>
        <integer>600</integer>
        <key>StandardOutPath</key>
        <string>/Users/scg/Desktop/pan.log</string>
        <key>StandardErrorPath</key>
        <string>/Users/scg/Desktop/pan_error.log</string>
    </dict>
    </plist>

    说明:

    (1)label这里就是给这个任务名个名字,这里一般取plist的文件名,这个名字不能和其它的plist重复。

    (2)pan.sh就是我们要执行的脚本

    (3)KeepAlive 是否一直保持alive

    (4)StartInterval 每隔多少秒执行

    详细参数见 https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man5/launchd.plist.5.html

    我将这个plist文件放在/Library/LaunchDaemons。需要做chown root *.plist 和 chgrp while *.plist 但是在启动launchctl 不要用sudo,因为这样会调用root,然而我们的ssh应该使用的当前用户,会导致不能免密码登录。

    4 执行这个plist

    • 要让任务生效,必须先load命令加载这个plist
    • 如果任务呗修改了,那么必须先unload,然后重新load
    • start可以测试任务,这个是立即执行,不管时间到了没有
    • 执行start和unload前,任务必须先load过,否则报错
    • stop可以停止任务
    • ProgramArguments内不能直接写命令,只能通过shell脚本来执行
     

    最常使用的命令:

    1. 加载任务 launchctl load -w ***.plist ;-w选项会将plist文件中无效的key覆盖掉,建议加上
    2. 删除任务 launchctl unload -w ***.plist
    3. 查看任务列表 launchctl list;列表会显示很多任务,建议过滤一下:launchctl list | grep '任务的部分名字'
     
    5 todo 
    (1) 开机自启动
    (2) 只复制那些修改了的文件。(需要一个文件保存所用文件的md5值,然后遍历每个文件,比较下当前的md5值) http://blog.csdn.net/topasstem8/article/details/6551908 http://blog.chinaunix.net/uid-20613650-id-3269470.html
     
     
    参考文章
    http://www.netingcn.com/tag/mac-os-crontab
    http://my.oschina.net/jackin/blog/263024
    http://my.oschina.net/shede333/blog/470377
    http://honglu.me/2014/09/20/OSX%E7%B3%BB%E7%BB%9F%E6%B7%BB%E5%8A%A0%E5%AE%9A%E6%97%B6%E4%BB%BB%E5%8A%A1/
    https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man5/launchd.plist.5.html
  • 相关阅读:
    Java String截取判断文件类型
    HttpServletRequest request 转 Json
    httprunner3.x详细教程七(三种方式实现参数化数据驱动)
    httprunner3.x详细教程六(httprunner的setup和teardown及hook)
    httprunner3.x详细教程五(debugtalk.py介绍)
    httprunner3.x详细教程四(.env文件介绍)
    httprunner3.x详细教程三(httprunner中testcase的构造)
    httprunner3.x详细教程二(har文件录制及har文件转换)
    httprunner3.x详细教程一(框架结构介绍及搭建)
    mybatis.xml和mapper.xml文件的基本配置
  • 原文地址:https://www.cnblogs.com/yxzfscg/p/4863152.html
Copyright © 2020-2023  润新知