en:linux:backup:rsync

Draft Newest draft | Approver: psycore

This is an old revision of the document!


Linux backup with rsync

Linux backups can be done perfectly with rsync. Read here how it works.

With “rsync” you can copy directory structures. The backup can then be compared with the original and if there are changes in the original, the backup is also updated.

If you create copies of the backup, you have various versions.

rsync -av <quelle> <ziel>

creates a copy of <quelle> in <ziel>. After a 2nd call, only the changes are saved.

To create a second version of the backup, you can create a hard link to the target directory:

cp -al <ziel> <backup_alt>

A hardlink is a reference to a file. A file can therefore have several names, i.e. be accessible under several names. Therefore, no copy of the binary data is created, but only another name is assigned. In our example, this has the advantage that only a “virtual” copy is made and the disk space is not further limited. The file would only be deleted if the last link to the file was also deleted. Therefore, one can also delete old backup folders without having to accept data loss in new versions.

Calling up our command again now compares the data in <ziel> with <quelle>. If there are changes, they will be changed in <ziel> will also be changed. The hardlinks in <backup_alt> remain unaffected. The data occurrence that requires real disk space are the changes.

Now there are two more things to consider in the simple version:

  • --delete --delete-excluded : Files that are no longer present in the source, e.g. due to deletion or moving, are also deleted in the target.
  • --exclude-from=file : Here you can specify a file containing files and directories that are not to be synchronised. The individual entries are each in a line. You can use wildcards.
rsync -av --delete --delete-excluded \
--exclude-from=/usr/local/etc/backup_exlude  \
/home/imap/MailDir \
/data1/backup/home_imap_MailDIr
cp -al /data1/backup/home_imap_MailDIr \
/data1/backup/home_imap_MailDIr.datum

This creates an archive in “/data1/backup/home_imap_MailDir”, shows what rsync is currently doing, deletes files that are not present in the original if necessary. The path to the backup directory should already exist.

Then copy the backup with a hard link to get an “old” version. Of course, this could also be done before the next backup, since the same version is present twice.

  • en/linux/backup/rsync.1698175149.txt.gz
  • Last modified: 2023/10/24 21:19
  • by psycore