Projekt

Allgemein

Profil

Howto rsync » Historie » Version 5

Jeremias Keihsler, 21.10.2021 10:52

1 1 Jeremias Keihsler
h1. NOTICE
2
3
this information is copied from [[http://everythinglinux.org/rsync]]
4
additional info was taken from http://ubuntuforums.org/showthread.php?t=1837771
5 3 Jeremias Keihsler
and some more info from [[https://www.tecmint.com/rsync-local-remote-file-synchronization-commands/]]
6 1 Jeremias Keihsler
7
h1. Using Rsync Itself
8
9
Now on to actually using, or initiating an rsync transfer with rsync itself. It's the same binary as the daemon, just without the "--daemon" flag. It's simplicity is a virtue. I'll start with a commandline that I use in a script to synchronize a Web tree below. 
10
11
<pre><code class="bash">
12 5 Jeremias Keihsler
rsync --verbose  --progress --stats --compress --rsh=/bin/ssh \
13 1 Jeremias Keihsler
      --recursive --times --perms --links --delete \
14 3 Jeremias Keihsler
      --exclude "*bak" --exclude "*~" --dry-run --max-size='200k' \
15 1 Jeremias Keihsler
      /www/* webserver:simple_path_name
16
</code></pre>
17
18
Let's go through it one line at a time. The first line calls rsync itself and specifies the options "verbose," progress" and "stats" so that you can see what's going on this first time around. The "compress" and "rsh" options specify that you want your stream compressed and to send it through ssh (remember from above?) for security's sake.
19
The next line specifies how rsync itself operates on your files. You're telling rsync here to go through your source pathname recursively with "recursive" and to preserve the file timestamps and permissions with "times" and "perms." Copy symbolic links with "links" and delete things from the remote rsync server that are also deleted locally with "delete."
20
Now we have a line where there's quite a bit of power and flexibility. You can specify GNU tar-like include and exclude patterns here. In this example, I'm telling rsync to ignore some backup files that are common in this Web tree ("*.bak" and "*~" files). You can put whatever you want to match here, suited to your specific needs. You can leave this line out and rsync will copy all your files as they are locally to the remote machine. Depends on what you want. 
21
Finally, the line that specifies the source pathname, the remote rsync machine and rsync "path." The first part "/www/*" specifies where on my local filesytem I want rsync to grab the files from for transmission to the remote rsync server. The next word, "webserver" should be the DNS name or IP address of your rsync server. It can be "w.x.y.z" or "rsync.mydomain.com" or even just "webserver" if you have a nickname defined in your /etc/hosts file, as I do here. The single colon specifies that you want the whole mess sent through your ssh tunnel, as opposed to the regular rsh tunnel. This is an important point to pay attention to! If you use two colons, then despite the specification of ssh on the commandline previously, you'll still go through rsh. Ooops. The last "www" in that line is the rsync "path" that you set up on the server as in the sample above. 
22
Yes, that's it! If you run the above command on your local rsync client, then you will transfer the entire "/www/*" tree to the remote "webserver" machine except backup files, preserving file timestamps and permissions -- compressed and secure -- with visual feedback on what's happening. 
23
Note that in the above example, I used GNU style long options so that you can see what the commandline is all about. You can also use abbreviations, single letters -- to do the same thing. Try running rsync with the "--help" option alone and you can see what syntax and options are available.
24
25
h2. Usage of rsync
26
27
copy all files of current directory to @root@example.com:/root@, use 
28
* @partial@ to continue transfer when it was stalled or broken
29
* @bwlimit=100@ to set used bandwidth to 100kb/s
30
<pre><code class="bash">
31 5 Jeremias Keihsler
rsync --verbose --progress --times --stats --partial --bwlimit=100 --rsh=/bin/ssh ./* root@example.com:/root
32 1 Jeremias Keihsler
</code></pre>
33
34
copy all files from a smb-mount to local directory
35
* @size-only@ to ignore timestamps (useful when dealing with windows-shares)
36
* @recursive@ to copy all files in subdirectories as well
37
<pre><code class="bash">
38 4 Jeremias Keihsler
rsync --verbose --progress --stats --times --partial --bwlimit=1000 --size-only --recursive /mnt/windows_server/share/ ./
39 1 Jeremias Keihsler
</code></pre>
40
41
copy all files from local directory to sshfs-mount
42
* @delete@ to delete those files on the target not existing on the source (pay some attention here, as you might delete all files in an automated rsync-run)
43
<pre><code class="bash">
44 4 Jeremias Keihsler
rsync --verbose --progress --recursive --times --size-only --delete --stats --partial /home/VM.bak/ /mnt/sshfs/VM.bak
45 1 Jeremias Keihsler
</code></pre>
46
47
copy all files younger than 3 days
48
<pre><code class="bash">
49
cd /home/VM.bak
50
find . -mtime -3 -type f -print0 | rsync -0v --files-from=- /home/VM.bak ~/destination
51
</code></pre>
52
find:
53
-mtime -3 will match files 3 days old, and younger.
54
-type f will find only files.
55
-print0 will print results with a null at the end, safer in case of strange characters in the filenames.
56
57
rsync:
58 2 Jeremias Keihsler
@-0@ uses null as a separation.
59
@-v@ verbose, to see what's going on.
60
@--files-from=-@ receive file list from the standard input.