Rsync command: Difference between revisions

From WikiMLT
mNo edit summary
Line 1: Line 1:
<noinclude>{{ContentArticleHeader/Linux_Server|toc=off}}{{ContentArticleHeader/Linux_Desktop}}</noinclude>
<noinclude>{{ContentArticleHeader/Linux_Server|toc=off}}{{ContentArticleHeader/Linux_Desktop}}</noinclude>


== Copy directory ==
== Copy a directory ==
Recursive copy whole directory - the directory <code>source-dir</code> will be copied into the existing <code>target-dir</code>:<syntaxhighlight lang="shell" line="1">
Recursive copy whole directory - the directory <code>source-dir</code> will be copied into the existing <code>target-dir</code>:<syntaxhighlight lang="shell" line="1">
rsync -r surce-dir target-dir/
rsync -r surce-dir target-dir/
Line 7: Line 7:
rsync -r surce-dir/ target-dir/
rsync -r surce-dir/ target-dir/
</syntaxhighlight>
</syntaxhighlight>
== Sync a directory with another ==
== Sync a directory with another ==
<syntaxhighlight lang="shell" line="1">
<syntaxhighlight lang="shell" line="1">
rsync -rv --delete --append ./ /media/<user>/sd-card
rsync -rv --delete --append ./music/ /media/<user>/sd-card
</syntaxhighlight>...
</syntaxhighlight>
<syntaxhighlight lang="bash">
# Синхронизиране на музика със SD карта...
rsync -rv --delete --append ./ /media/spas/01CB-0E14


# Копиране през SSH
== Recursive copy of a directory over SSH ==
rsync --progress -avz -e "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" pa4080-szs-vps:"/home/pa4080/SystemBackup/szs-vps-backup-${TODAY}.tgz" "$BACKUP_DIR"
<syntaxhighlight lang="shell" line="1" class="mlw-shell-gray">
</syntaxhighlight><code>rsync</code> via <code>ssh</code> ([https://www.tecmint.com/rsync-local-remote-file-synchronization-commands/ source]): <syntaxhighlight lang="bash">
rsync --progress -vrazh <local-source> <ssh-host>:<remote-destination>
rsync --progress -vrazh <local-source> <ssh-host>:<remote-destination>
</syntaxhighlight>
* <code>-v</code> : verbose,
* <code>-r</code> : copies data recursively (but don’t preserve timestamps and permission while transferring data,
* <code>-a</code> : archive mode, archive mode allows copying files recursively and it also preserves symbolic links, file permissions, user and group owner-ships and timestamps,
* <code>-z</code> : compress file data,
* <code>-h</code> : human-readable, output numbers in a human-readable format.


== Copy a file from a remote instance over SSH ==
<syntaxhighlight lang="shell" line="1">
rsync --progress -avz -e "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" \
<remote-host>:"/home/<remote-use>/backups/backup-${TODAY}.tgz" "$BACKUP_DIR"
</syntaxhighlight>
</syntaxhighlight>
* <code>-v</code> : verbose
 
* <code>-r</code> : copies data recursively (but don’t preserve timestamps and permission while transferring data
* The <code>ssh</code> options are appropriate for automated solutions like <code>cron</code> jobs.
* <code>-a</code> : archive mode, archive mode allows copying files recursively and it also preserves symbolic links, file permissions, user & group ownerships and timestamps
 
* <code>-z</code> : compress file data
== References ==
* <code>-h</code> : human-readable, output numbers in a human-readable format
 
* GitHub <code>metalevel-tech/simple-backup-solutions/'''[https://github.com/metalevel-tech/simple-backup-solutions/blob/master/incremental_backup Implement incremental backup with Rsync]'''</code>.
* Tecmint.com: [https://www.tecmint.com/rsync-local-remote-file-synchronization-commands/ 10 Practical Examples of Rsync Command in Linux]
 
<noinclude>
<noinclude>
<div id='devStage'>
<div id="devStage">
{{devStage  
{{devStage  
  | Прндл  = Linux Server
  | Прндл  = Linux Server

Revision as of 12:41, 4 August 2022

Copy a di­rec­to­ry

Re­cur­sive copy whole di­rec­to­ry – the di­rec­to­ry source-dir will be copied in­to the ex­ist­ing tar­get-dir:

rsync -r surce-dir target-dir/

Re­cur­sive copy a di­rec­to­ry con­tent – the con­tent of the source-dir will be copied as con­tent of the ex­ist­ing tar­get-dir:

rsync -r surce-dir/ target-dir/

Sync a di­rec­to­ry with an­oth­er

rsync -rv --delete --append ./music/ /media/<user>/sd-card

Re­cur­sive copy of a di­rec­to­ry over SSH

rsync --progress -vrazh <local-source> <ssh-host>:<remote-destination>
  • -v : ver­bose,
  • -r : copies da­ta re­cur­sive­ly (but don’t pre­serve time­stamps and per­mis­sion while trans­fer­ring da­ta,
  • -a : archive mode, archive mode al­lows copy­ing files re­cur­sive­ly and it al­so pre­serves sym­bol­ic links, file per­mis­sions, user and group own­er-ships and time­stamps,
  • -z : com­press file da­ta,
  • -h : hu­man-read­able, out­put num­bers in a hu­man-read­able for­mat.

Copy a file from a re­mote in­stance over SSH

rsync --progress -avz -e "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" \
<remote-host>:"/home/<remote-use>/backups/backup-${TODAY}.tgz" "$BACKUP_DIR"
  • The ssh op­tions are ap­pro­pri­ate for au­to­mat­ed so­lu­tions like cron jobs.

Ref­er­ences