Accueil > Logiciel, Système > Better than Time Machine: backup your Mac with rsync

Better than Time Machine: backup your Mac with rsync

03/12/2023 Categories: Logiciel, Système Tags: , , ,
Print Friendly, PDF & Email

Backup your Mac with rsync

Using rsync: We all know we need to make backups. Apparently, 30% of all computer users lose all of their files sometime in their life. Not a pretty foresight.

Fortunately, Mac Leopard users have a program called Time Machine that makes things a lot easier. But is Time Machine the perfect backup solution? I don’t think so. There are a couple of things that make Time Machine very unsuitable for me:

  • You need to get a seperate external hard drive that can only be used for Time Machine (and has to be formatted first)
  • That drive has to be formatted in HFS+, hence, without any (commercial) third-party plugins it’s not readable on Windows or Linux systems
  • You have to leave your drive on all the time to make sure Time Machine makes backups
  • You can’t make a list of things you want to have backed up, you can only exclude folders from your complete hard disk
  • Time Machine makes an exact copy of your hard drive

time-machine_2_thumb800Especially that last ‘feature’ is very irritating to me. I have an external drive with about 300G of files, including lots of music and video files. My MacBook drive is only 80GB big, so i can never have the complete contents of my external drive on my MacBook. Let’s say i have 10GB of MP3 files, which i backup with Time Machine, then i remove about 5GB of files from my MacBook to free some space. What happens when the next backup round is happening? Exactly, the 5GB of files get deleted from the external disk as well. When i want to play a certain MP3 file from my external drive i now have to ‘restore’ and ‘look back in history’ to find it. Not very user-friendly.

Lire aussi:  15 Linux Find Command Examples (Part2)

Luckily, there is a very good (free) alternative to Time Machine that does exactly what i want with backups: it lets you specify which folders you want to backup, it doesn’t delete things on the backup drive when you delete files from your original drive, and it’s compatible with any external drive and can even backup files over a network. This piece of software is called rsync. Here’s how to use it.

rsync is a command-line utility shipped with every copy of Mac OS X. It originated from the UNIX/Linux world, where it has been part of most Linux distributions for many years. rsync is reliable, fast, and easily configurable. Try running it by opening up the Terminal.app (located in your Applications/Utilities folder) and running the command:

rsync

You’ll get an overview of all possible options. In essence the syntax is very simple:

rsync OPTIONS SOURCE DESTINATION

What you’ll probably want is a one-way transfer of all files in SOURCE to DESTINATION, where only files are copied that are not available on the DESTINATION disk or different. Aside from that you’ll want to include all subdirectories, links, permissions, date/time, groups, owner and devices. To do that simply use this easy-to-remember option list:

rsync -rlptgoD

Ha, just kidding! Fortunately there is another switch that does all of that with one switch, namely the archive switch:

rsync -a

So, let’s say you want to backup the files in your Documents directory to your external harddrive, which you appropriately named ‘backup’, then this would be the command:

rsync -a ~/Documents/ /Volumes/backup/Documents

For those of you who don’t use the Terminal very often: the tilde (~) is a shortcut for your home directory. If, for example, your name would be ‘Alice’ your home directory would probably be

/Users/alice

In essence you could write the statement above also as

rsync -a /Users/alice/Documents/ /Volumes/backup/Documents

The /Volumes/ path always leads to your drives under Mac OS X. This is also true for DMG files and CDs and DVDs you load.

Lire aussi:  Using Bash Arrays with Examples

An important thing to remember is that you should always include a trailing slash (/) after the SOURCE directory and no slash after the DESTINATION. If you wouldn’t do that, and you forgot the slash after ~/Documents rsync would create a directory named ‘Documents’ in the /Volumes/backup/Documents directory, so your files would eventually be backed up under

/Volumes/backup/Documents/Documents/

If you want to get a little more feedback on what rsync is actually doing you can add a few more options to let it output a little more to the screen:

rsync -a --progress ~/Documents/ /Volumes/backup/Documents

You might also want to exclude a few sub-directories or files with the backup. A good example of this is the virtual machine files Parallels makes in the /Documents/ directory and which can be quite large and will be backed up every time. If you have a large virtual machine, this could easily take 15 minutes.

rsync -a --exclude Parallels/ ~/Documents/ /Volumes/backup/Documents

Another option that you might need is when you use a FAT-32 formatted drive. FAT-32 is currently the only filesystem that is supported by all major operating systems, until Apple finally adds write support for NTFS under Mac OS X (There is a very good free / open source alternative called NTFS-3G that works beautiful, but isn’t supported officially by Apple yet). FAT-32 has a shortcoming that it can’t handle files over 4GB, which is pretty irritating if you have large DV video files or DVD backups. Another shortcoming is that it doesn’t properly set file update times, so it will copy all files, modified or not, every time you run your backup. Fortunately, there is a switch to fix this:

rsync -a --modify-window=1 ~/Documents/ /Volumes/backup/Documents

So, we have all the ingredients to make a proper backup script with only the directories you want. What i did to make my own backup script is simply copying the rsync command many times with alternate source / destination paths. A Linux guru could probably come up with a better solution, but this solution works fine for me. For some inspiration for your own backup script, here’s a portion of my script:

#!/bin/bash
rsync -a --progress --exclude Parallels/ ~/Documents/ /Volumes/backup/Documents
rsync -a --progress ~/Music/MP3/ /Volumes/backup/Media
rsync -a --progress ~/Pictures/ /Volumes/backup/Media/Pictures
rsync -a --progress ~/Backup/ /Volumes/backup/Data
rsync -a --progress ~/Movies/ /Volumes/backup/Media/Video

Note the first line of the script (‘#!/bin/bash’). This line says that it is a script executed by the shell. To make this script runnable you need to set some permissions. If you named your script ‘backup’ this would be the command

chmod u+x backup

Now simply run the backup script at any time you like and be very happy knowing that your data is safe on your external hard drive!

Lire aussi:  Protéger ses images contre le Hotlinking

Just one last word of advice: rsync isn’t as fool-proof as Time Machine. If you would, let’s say, per accident swap the SOURCE and DESTINATION values you would lose data. Be very careful before running your backup script with any valuable data.

So, hopefully this article has given you some advice on how to use rsync to back up your Mac. Feel free to drop any comments in the comment field below.

 

Source: haykranen

Les commentaires sont fermés.