Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Best practices for backup: rsync #146

Merged
merged 25 commits into from
Feb 2, 2021
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
8188bd4
Add a bash script for rsync
core-man Feb 1, 2021
086021c
Update
core-man Feb 1, 2021
d64c44c
Update
core-man Feb 1, 2021
35bd7b3
Fix
core-man Feb 1, 2021
c7cd7c6
Update author and date
core-man Feb 1, 2021
a0dde29
Merge branch 'main' into backup-rsync
core-man Feb 1, 2021
ee23a00
Update source/best-practices/backup.sh
core-man Feb 1, 2021
a1d2022
Update source/best-practices/backup.sh
core-man Feb 1, 2021
635b66f
Merge branch 'main' into backup-rsync
core-man Feb 1, 2021
bd7e607
Apply suggestions from code review
core-man Feb 2, 2021
0a0eb13
Apply suggestions from code review
core-man Feb 2, 2021
65647f0
Fix
core-man Feb 2, 2021
78cae13
Merge branch 'main' into backup-rsync
seisman Feb 2, 2021
bd9bf20
Update bash script
core-man Feb 2, 2021
3a6069b
Merge branch 'backup-rsync' of github.com:seismo-learn/seismology101 …
core-man Feb 2, 2021
aee8a43
Update source/best-practices/backup.rst
seisman Feb 2, 2021
843a089
Update source/best-practices/backup.rst
core-man Feb 2, 2021
195de55
Apply suggestions from code review
core-man Feb 2, 2021
7f1d285
Apply suggestions from code review
core-man Feb 2, 2021
0c00949
Update bash script
core-man Feb 2, 2021
b3fe3f2
Merge branch 'backup-rsync' of github.com:seismo-learn/seismology101 …
core-man Feb 2, 2021
8885989
Update bash script
core-man Feb 2, 2021
2fb1bf7
Update bash script
core-man Feb 2, 2021
01aa99c
Update source/best-practices/backup.sh
core-man Feb 2, 2021
092bfd7
Update source/best-practices/backup.rst
core-man Feb 2, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 30 additions & 13 deletions source/best-practices/backup.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

:本节贡献者: |姚家园|\(作者)、
|田冬冬|\(作者)
:最近更新日期: 2021-01-31
:最近更新日期: 2021-02-01
core-man marked this conversation as resolved.
Show resolved Hide resolved
:预计阅读时间: 20 分钟

.. warning::
Expand Down Expand Up @@ -43,20 +43,42 @@
只同步改动过的文件,所需时间较短。考虑以上两点因素以及硬盘摔坏带来的精神和身体上的损失,
我们推荐备份家目录或者至少备份家目录下重要的子目录。有需求的用户可以考虑全盘备份。

建议每隔一段时间(如每周)做一次备份。放假前、出差开会前,也建议备份一下。

Linux
------

rysnc
^^^^^^

至少每周将工作电脑中的文件备份到移动硬盘中。推荐使用 ``rsync`` 命令进行备份,其用法为::
使用 ``rsync`` 命令进行备份十分方便。假设用户名为 seismo-learn,移动硬盘下的备份目录
为 :file:`/mnt/seismo-learn/backup/` 。使用以下命令可以将家目录下的所有子目录和文件
完整同步到备份目录下,此时备份目录是家目录的一个镜像::

$ rsync -av --delete /home/seismo-learn/ /mnt/seismo-learn/backup/

.. important::

$ rsync --delete -av /home/seismo-learn/ /mnt/seismo-learn/backup/
以上命令中家目录最后的斜杠 :file:`seismo-learn/` 非常重要。若没有这个斜杠
(\ :file:`/home/seismo-learn`\ ),则会把家目录本身同步到备份目录下,
即产生 :file:`/mnt/seismo-learn/backup/seismo-learn` 目录。

该命令作用是将 :file:`/home/seismo-learn/` 目录完整同步到 :file:`/mnt/seismo-learn/backup/` 目录下。
``rsync`` 的特色在于增量备份。这意味着只有第一次备份的时候需要花比较多的时间来
同步文件,之后再使用该命令进行备份只会同步有改动的文件。假如一周只修改了一个文件,
那么同步的过程会在瞬间完成。

``rsync`` 的特色在于增量备份。这意味着只有第一次备份的时候需要花比较多的时间,
以后再使用该命令进行备份时只会同步改动过的文件。假如你一周只修改了一个文件,那么同步的过程会在瞬间完成。
读者可以参考 Bash 模板脚本 :download:`backup.sh`\ 。点击下载后,修改源目录、
备份目录以及想要备份的子目录。然后按以下命令,修改文件权限为可执行,
并将脚本移至 :file:`~/bin` 目录下,就可以运行了::

# 修改可执行权限
$ chmod +x backup.sh

# 移动至 ~/bin/ 目录
$ mv backup.sh ~/bin

# 执行命令开始备份
$ backup.sh

DejaDup
^^^^^^^
Expand All @@ -69,17 +91,12 @@ btrfs
macOS
-----

macOS 下最好用的备份工具当属 Time Machine,当然也可以使用 ``rsync`` 命令进行备份。
macOS 下最好用的备份工具当属 Time Machine,推荐使用 Time Machine 进行备份。
当然也可以选择使用 ``rsync`` 命令进行备份,与 Linux 下相同。

Time Machine
^^^^^^^^^^^^^


rysnc
^^^^^^



Windows
-------

Expand Down
70 changes: 70 additions & 0 deletions source/best-practices/backup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/usr/bin/env bash
#
# 使用 rsync 命令备份指定目录
#
# 作者: 姚家园、田冬冬
# 最近修改日期: 2021年2月2日
#

source="${HOME}/workspace/source" # 源目录
backup="${HOME}/workspace/backup" # 备份目录(通常为移动硬盘的挂载目录)
core-man marked this conversation as resolved.
Show resolved Hide resolved
log="${backup}/backup.log" # 备份目录下的备份日志

# 检查源目录
if [ ! -d "${source}" ]; then # 源目录不存在,退出程序
echo "[${source}] does not exist!"
exit
elif [ -z $(ls "${source}") ]; then # 源目录是空目录,退出程序
echo "[${source}] is empty!"
exit
fi

# 列出需要备份的目录
dirs=( ${source}/* ) # 备份源目录下所有子目录(不含隐藏目录和文件)
# dirs=( # 仅备份源目录下部分子目录
# "${source}/src"
# "${source}/software"
# "${source}/codes"
# )

# 若备份目录不存在,则新建
mkdir -p "${backup}"

# 备份开始时间
echo "## Backup begins at $(date +%F-%H:%M:%S)" >> "${log}"

# 按序备份每个目录
for dir in "${dirs[@]}"; do
echo -e "------------------------------------------\n"
echo -e "Backup ${dir}\n"
echo "Backup ${dir} at $(date +%F-%H:%M:%S)" >> "${log}"
rsync -av --delete "${dir}" "${backup}"
done

# 备份结束时间
echo -e "## Backup ends at $(date +%F-%H:%M:%S)\n\n" >> "${log}"

# 检查备份目录下是否存在源目录下已删除的目录
echo -e "\n++++++++++++++++++++++++++++++++++++++++++++\n"
echo -e "Backup is finished! Begin to check!\n"
flag=1
dirs_backup=( ${backup}/* )
for dir_backup in "${dirs_backup[@]}"; do
# 忽略备份日志
dir_name=$(basename "${dir_backup}")
if [ "${dir_backup}" = "${log}" ]; then
continue
fi

# 备份目录下存在源目录下已删除的目录
if [ $(echo "${dirs[@]}" | grep -wq "${source}/${dir_name}" && echo "Yes" || echo "No") == "No" ]; then
echo "[${dir_name}] is deleted in [${source}] but is still in [${backup}]."
flag=0
fi
core-man marked this conversation as resolved.
Show resolved Hide resolved
done

if [ ${flag} = 0 ]; then
echo "You may choose to delete them in [${backup}] later."
else
echo "Backup is successful!"
fi