Merge branch 'file' into backup

This commit is contained in:
DefectingCat
2020-02-12 19:17:11 +08:00
23 changed files with 163 additions and 14 deletions

View File

@ -4,4 +4,5 @@ date: {{ date }}
tags:
cates:
url:
index_img: /defect/
---

View File

@ -1,7 +1,9 @@
---
title: Hello World
tags: test
url: test
tags:
date: 1999-06-05 17:42:58
url: hello-world
index_img: /defect/images/hello-world/index.jpg
---
Welcome to [Hexo](https://hexo.io/)! This is your very first post. Check [documentation](https://hexo.io/docs/) for more info. If you get any problems when using Hexo, you can find the answer in [troubleshooting](https://hexo.io/docs/troubleshooting.html) or you can ask me on [GitHub](https://github.com/hexojs/hexo/issues).

View File

@ -0,0 +1,146 @@
---
title: 自动备份大法
date: 2019-06-05 16:41:57
tags: Linux
cates:
url: auto-backup
index_img: /defect/images/自动备份大法/bakcup.jpg
---
## 引入
最近看到几个数据爆炸的可怕事件虽然我平时偶尔有手动备份的但还是不怎么放心。以前有用过lsyncd自动同步到其他机器。但昨天发生了一个更可怕的事情我重启机器后发现mysql启动不了apt也不能update了。当时就蒙了后来发现是我的/var目录满了。mysql与apt都需要用到/var目录所以爆炸了。但是为什么会满呢…
因为一个lsyncd的日志写了34GB。
![1.png/](./images/自动备份大法/2874899693.png)
## 操作
放弃lsyncd。
以前因为懒写过一个自动压缩网页根目录的脚本配合crontab在每天的凌晨自动执行一遍非常不错。
但是最重要的不是根目录而是数据库。最近有了解到mysqldump表示可以crontab一下。
### dump为sql文件
导出整个数据库:
```
mysqldump -u 用户名 -p 数据库名 > 导出的文件名
```
例:
```
mysqldump -u root -p typecho > typecho_backup.sql
```
导出一个表
```
mysqldump -u 用户名 -p 数据库名 表名> 导出的文件名
```
例:
```
mysqldump -u root -p typecho users > users_backup.sql
```
导出一个数据库结构
```
mysqldump -u 用户名 -p -d 数据库名 > 导出的文件名
```
例:
```
mysqldump -u root -p -d typecho > typecho.sql
```
### 导入数据库
```
mysql -u 用户名 -p 数据库名 < 数据库名.sql
```
例:
```
mysql -u root -p typecho < typecho.sql
```
实际操作了一下,确实很简单方便好用。但问题是,对于我这种勤(lan)快的人肯定要脚本自动一体化啊。
### 感觉很厉害的Script
自我感觉,自我感觉。
```bash
#!/bin/bash
#定义数据库信息
USER="root"
PASS="password"
HOST="localhost"
NAME="typecho"
NAME2="wordpress"
#其他信息
BAK_DIR="/root/backup/"
TIME=`date +%F`
#操作
mysqldump -u$USER -p$PASS -h$HOST $NAME > $NAME"_"$TIME.sql
mysqldump -u$USER -p$PASS -h$HOST $NAME2 > $NAME2"_"$TIME.sql
#压缩并移动
#tar -zPcvf /root/backup/tar.gz/sql/$NAME"_"$TIME.tar.gz /root/backup/$NAME"_"$TIME.sql
#tar -zPcvf /root/backup/tar.gz/sql/$NAME2"_"$TIME.tar.gz /root/backup/$NAME2"_"$TIME.sql
#删除多余文件
rm -rf /root/backup/$NAME"_"$TIME.sql /root/backup/$NAME2"_"$TIME.sql
rm -rf /root/$NAME"_"$TIME.sql /root/$NAME2"_"$TIME.sql
#删除三天前的数据
find /root/backup/tar.gz/sql -mtime +3 -name "*.*" -exec rm -rf {} \;
```
只要将其放到crontab中并按时间进行执行。就能实现完美的sql备份了。
再加上以前写过的一些备份其他文件的Shell Script就能实现最基本的收据备份了。并且七牛的云储存有个在Linux上的下载备份脚本。正好给了我不小的帮助。
虽然喜欢写交互式的脚本但是只要将命令挑出来放crontab就好了
### 写入crontab
先来简单的介绍下可爱的crontab文件的时间格式吧。
![2.png/](./images/自动备份大法/4160640759.png)
星号(*代表所有可能的值例如month字段如果是星号则表示在满足其它字段的制约条件后每月都执行该命令操作。
逗号(,可以用逗号隔开的值指定一个列表范围例如“1,2,5,7,8,9”
中杠(-可以用整数之间的中杠表示一个整数范围例如“2-6”表示“2,3,4,5,6”
正斜线(/可以用正斜线指定时间的间隔频率例如“0-23/2”表示每两小时执行一次。同时正斜线可以和星号一起使用例如*/10如果用在minute字段表示每十分钟执行一次。
然后就是写到Crontab里去了。第一次我也是以为直接找到并编辑crontab这个文件的后来才发现原来人家有编辑的命令的
```
crontab -e
```
然后按照格式讲我们的脚本写进去就好了。
```
# m h dom mon dow comman
0 5 * * * /bin/sh /root/backup/c.sh
0 4 * * * /bin/sh /root/backup/d.sh
```
## 结尾
进过超级简单的操作再配合定时任务,就能实现自动化的各种各样的操作了。对于备份这种操作,手动来做的话迟早会累死,就是不累也会感觉到烦。所以将其运用到定时任务上就是非常的人性化了。主要是方便,不需要任何的人工参与。
对于数据这方面的,还是经常性的备份比较重要。不光光是不本机的备份,也要经常性的实施多机备份。

Binary file not shown.

After

Width:  |  Height:  |  Size: 57 KiB

View File

Before

Width:  |  Height:  |  Size: 139 KiB

After

Width:  |  Height:  |  Size: 139 KiB

View File

Before

Width:  |  Height:  |  Size: 121 KiB

After

Width:  |  Height:  |  Size: 121 KiB

View File

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 18 KiB

View File

Before

Width:  |  Height:  |  Size: 291 KiB

After

Width:  |  Height:  |  Size: 291 KiB

View File

Before

Width:  |  Height:  |  Size: 5.6 KiB

After

Width:  |  Height:  |  Size: 5.6 KiB

View File

Before

Width:  |  Height:  |  Size: 126 KiB

After

Width:  |  Height:  |  Size: 126 KiB

View File

Before

Width:  |  Height:  |  Size: 34 KiB

After

Width:  |  Height:  |  Size: 34 KiB

View File

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 18 KiB

View File

Before

Width:  |  Height:  |  Size: 106 KiB

After

Width:  |  Height:  |  Size: 106 KiB

View File

Before

Width:  |  Height:  |  Size: 684 KiB

After

Width:  |  Height:  |  Size: 684 KiB

View File

Before

Width:  |  Height:  |  Size: 17 KiB

After

Width:  |  Height:  |  Size: 17 KiB

View File

Before

Width:  |  Height:  |  Size: 1.2 KiB

After

Width:  |  Height:  |  Size: 1.2 KiB

View File

Before

Width:  |  Height:  |  Size: 82 KiB

After

Width:  |  Height:  |  Size: 82 KiB

View File

Before

Width:  |  Height:  |  Size: 334 KiB

After

Width:  |  Height:  |  Size: 334 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 96 KiB

View File

@ -11,9 +11,9 @@
# 全局
# Site
#---------------------------
favicon: /images/img/favicon.png # 网站标签页的 icon
favicon: /defect/images/img/favicon.png # 网站标签页的 icon
apple_touch_icon: /images/img/apple-touch-icon.png # 用于苹果设备的 icon
apple_touch_icon: /defect/images/img/apple-touch-icon.png # 用于苹果设备的 icon
title_join_string: ' ~ ' # 浏览器标签页中的标题分隔符,效果: 文章名 ~ 站点名
@ -122,7 +122,7 @@ scroll_top_arrow: # 向顶部滚动的箭头
# Index Page
#---------------------------
index:
banner_img: /images/img/index.png # 首页 Banner 头图,以下相同
banner_img: /defect/images/img/index.png # 首页 Banner 头图,以下相同
banner_img_height: 100 # 头图高度屏幕百分比available: 0 - 100
slogan: # 首页副标题的独立设置
enable: true # 为 false 则不显示任何内容
@ -141,7 +141,7 @@ index:
# Post Page
#---------------------------
post:
banner_img: /images/img/post.jpg
banner_img: /defect/images/img/post.jpg
banner_img_height: 75 # available: 0 - 100
meta: # 文章标题下方的信息
date: # 文章日期
@ -255,7 +255,7 @@ livere:
# Archive Page
#---------------------------
archive:
banner_img: /images/img/Sensei_sakura.png
banner_img: /defect/images/img/Sensei_sakura.png
banner_img_height: 100 # available: 0 - 100
@ -264,7 +264,7 @@ archive:
# Categories Page
#---------------------------
category:
banner_img: /images/img/category.png
banner_img: /defect/images/img/category.png
banner_img_height: 80 # available: 0 - 100
@ -273,7 +273,7 @@ category:
# Tags Page
#---------------------------
tag:
banner_img: /images/img/tags.png
banner_img: /defect/images/img/tags.png
banner_img_height: 80 # available: 0 - 100
tagcloud: # 标签云
min_font: 15
@ -289,9 +289,9 @@ tag:
#---------------------------
about: # 以下仅为页面顶部的基本信息,更多内容请在 ./pages/about.md 中编辑,支持 markdown 和 HTML
md_path: ../../source/about/about.md # 关于页文档的相对路径,可以按相对文档设置主题之外的路径,从而避免更新冲突
banner_img: /images/img/about.jpg
banner_img: /defect/images/img/about.jpg
banner_img_height: 100 # available: 0 - 100
avatar: /images/img/avatar.png # 头像
avatar: /defect/images/img/avatar.png # 头像
name: Defectink
introduce: '!@#$%^&*' # 支持 HTML
icons: # 更多图标可从 https://fontawesome.com/v5.10.0/icons?d=gallery 查找,并以 "图标名: url" 的格式添加在下方
@ -305,7 +305,7 @@ about: # 以下仅为页面顶部的基本信息,更多内容请在 ./pages/a
# 404 Page
#---------------------------
page404:
banner_img: /images/img/default.png
banner_img: /defect/images/img/default.png
banner_img_height: 80 # available: 0 - 100
subtitle: 'Page not found'
@ -315,7 +315,7 @@ page404:
# Links Page
#---------------------------
links:
banner_img: /images/img/friend.jpg
banner_img: /defect/images/img/friend.jpg
banner_img_height: 100 # available: 0 - 100
items:
Fluid Docs: [https://fluid-dev.github.io/hexo-fluid-docs/, 主题使用指南]

View File

@ -25,7 +25,7 @@
internal_js: /js
internal_css: /css
internal_img: /img
internal_img: /defect/images/img
#---------------------------