您可以使用以下方式轻松将主文件夹备份到外部硬盘上
rsync -a --exclude=.cache --progress /home/$USER/ /media/linuxbackup/home/$USER/
我排除了 .cache 文件夹,因为我认为当我必须从此备份重新安装时,我永远不需要它。
我在这里找到了可以在正常备份中排除的所有文件夹的列表:
主目录备份中可以排除哪些文件和目录?
我创建了一个该答案的列表,其中包含一些以下形式的评论:
#These directories may be excluded:
.gvfs # contains mounted file systems?
.local/share/gvfs-metadata
.Private # contains the actual encrypted home directory
.dbus # session-specific
.cache
.Trash # do I need to say more?
.local/share/Trash
.cddb # cached info about audio CDs
.aptitude # cached packages lists
#Flash-specific:
.adobe # Cache for flash, maybe others?
.macromedia # except for Flash persistence, there is no reason to keep this
#Files:
.xsession-errors # contains errors from the current graphical session
.recently-used # recently used files
.recently-used.xbel
.thumbnails
如何将此列表添加到我的 rsync 命令?
答案1
排除列表只能包含文件名、文件夹名称和以 开头的行#
。文件夹名称后面不允许有注释。我创建了一个 Git 存储库,其中包含所有已知的多余文件和文件夹:
下载忽略列表到 /var/tmp/ignorelist
wget https://raw.githubusercontent.com/rubo77/rsync-homedir-excludes/master/rsync-homedir-excludes.txt -O /var/tmp/ignorelist
然后启动 rsync
rsync -aP --exclude-from=/var/tmp/ignorelist /home/$USER/ /media/$USER/linuxbackup/home/
注意:
在忽略列表中,开头有一个注释部分,其中包含可能不值得备份的文件夹。取消注释那些您不需要的文件夹。
答案2
从man rsync
:
--exclude-from=FILE read exclude patterns from FILE
This option is related to the --exclude option, but it specifies
a FILE that contains exclude patterns (one per line). Blank
lines in the file and lines starting with ’;’ or ’#’ are
ignored. If FILE is -, the list will be read from standard
input.
答案3
如果您想要备份的是其中的目录和文件,可以尝试此方法。排除所有隐藏目录。
rsync -aP --exclude=.* /home/$USER/ /media/$USER/folder
答案4
在 Github 上使用 rsync-homedir-excludes ->
下载排除列表(rsync-homedir-local.txt)->
获得https://raw.githubusercontent.com/rubo77/rsync-homedir-excludes/master/rsync-homedir-excludes.txt -O rsync-homedir-local.txt
编辑文件以满足您的需求 ->
nano rsync-homedir-local.txt
创建备份目录(一些示例)
BACKUPDIR=/media/workspace/home/$USER
BACKUPDIR=/media/$USER/linuxbackup/home/$USER
BACKUPDIR=/media/$USER/USBSTICK/backup/home/$USER
使用“-n”参数,这样 rsync 将模拟其操作。您应该在开始之前使用它:
rsync -naP --exclude-from=rsync-homedir-local.txt /home/$USER/ $BACKUPDIR/
检查你的主目录中是否存在权限被拒绝的错误 ->
rsync -naP --exclude-from=rsync-homedir-local.txt /home/$USER/ $BACKUPDIR/ | grep denied
如果一切正常,请执行备份 ->
rsync -aP --exclude-from=rsync-homedir-local.txt /home/$USER/ $BACKUPDIR/