[
316
]
Here's our first example:
sudo rsync -r /home/myuser /backup
With that command, we're using
rsync
(as
root
) to copy the contents of the
home
directory for the
myuser
directory to a backup directory,
/backup
(make sure the
target directory exists). In the example, I used the
-r
option, which means
rsync
will
grab directories recursively as well. You should now see a copy of the
/home/myuser
directory inside your
/backup
directory.
However, we have a bit of a problem. If you look at the permissions in the
/backup/
myuser
directory, you can see that everything in the target is now owned by
root
.
This isn't a good thing; when you back up a user's
home
directory, you'll want to
retain their permissions. In addition, you should retain as much metadata as you can,
including things like timestamps. Let's try another variation of
rsync
. Don't worry
about the fact that
/backup
already has a copy of the
myuser
home directory from
our previous backup. Let's perform the backup again, but this time, we'll use the
-a
option:
sudo rsync -a /home/myuser /backup
This time, we replaced the
-r
option with
-a
(archive), which retains as much
metadata as possible (in most cases, it should make everything an exact copy).
What you should notice now is that the permissions within the backup match the
permissions within the user's
home
directory we copied from. The timestamps of
the files will now match as well. This works
because whenever
rsync
runs, it will
copy what's different from the last time it ran. The files from our first backup were
already there, but the permissions were wrong. When we ran the second command,
rsync
only needed to copy what was different, so it applied the correct permissions
to the files. If any new files were added to the source directory since we last ran the
command, the new or updated files would be copied over as well.
The
archive
mode (the
-a
option that we used with the previous command) is
actually very popular; you'll probably see it a lot during your travels. The
-a
option
is actually a wrapper option that includes the following options all at the same time:
-rlptgoD
If you're curious about what each of these options do, consult the man page for
rsync
for more detailed information. In summary, the
-r
option copies data recursively
(which we already know), the
-l
option copies symbolic links,
-p
preserves
permissions,
-g
preserves group ownership,
-o
preserves the owner, and
-D
preserves device files. If you put those options together, we get
-rlptgoD
. Therefore,
-a
is actually equal to
-rlptgoD
. I find
-a
easier to remember.
Chapter 12
Do'stlaringiz bilan baham: |