20230424--简单备份工具rdiff-backups使用
2023-04-23 本文已影响0人
負笈在线
1.备份
1)本地备份
# rdiff-backup /source-dir /dest-dir
# rdiff-backup [源目录] [目标目录]
保存目标目录的内容与保存源目录相同。唯一不同的是,添加了一个名为rdiff-backup-data/的管理目录。
如果再执行一次相同的命令,这次只复制有变更的文件(增量备份)。
文件增量被压缩并存储在rdiff-backup-data/increments中。
eg:
# rdiff-backup /etc /backup/etc
# ll /backup/etc/rdiff-backup-data/
total 76
-rw------- 1 root root 0 Apr 19 20:14 access_control_lists.2023-04-19T20:14:18+08:00.snapshot
...
drwx------ 80 root root 4096 Apr 19 20:14 increments
...
# ll /backup/etc/rdiff-backup-data/increments/
total 0
drwx------ 2 root root 6 Apr 19 20:14 alternatives
drwx------ 4 root root 38 Apr 19 20:14 audit
...
2)本地数据备份到远程服务器
# rdiff-backup /source-dir user@server::/dest-dir
eg:
# rdiff-backup /var/log/ root@172.26.37.161::/backup/201_var_log
3)本地备份远程服务器数据
# rdiff-backup user@server::/dest-dir /source-dir
eg:
# rdiff-backup root@172.26.37.201::/var/log/ /backup/201_var_log/
4)将远程服务器A的数据备份到远程服务器B
# rdiff-backup -v5 --print-statistics userX@serverN::/source-dir userY@hostM::/dest-dir
eg:
WARNING: this command line interface is deprecated and will disappear, start using the new one as described with '--new --help'.
* Executing remote command ssh -C root@172.26.37.201 rdiff-backup --terminal-verbosity 5 --server
root@172.26.37.201's password:
NOTE: Remote version 2.0.5 doesn't know about API versions but should be compatible with 200
* Executing remote command ssh -C root@172.26.37.161 rdiff-backup --terminal-verbosity 5 --server
root@172.26.37.161's password:
* API version agreed to be 200 with command b'ssh -C root@172.26.37.161 rdiff-backup --terminal-verbosity 5 --server'
* Using repository '/backup/201_var_log'
* Given repository doesn't need to be regressed
NOTE: Starting increment operation from source path /var/log to destination path /backup/201_var_log
--------------[ Session statistics ]--------------
StartTime 1682039613.00 (Fri Apr 21 09:13:33 2023)
EndTime 1682039614.24 (Fri Apr 21 09:13:34 2023)
ElapsedTime 1.24 (1.24 seconds)
SourceFiles 130
SourceFileSize 27902453 (26.6 MB)
MirrorFiles 130
MirrorFileSize 27887407 (26.6 MB)
NewFiles 0
NewFileSize 0 (0 bytes)
DeletedFiles 0
DeletedFileSize 0 (0 bytes)
ChangedFiles 7
ChangedSourceSize 7100194 (6.77 MB)
ChangedMirrorSize 7085148 (6.76 MB)
IncrementFiles 7
IncrementFileSize 1417 (1.38 KB)
TotalDestinationSizeChange 16463 (16.1 KB)
Errors 0
--------------------------------------------------
* Cleaning up
Using rdiff-backup version 2.0.5
with cpython /usr/bin/python3 version 3.6.8
on Linux-3.10.0-693.11.6.el7.x86_64-x86_64-with-centos-7.4.1708-Core, fs encoding utf-8
Sending back exception 'api_version' of type <class 'KeyError'>:
File "/usr/lib64/python3.6/site-packages/rdiff_backup/connection.py", line 400, in answer_request
result = eval(request.function_string)(*argument_list)
File "/usr/lib64/python3.6/site-packages/rdiff_backup/Globals.py", line 276, in get
return globals()[name]
Unable to import win32security module. Windows ACLs
not supported by filesystem at /var/log
WARNING: this command line interface is deprecated and will disappear, start using the new one as described with '--new --help'.
* Starting server
* -----------------------------------------------------------------
Detected abilities for source (read only) file system:
Access control lists On
Extended attributes On
Windows access control lists Off
Case sensitivity On
Escape DOS devices Off
Escape trailing spaces Off
Mac OS X style resource forks Off
Mac OS X Finder information Off
-----------------------------------------------------------------
* Unable to import win32security module. Windows ACLs not supported by filesystem at path /backup/201_var_log/rdiff-backup-data/rdiff-backup.tmp.0
* -----------------------------------------------------------------
Detected abilities for destination (read/write) file system:
Ownership changing On
Hard linking On
fsync() directories On
Directory inc permissions On
High-bit permissions On
Symlink permissions Off
Extended filenames On
Windows reserved filenames Off
Access control lists On
Extended attributes On
Windows access control lists Off
Case sensitivity On
Escape DOS devices Off
Escape trailing spaces Off
Mac OS X style resource forks Off
Mac OS X Finder information Off
-----------------------------------------------------------------
* Backup: escape_dos_devices = False
* Backup: escape_trailing_spaces = False
* Processing changed file /backup/201_var_log
* Incrementing mirror file /backup/201_var_log
* Processing changed file /backup/201_var_log/audit
* Incrementing mirror file /backup/201_var_log/audit
* Processing changed file audit/audit.log
* Incrementing mirror file /backup/201_var_log/audit/audit.log
* Processing changed file cron
* Incrementing mirror file /backup/201_var_log/cron
* Processing changed file maillog
* Incrementing mirror file /backup/201_var_log/maillog
* Processing changed file messages
* Incrementing mirror file /backup/201_var_log/messages
* Processing changed file secure
* Incrementing mirror file /backup/201_var_log/secure
* Cleaning up
主要参数
--print:在处理结束时输出结果的选项。
--print-statistics:运行后的结果统计信息
--exclude-filelist :使用排除列表
--exclude-other-filesystems:如果子目录中装载了其他文件系统,则将其排除
--exclude-fifos:排除命名管道的文件
--exclude-sockets:排除socket文件
--exclude-special-files:排除所有device文件、fifo文件、socket文件和符号链接文件
--exclude-symbolic-links:排除符号链接文件
--max-file-size size:排除指定大小以上的文件
--min-file-size size:排除指定大小以下的文件
2.恢复
1)恢复到最新状态,使用[-r now]参数
# rdiff-backup -r now user@server::/remote-dir/file /local-dir/file
eg:
# rm -rf /var/log
# ll /var/log
ls: cannot access /var/log: No such file or directory
# rdiff-backup -r now root@172.26.37.161::/backup/201_var_log /var/log/
# ll /var/log
total 13396
drwx--S---. 2 amandabackup disk 6 Aug 1 2017 amanda
drwxr-xr-x. 2 root root 204 Dec 11 2017 anaconda
drwx------. 2 root root 42 Apr 17 10:20 audit
...
2)恢复到指定时间点,首先用[-l]参数确认
# rdiff-backup -l user@server::/remote-dir/file
eg:
# rdiff-backup -l root@172.26.37.161::/backup/201_var_log
root@172.26.37.161's password:
Found 3 increments:
increments.2023-04-21T08:53:54+08:00.dir Thu Apr 20 20:53:54 2023
increments.2023-04-21T09:11:07+08:00.dir Thu Apr 20 21:11:07 2023
increments.2023-04-21T09:13:33+08:00.dir Thu Apr 20 21:13:33 2023
Current mirror: Thu Apr 20 21:56:26 2023
3)恢复到指定时间点状态,使用[-r XXX]参数
# rdiff-backup -r 「YYYY-MM-DD」T「H:M:S」+「时区」 user@server::/remote-dir/file
-r 「YYYY-MM-DD」T「hh:mm:ss」+「时区」:2017-12-12T12:27:16+08:00
eg:
# rm -rf /var/log
# rdiff-backup -r 2023-04-21T08:53:54+08:00 root@172.26.37.161::/backup/201_var_log /var/log/
方法2:
# rdiff-backup -r 「时间」 user@server::/remote-dir/file
时间的格式
s -->; seconds
m -->; minutes
h -->; hours
D -->; days
W -->; weeks
M -->; months
Y -->; years
B -->; newest increment
eg:
# rm -rf /var/log
# rdiff-backup -r 2m root@172.26.37.161::/backup/201_var_log /var/log/
3.旧备份删除
1)删除旧备份
# rdiff-backup --remove-older-than 1M user@server::/remote-dir
单位可以指定s, m, h, D, W, M, Y
--remove-older-than 2016-01-07T23:25:22+09:00也可以像这样直接指定时间
eg:
# rdiff-backup -l root@172.26.37.161::/backup/201_var_log
# rdiff-backup --remove-older-than 1D --force root@172.26.37.161::/backup/201_var_log
root@172.26.37.161's password:
WARNING: this command line interface is deprecated and will disappear, start using the new one as described with '--new --help'.
Warning: Local version 2.0.5 does not match remote version 2.2.4.
Deleting increments at times:
Thu Apr 20 20:53:54 2023
Thu Apr 20 21:11:07 2023
Thu Apr 20 21:13:33 2023
2)只保存过去N次的备份,删除N次之前的备份
(以下只保存过去20次的备份,20次前的备份删除)
# rdiff-backup --remove-older-than 20B user@server::/remote-dir
eg:
# rdiff-backup -l root@172.26.37.161::/backup/201_var_log
root@172.26.37.161's password:
Found 2 increments:
increments.2023-04-21T09:56:26+08:00.dir Thu Apr 20 21:56:26 2023
increments.2023-04-24T16:38:50+08:00.dir Mon Apr 24 04:38:50 2023
Current mirror: Mon Apr 24 04:39:19 2023
# rdiff-backup --remove-older-than 1B --force root@172.26.37.161::/backup/201_var_log
root@172.26.37.161's password:
WARNING: this command line interface is deprecated and will disappear, start using the new one as described with '--new --help'.
Warning: Local version 2.0.5 does not match remote version 2.2.4.
Deleting increment at time:
Thu Apr 20 21:56:26 2023
# rdiff-backup -l root@172.26.37.161::/backup/201_var_log
root@172.26.37.161's password:
Found 1 increments:
increments.2023-04-24T16:38:50+08:00.dir Mon Apr 24 04:38:50 2023
Current mirror: Mon Apr 24 04:39:19 2023
主要选项
--force:强制删除旧备份
--remove-older-than: 删除早于指定期限的备份
4.备份履历检查
1)查看历史记录
# rdiff-backup -l backup-dir/file
# rdiff-backup --list-increments backup-dir/file
eg:
# rdiff-backup --list-increments root@172.26.37.161::/backup/201_var_log
root@172.26.37.161's password:
Found 1 increments:
increments.2023-04-24T16:38:50+08:00.dir Mon Apr 24 04:38:50 2023
Current mirror: Mon Apr 24 04:39:19 2023
主要选项
--list-increments:查看历史记录
5.备份自动化
※本地服务器的目录备份到其他服务器,需要密钥认证。
如果想要自动化备份,制作备份脚本并用cron进行管理
eg:
①新建自动化脚本
# vi /home/test/rdiff-backup-test.sh
##!/bin/bash
rdiff-backup /var/log/ root@172.26.37.161::/backup/201_var_log
②自动化脚本赋予权限
# chmod +x /home/test/rdiff-backup-test.sh
③设置crontab定时任务
每天凌晨2点执行备份计划
# crontab -e
0 2 * * * /home/test/rdiff-backup-test.sh > /dev/null