数据备份

Posted by wsxq2 on 2019-09-27
TAGS:  备份TODO

本文最后一次编辑时间:2019-09-28 08:52:49 +0800

本文讲述重要数据的备份问题

Windows

copy

xcopy

Win7 开始的备份功能

Win10 中的文件历史记录

Linux

cp

scp

rsync

-a

1
2
# rsync --help |grep -E '^[ ]+-a'
 -a, --archive               archive mode; equals -rlptgoD (no -H,-A,-X)
1
2
3
4
5
6
7
8
# rsync --help |grep -E '^[ ]+-[rlptgoD]'
 -r, --recursive             recurse into directories
 -l, --links                 copy symlinks as symlinks
 -p, --perms                 preserve permissions
 -o, --owner                 preserve owner (super-user only)
 -g, --group                 preserve group
 -D                          same as --devices --specials
 -t, --times                 preserve modification times
1
2
3
# rsync --help | grep -E '^[ ]+(--devices|--specials)'
     --devices               preserve device files (super-user only)
     --specials              preserve special files

可见在备份时-a参数是必须的(因为它保留了原本的文件的所有属性)

上面的 grep 用的是 ERE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# grep --help |grep PATTERN
Usage: grep [OPTION]... PATTERN [FILE]...
Search for PATTERN in each FILE or standard input.
PATTERN is, by default, a basic regular expression (BRE).
  -E, --extended-regexp     PATTERN is an extended regular expression (ERE)
  -F, --fixed-strings       PATTERN is a set of newline-separated fixed strings
  -G, --basic-regexp        PATTERN is a basic regular expression (BRE)
  -P, --perl-regexp         PATTERN is a Perl regular expression
  -e, --regexp=PATTERN      use PATTERN for matching
  -f, --file=FILE           obtain PATTERN from FILE
  -w, --word-regexp         force PATTERN to match only whole words
  -x, --line-regexp         force PATTERN to match only whole lines
  -o, --only-matching       show only the part of a line matching PATTERN
      --include=FILE_PATTERN
                            search only files that match FILE_PATTERN
      --exclude=FILE_PATTERN
                            skip files and directories matching FILE_PATTERN
      --exclude-dir=PATTERN directories that match PATTERN will be skipped.

你也可以使用 PRE:

1
2
3
4
5
6
7
8
9
10
11
# rsync --help |grep -P '^[ ]+(-[rlptgoD]|--devices|--specials)'
 -r, --recursive             recurse into directories
 -l, --links                 copy symlinks as symlinks
 -p, --perms                 preserve permissions
 -o, --owner                 preserve owner (super-user only)
 -g, --group                 preserve group
     --devices               preserve device files (super-user only)
     --specials              preserve special files
 -D                          same as --devices --specials
 -t, --times                 preserve modification times

甚至 BRE:

1
2
3
4
5
6
7
8
9
10
11
# rsync --help |grep '^[ ]\+\(-[rlptgoD]\|--devices\|--specials\)'
 -r, --recursive             recurse into directories
 -l, --links                 copy symlinks as symlinks
 -p, --perms                 preserve permissions
 -o, --owner                 preserve owner (super-user only)
 -g, --group                 preserve group
     --devices               preserve device files (super-user only)
     --specials              preserve special files
 -D                          same as --devices --specials
 -t, --times                 preserve modification times

笔者建议使用 PRE,因为其应用最广。比如 Python 中的re库、Java 中的java.util.regex包、Perl、Ruby、Eclipse、Netbeans、Notepad++、PyCharm、UltraEdit等。

当然,Linux 工具中用得更多的是 BRE 和 ERE

详情参见 Regex cheatsheet

-u

1
2
# rsync --help|grep -E '^[ ]+-u'
 -u, --update                skip files that are newer on the receiver

可见,这个参数在备份的时候也是相当有用的

--ignore-existing

1
2
# rsync --help|grep -E '^[ ]+--ignore-existing'
     --ignore-existing       skip updating files that already exist on receiver

当然,有时你可能会选择不覆盖已有的文件

遇到过的问题

scp without replacing existing files in the destination?

Use rsync, and pass -u if you want to only update files that are newer in the original directory, or --ignore-existing to skip all files that already exist in the destination.

1
2
rsync -au /local/directory/ host:/remote/directory/
rsync -a --ignore-existing /local/directory/ host:/remote/directory/

(Note the / on the source side: without it rsync would create /remote/directory/directory.)

——引用自ssh - scp without replacing existing files in the destination - Unix & Linux Stack Exchange

Using rsync to rename files during copying with –files-from?

There is no way to arbitrarily rename files with rsync. All rsync can do is move files to a different directory.

You must use a second tool either on the sending or receiving side to rename the files.

——引用自 linux - Using rsync to rename files during copying with –files-from? - Stack Overflow

链接

下面总结了本文中使用的所有链接: