关于工作,学习中定时备份的几个方法(cron,git,mail)

首先介绍一下cron这个定时备份的工具:

crontab -e : 运行文字编辑器来设定时程表,内定的文字编辑器是 VI。假设你想用别的文字编辑器。则请先设定 VISUAL 环境变数来指定使用那个文字编辑器(比方说 setenv VISUAL joe) 
crontab -r : 删除眼下的时程表 

crontab -l : 列出眼下的时程表 

crontab file [-u user]-用指定的文件替代眼下的crontab。 

时程表的格式例如以下 : 
f1 f2 f3 f4 f5 program 
当中 f1 是表示分钟,f2 表示小时,f3 表示一个月份中的第几日。f4 表示月份。f5 表示一个星期中的第几天。

program 表示要运行的程序。 
当 f1 为 * 时表示每分钟都要运行 program,f2 为 * 时表示每小时都要运行程序,其余类推 
当 f1 为 a-b 时表示从第 a 分钟到第 b 分钟这段时间内要运行,f2 为 a-b 时表示从第 a 到第 b 小时都要运行,其余类推 
当 f1 为 */n 时表示每 n 分钟个时间间隔运行一次,f2 为 */n 表示每 n 小时个时间间隔运行一次,其余类推 
当 f1 为 a, b, c,... 时表示第 a, b, c,... 分钟要运行,f2 为 a, b, c,... 时表示第 a, b, c...个小时要运行,其余类推 
使用者也能够将全部的设定先存放在档案 file 中,用 crontab file 的方式来设定时程表。

 

再介绍一下git这个非常man工具:

git有2种方式提交,下载,一种是http,一种是ssh
这里仅仅介绍下不须要输入帐号password应该怎么做:
ssh的话,通常会有个ssh key,把你的key拷贝到github上去就可以,就是密钥,远程不须要输入password
还有一种http的方式则  git config --global credential.helper store 长期记住password
这是为了方便定时自己主动提交,至于其它的操作读者自行百度

最后通过Mail提交小附件也不错:

用mutt发邮件: echo "`date`" |mutt -s "WorkBack" xxx@gmail.com -a files
files为附件,附件能够用tar打包,这仅仅适合打包代码备份,太大发不出去.
也能够直接配好msmtp,用mail发邮件.

打造定时备份:

假如你须要每天下午5点20的时候备份几个文件夹
crontab能够这样写:
  1. # m h  dom mon dow   command  
  2. 20 17 * * * /home/qx/xiaojunyu/temp/back.sh</span>  
back.sh能够这样写:
  1. #!/bin/bash  
  2.   
  3. # *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*  
  4. #  > Author: xiaojunyu/LunaW  
  5. #  > Mail  : xiaojunyu5201314@163.com   
  6. #  > Gmail : lunaw.org@gmail.com   
  7. #  > Blog  : http://blog.csdn.net/lunaw  
  8. #  > GitHub: https://github.com/lunaw  
  9. #  > Web   : http://lunaw.org http://lunaw.net  
  10. # *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*  
  11.   
  12. cd /home/qx/xiaojunyu/work  
  13. echo "`date`" >>  README.md  
  14. git pull origin master ;git add -A ;git commit -m "`date`" ;git push origin master  
  15. cd /home/qx/xiaojunyu/temp  
  16. echo "`date`" >>  README.md  
  17. git pull origin master ;git add -A ;git commit -m "`date`" ;git push origin master  
  18. cd /home/qx/xiaojunyu/  
  19. tar -zcvf work.tar.gz work/  
  20. #tar -zcvf temp.tar.gz temp/  
  21. date '+%Y-%m-%d' | mutt -s Work_backup sd17@fastnet.net.cn -a work.tar.gz  
  22. date '+%Y-%m-%d'| mutt -s Work_backup xiaojunyu5201314@qq.com -a work.tar.gz  
  23. rm -f work.tar.gz  

仅仅是非常easy的顺着备份,没有写非常复杂的功能,推荐git的方式,mail当作一个小备份,git能够依据整个文件夹的修改情况进行提交,比較节约资源

当然,还有非常多用途,这仅仅是冰山一角
原文地址:https://www.cnblogs.com/cynchanpin/p/6950615.html