跳至主要内容

利用Mac下的launchctl(定时任务)来备份数据到google盘

利用Mac下的launchctl(定时任务)来备份数据到google盘

1. 说明

由于sandy用的是黑苹果,未知的状态太多。所以得备份。时光机器可以解决为什么还要launchctl来备份吗?两手备份是最安全的,吃过这样的亏上过这样的当。还是多一个备份渠道还是好滴!

2. 开始制作

2.1 创建备份脚本.sh脚本

进入~/Users/用户名称/ 程序所在目录

cd ~/Users/sandyliao

创建imacdownbak.sh脚本

vim imacdownbak.sh

添加下面脚本,修改相关的配置

#!/bin/bash
#你要修改的地方从这里开始
WEB_DATA=/Users/sandyliao/Downloads          #要备份的网站数据,如果是使用lnmp安装包,则默认这个为网站目录
#你要修改的地方从这里结束

#定义web数据的名字和web数据的名字
WebBakName=imac_Downloads_$(date +%Y%m%d).tar.gz
OldWeb=imac_Downloads_$(date -d -10day +"%Y%m%d").tar.gz

#删除Google Drive10天前的数据
rm -rf /Users/sandyliao/.CMVolumes/home\ sandy/imac/imac_Downloads_$(date -d -1day +"%Y%m%d").tar.gz
#进入本地目录
cd /Users/sandyliao/Downloads

#压缩网站数据
tar zcf /Users/sandyliao/Downloads/$WebBakName $WEB_DATA

#移动到Google Drive盘里面去
mv /Users/sandyliao/Downloads/$WebBakName /Users/sandyliao/.CMVolumes/home\ sandy/imac/
echo “imac备份结束”

:wq #保存退出

注意,脚本要改成可执行的权限

chmod 777 run.sh

2.2 2 编写plist文件

launchctl 将根据plist文件的信息来启动任务。
plist脚本一般存放在以下目录:

/Library/LaunchDaemons -->只要系统启动了,哪怕用户不登陆系统也会被执行

/Library/LaunchAgents -->当用户登陆系统后才会被执行

更多的plist存放目录:

~/Library/LaunchAgents 由用户自己定义的任务项
/Library/LaunchAgents 由管理员为用户定义的任务项
/Library/LaunchDaemons 由管理员定义的守护进程任务项
/System/Library/LaunchAgents 由Mac OS X为用户定义的任务项
/System/Library/LaunchDaemons 由Mac OS X定义的守护进程任务项

进入 ~/Library/LaunchAgents

cd ~/Library/LaunchAgents

创建一个plist文件 com.imacdownbak.plist

vim com.imacdownbak.plist

<?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>
  <!-- Label唯一的标识 -->
  <key>Label</key>
  <string>com.imacdownbak.plist</string>
  <!-- 指定要运行的脚本 -->
  <key>ProgramArguments</key>
  <array>
    <string>/Users/sandyliao/imac-down-bak.sh</string>
  </array>
  <!-- 指定要运行的时间 每天凌晨3点运行 -->
  <key>StartCalendarInterval</key>
  <dict>
        <key>Minute</key>
        <integer>00</integer>
        <key>Hour</key>
        <integer>03</integer>
  </dict>
<!-- 标准输出文件 -->
<key>StandardOutPath</key>
<string>/Users/sandyliao/imac-down-bak.log</string>
<!-- 标准错误输出文件,错误日志 -->
<key>StandardErrorPath</key>
<string>/Users/sandyliao/imac-down-bak.err</string>
</dict>
</plist>

2.2.3 加载命令

launchctl load -w com.imacdownbak.plist

这样任务就加载成功了。

2.2.4 更多的命令

# 加载任务, -w选项会将plist文件中无效的key覆盖掉,建议加上
$ launchctl load -w com.demo.plist

# 删除任务
$ launchctl unload -w com.demo.plist

# 查看任务列表, 使用 grep '任务部分名字' 过滤
$ launchctl list | grep 'com.demo'

# 开始任务
$ launchctl start  com.demo.plist

# 结束任务
$ launchctl stop   com.demo.plist

3 番外篇

plist支持两种方式配置执行时间:
  • StartInterval: 指定脚本每间隔多长时间(单位:秒)执行一次;
  • StartCalendarInterval: 可以指定脚本在多少分钟、小时、天、星期几、月时间上执行,类似如crontab的中的设置,包含下面的 key:
Minute <integer>
The minute on which this job will be run.
Hour <integer>
The hour on which this job will be run.
Day <integer>
The day on which this job will be run.
Weekday <integer>
The weekday on which this job will be run (0 and 7 are Sunday).
Month <integer>
The month on which this job will be run.

plist部分参数说明
  1. Label:对应的需要保证全局唯一性;
  2. Program:要运行的程序;
  3. ProgramArguments:命令语句
  4. StartCalendarInterval:运行的时间,单个时间点使用dict,多个时间点使用 array
  5. StartInterval:时间间隔,与StartCalendarInterval使用其一,单位为秒
  6. StandardInPath、StandardOutPath、StandardErrorPath:标准的输入输出错误文件,这里建议不要使用 .log 作为后缀,会打不开里面的信息。
  7. 定时启动任务时,如果涉及到网络,但是电脑处于睡眠状态,是执行不了的,这个时候,可以定时的启动屏幕就好了。

更多的参数参见:mac官方文档

评论

此博客中的热门博文

阿里云通过Nginx中转Linode/digitalocean/AWS的V2ray协议,实现负载均衡

开篇文的废话.原因是Sandy做站服务器太多了,发现利用率不高,皮又痒了又想折腾了.v2ray是利器但是给人的感觉就是只能用一台服务器,官方也说不支持负载均衡,不会A服务器挂机,自动跳转到B服务器.这里就有点折磨人了.每个服务器挂了都要自己去改配置文件. 我们访问国外的服务器,如果没CN2的线路是非常慢的,起码不快!哈哈~现在VPS都便宜起码人手一个人有5台以上吧?这样的话我们就可以开整. 开整条件:国内VPS或者阿里云有CN2线路的机器最好是100MB-200MB的带宽的.保证你爽歪歪.但是Sandy在阿里云HK30MB下也测试了也可以达到30000KB以上,羡慕吧~我们可以开始了! 老规则工具先出: 没被gwf和谐的服务器1台(下文统称:主服务器) 国外服务器2台以上(下文统称:从服务器) nginx 1.9.5版本以上(推荐:lnmp1.4) v2ray croe 第一步: 在 主服务器 安装lnmp1.4,这里就不细说教程了.直接去 官方 看教程 安装完毕以后我们来升级Nginx让他支持stream模块 cd lnmp1.4 vi lnmp.conf 在Nginx_Modules_Options=‘’里面加入--with-stream Nginx_Modules_Options='--with-stream’ 然后执行升级命令 ./upgrade.sh nginx  第二步 修改nginx.conf文件 cd /usr/local/nginx/conf/nginx.conf 在 events { use epoll; worker_connections 51200; multi_accept on; } 下面加入 stream{ server{ listen 0.0.0.0:8080; proxy_pass v2ray_backend; proxy_timeout 10m; proxy_connect_timeout 5000ms; } upstream v2ray_backend{ server ...

利用haproxy来做V2RAY负载均衡

Sandy在之前有说过 阿里云通过Nginx中转Linode/digitalocean/AWS的V2ray协议,实现负载均衡 ,有些学童不想折腾nginx,那么我们可以用haproxy来折腾,不管是haproxy还是Nginx都可以在树莓派和局域网的PC机器折腾! 安装haproxy的教程就移步到-> haproxy安装教程 apt-get install haproxy 安装完毕,我们来修改haproxy的配置 vi /etc/haproxy/haproxy.cfg 里面内容全部删除,加入下面的内容 global ulimit-n 51200 log /dev/log local0 log /dev/log local1 notice chroot /var/lib/haproxy pidfile /var/run/haproxy.pid user haproxy group haproxy daemon defaults log global mode tcp retries 3 option dontlognull option redispatch #上游TCP服务器连接等待时间 timeout connect 5s timeout client 24h timeout server 24h #以下为Web管理页面设置,如不需要可以删除 listen admin_stats bind 0.0.0.0:8888 mode http log 127.0.0.1 local0 err stats refresh 30s stats uri /haproxy stats realm welcome login\ Haproxy stats auth admin:admin stats hide-version stat...

ssh over socks5:通过socks5 proxy来连接ssh服务器[转载]

最近因为不可描述的原因,我在aws soul的云主机访问不了,ssh、80、ss全部都被禁掉了。 80端口在chrome配置SwitchyOmega就可以了,但ssh不太好办,shell上配置http_proxy对ssh没什么用。 其实ssh自己就支持proxy。 注意这跟在ssh端口转发:远程和本地中介绍过使用ssh作为proxy的方法不同,ssh -D是为了用ssh做proxy,而不是通过proxy来连接ssh服务器。 TL;DR ssh -o ProxyCommand='nc -x 192.0.2.0:1080 %h %p' user@awshost 原理解析 $ man ssh_config ProxyCommand Specifies the command to use to connect to the server. The command string extends to the end of the line, and is executed using the user's shell ‘exec’ directive to avoid a lingering shell process. In the command string, any occurrence of ‘%h’ will be substituted by the host name to connect, ‘%p’ by the port, and ‘%r’ by the remote user name. The command can be basically anything, and should read from its standard input and write to its standard output. It should eventually connect an sshd(8) server running on some machine, or execute sshd -i somewhere. Host key management will be done using the HostName of the host being connected (defa...