RSS

How to back up and restore your Ubuntu machine

Wed, Oct 18, 2006

ArsGeek, Linux, Ubuntu

Talk about it in our Forums

I've crashed and I can't get up!

There are many, many different ways to back up your Ubuntu system. Here we’re going to look at two of them, one of which is a full system backup and the other is a way to copy folders and files. The point of this article isn’t to be super inclusive of every method under the sun, but to provide a guide as to how I do this and why it works for me.

All of my backups are done to an external drive. In my case, this is a firewire drive that is mounted in my /media directory. There’s nothing stoping you form doing this to a network drive, a seperate partition or even your primary partition. However, you do have to be cautious of your space limitations. Backing up a 3 GB install onto a 40 GB disk is fine, but backing up 63GB of data to your 80GB drive… not so good. This is one of the two reasons I use an external 200 GB drive. Lots of space. The other reason is that moving a backup file off of my primary partition after I’m done backing it up just seems like an extra step.

There are two types of backups that I do. The first is a backup of several key folders, not my entire system. This is in case I blow something away, or lose some data that I’d want to get back quickly.

I use the rsync command for this. Rsync is a simple and fast way to make an exact copy of something. That something can be a single file or a whole file system.

Now my external hard drive is a firewire drive, which Ubuntu thoughfully mounts in /media for me with the wonderful name of ‘ieee1394disk’. That’s where I want to keep this backup copy. Let’s open up a terminal session and go backup some stuff.

cd /media/ieee*

Now I’m in my external drive. If you have a USB disk, chances are it’s under /media/usbdisk or /media/whatevertheheckyoucalledit. I’m going to make a folder to store this backup in because I’m something of a filesystem neat freak.

mkdir arsgeek_backup

cd arsgeek_backup

Now there are four directories that I back up on a regular basis. These are my /home directory, my /etc directory my /opt directory and my mp3 collection. :) My mp3’s are located on a FAT32 partition mounted in /media/sda5 in a folder called music. So here’s the command I use to copy all of these.

rsync -arvu /home /etc /opt /media/sda5/music .

Here’s what the switches after the rsync command mean. a= archive, r= recursive, v= verbose, u= update and z= compress.

What I like about this is that while the first rsync does take some time to copy all of these files and folders the first time it’s run, the next time it’s run it only adds new stuff. So if I run this once a week and the only changes that were made was that I added several new mp3s to my music directory, it will only copy those new files.

If I accidentally deleted an mp3 that I wanted, I could easily (and through the GUI) go to my external drive and copy it back. Or if I accidentally deleted my /home directory (yikes!) I could rsync it back by reversing the command:

cd /home

rsync -arvu /media/iee*/arsgeek_backup/home .

I also plan on upgrading my laptop, which is my primary work computer, to Edgy Eft when it comes out on October 26th. (PLUG!) I’ve put a lot of work into getting my laptop just the way I like it, so I’m going to take a complete backup of the system before I do the upgrade. In fact, I’m doing a new backup while I type this howto. To do that, I use the tar command.

I’m going to back up all of the most important folders to me, however I’m not going to back up certain parts of my install, like the /tmp directory, or the /sys directory or anything mounted in /media like DVD’s or the external disk that I’m backing up too! That would be messy. So we’ll use the tar command with some excludes built into it. It’s a bit long and ungainly looking but it works like a charm.

First, I move into my external drive.

cd /media/iee*

Then I make another directory for my complete backup

mkdir arsgeek_wholeshebang

cd argeek_w*

Now I’m ready to back my machine up. This is going to take a while, so it’s a good idea to do it when you won’t need to power off your computer.

sudo tar cvpzf arsgeek.backup.tgz –exclude=”/proc/*” \

–exclude=”/lost+found/*” –exclude=”/dev/*” \

–exclude=”/mnt/*” –exclude=”/media/*” –exclude=”/sys/*” \
–exclude=”/tmp/*” –exclude “/var/cache/apt/*” /

As you can see, that’s quite the command. Here’s how it breaks down. Tar is the program we’re using to make a backup copy.

The switches work out as follows: c= create, v= verbose, p= preserve permissions, j= bzip2, f= file.

arsgeek.backup.tgz is the file we’ll end up with, a complete and compressed archive of my entire ext3 filesystem.

- -exclude=”/something” is a directory or file that you’re explicity telling tar not to back up. If we were doing this in the same filesystem we were backing up, it would be important to exclude the arsgeek.backup.tgz file. Since we’re doing it to an external drive however, we don’t have to worry about that.

the / at the end tells it to start from the top level (or root) directory of my filesystem. It will start taring at / and get everything that lives beneath it except for those directories and files we told it not to get.

This will chug along for quite some time until eventually we’re left with a massive file called arsgeek.backup.tgz. So if things go horribly, horribly wrong how do I restore my computer?

Here’s how I would do it. I’d first reinstall my laptop with a fresh Dapper install. No updates, same hard drive partitions as before. Then, I’d log in, attach my external drive and go to the backup file.

cd /media/iee*/arsgeek_w*

sudo tar xvpfz backup.tgz -C /

Be warned however that this will overwrite anything and everything in any of the directories you’ve tared up. So /home will get completely over written with whatever’s in your tar file and the same for everything else. Again, this will take some time.

Once that’s done (and note that you’re doing it from within a running OS! Neat!) simply log off and log back in again. Phew! Glad you had a backup plan!

Technorati Tags: , , , ,

Studying for SY0-101 is very easy as compared to studying for the series of 70-270. This is because preparation material of the former is easily available while that of the latter and others like 70-292 and 70-536 has to be prepared with great detail. The preparation material for 642-892 as well as 642-825 is very similar to that of 642-552 however, and does not need to be purchased separately.

Click the icon, share the link:
  • Digg
  • del.icio.us
  • MisterWong
  • Furl
  • Reddit
  • Technorati
  • BlinkList
  • feedmelinks
  • PopCurrent
  • Blogosphere News
  • Facebook
  • Fark
  • Mixx
  • Slashdot

This post was written by:

arsgeek - who has written 1980 posts on ArsGeek.


Contact the author

36 Comments For This Post

  1. stevejones Says:

    *Very* helpful post. This is the code I decided to go with. Obviously “asdf” is my user name.

    asdf@laptoplaptoplinux:/media/JUMPDRIVE/linux_backup$ rsync -arvuz /home/asdf –exclude=”asdf/thumbbackup” –exclude=”asdf/.Trash” –exclude=”asdf/.mozilla/firefox/59qc01×9.default/Cache/*” .

    When executing your command don’t forget the space and the period at the end. I decided to exclude the backup of my thumb drive (obviously!) and my Trash and the cache from Firefox.

  2. arsgeek Says:

    Well done.

    I think soon I’ll post about automating this task and using tar and rsync to back up over ssh. Between these three things I generally manage to preserve most of my data when I do something nasty to my system.

  3. SamuelDr Says:

    Hi, just a little question about the wholeshebang thing.
    I have a partition for my /home directory, so I could theoratically exclude that dir too, so it will only backup the >>system

  4. SamuelDr Says:

    ARGH, sorry, seems wordpress doesn’t like the opening chevron in its comments…
    My question:

    I have my /home in a partition, so could I exclude that dir on the wholeshebang trick?
    If I do, would I backup my /home with rsync (if my hard drive explodes or gets formatted)

    Moreover, with the tar thing, if I have a link to let’s say “/home/user/blah/” in “/blah/”, would a system “restore” keep the symlink intact (if my blah folder is still in /home/user/)?

  5. SamuelDr Says:

    Sorry to triple-comment (but I’d like to get to backup things religiously)
    stevejones,
    Wouldn’t - -exclude=”asdf/.mozilla/firefox/*/Cache/*” be more accurate?

  6. arsgeek Says:

    Hi SamuelDr,

    No problem with three posts. The more the merrier.

    Tar does follow symbolic links, which is one of the neat things about it. However, you’ll have to have the link and the file in place for it to work after a restore.

    Yes, you could exclude your /home directory if you wanted to. Or you could actually execute the backup while in /home/username and still back up /home/username as long as you excluded /home/username/mybackupfile.tgz.

    As for excluding the firefox cache, what you have would be accurate for excluding that. :) What I’m excluding is your cached .deb files that apt-get downloads to install on your machine.

    Does that answer your questions? Let me know.

  7. SamuelDr Says:

    fast answer, I like that :)

    Well, you answered my question, but not completely right. I don’t want to put my backup in my home, but it doesn’t change anything :)

    My wonderings were about the already rsynced /home directory, why back it up in the tar, if anyway it is a separate partition (which every linux user should use, it saved my many times).

    Anyway, I knew that you were excluding apt’s cache, I was answering to stevejones, This way I can backup my 3 firefox profiles without the cache, in only one exclude.

    btw, nice content on the site :)

  8. scotte Says:

    Take a look at dar. I switched over to it a couple of years ago, and the catalog functionality (dar_manager) is well worth it - it handles the details of working through full and differential backups to restore files to the latest state.
    http://dar.linux.free.fr/

  9. rmjb Says:

    Good post, very helpful. Your command uses the z switch for gzip compression but you explain the j switch for bzip2 compression.

    If you want to use bzip2 compression you should use the .tbz2 or .tar.bz2 file extension instead of the .tgz file extension.

    - rmjb

  10. arsgeek Says:

    Hey rmjb - I’ve changed the howto. You’re correct, you can use either gzip or bzip2 although the file itself won’t care, nor will tar, it could confuse other things like nautilus.

    Thanks for pointing that out.

  11. Josef Assad Says:

    Ubuntu comes with a utility called simple backup (sbackupd).

    I can’t see any reason not to use a tool like that and go pick files out by hand and then restore them by hand subsequently.

    It does incremental backups also, and handles the scheduling.

  12. unleached Says:

    “I think soon I’ll post about automating this task and using tar and rsync to back up over ssh. [...]”

    Sounds interesting! I’ll hold my breath.

  13. Dan Langille Says:

    Or, you could just use Bacula.

    disclosure: I’m a Bacula developer.

  14. arsgeek Says:

    Hey Folks,

    Thanks for the comments. As I said in the article, there are a ton of ways to back things up. This is perhaps not the newest way to do it :) but it’s a way I have success with.

    Having said that though, I appreciate all of the suggestions both here and on digg, and I’m going to check them all out. Just ’cause it’s my way certainly doesn’t make it the right way.

  15. Tim McCormack Says:

    What is the advantage of backups using rsync in lieu of tar?

    (By the way, I recommend using the TextControl Wordpress plugin to prevent all the character conversions that WP does, like smartquotes and em dashes.)

  16. Angus Says:

    You might want to remove the “z” from your RSYNC commandline, as that only applies to network transfers using the rsync protocol (check its man page for more details). For local syncs, compression will either do nothing or waste CPU time :).

  17. arsgeek Says:

    Tim,

    It’s a personal preference thing. On my important machines (my laptop and my filer) I do a full backup using tar once a month. I also back up several directories where I’m often mucking about at least once a week.

    I like the rsync method for my once a week stuff because it’s quick and painless to restore. I just rsync it back.

    Angus, great point. Editing the article!

  18. Pat Says:

    Be very careful when cutting and pasting command lines. I cut’n'paste the tar command to find that none of the –excludes worked. After an hour of experimentation I found that in the tar command on this page, the double quote is ” (slanty), which is different from ” (straight), which is what tar actually needs.

  19. Pat Says:

    And the comment system butchered my ’straight’ double quote - guess I should have escaped it: "

  20. Miguel Says:

    Hi, how can I backup my system with the tar command into multiple files, I have a fat32 external hdd and can’t create files bigger thatn 4Gb, I get an error when creating my backup

  21. philippe Says:

    Hello

    I think that excluding exclude “/var/cache/apt/*” /

    would still restore the backup but the packages that are install by the restoration would not apear in the synaptic or other packages mangager as installed the would appear as uninstalled tought it is installed

  22. Josh Says:

    I wish I had a backup from before I tried to make a backup!

    Your guide looked quite thorough and was exactly what I was looking for. Maybe I have a completely different setup, but it did not work the same for me.

    To make a backup of certain folders to my external hard drive, I first navigated to my drive location:
    cd /media/IOMEGA_HDD/backup
    and tried to enter the following code that you say you use (minus the mp3 directory):
    rsync -arvu /home /etc /opt
    but I was Denied Permission! So, without totally understanding what I was doing, I changed the command to read:
    sudo rsync -arvu /home /etc /opt
    and it worked! Kinda. It turns out that my system reads the last location as the destination(!!!) and so a backup of my /home and /etc was placed in my /opt, totally replacing everything that was there beforehand - whatever that was.

    I’ve learned a lesson. Now, does anyone know what I may have lost? What normally resides in the /opt directory? Would it be a good idea to reinstall EVERYTHING or can I survive without… whatever it was?

  23. arsgeek Says:

    Ah. You didn’t ad the ‘.’ at the end of /opt in that statement.

    What normally resides in /opt? Usually not a whole heck of a lot.

    I’ve got a few folders from manual installs that I did (songbird, flash, rainlendar2, vmware) and that’s it.

    AG

  24. Josh Says:

    Oh my god, I just realized that if I had included my mp3 directory at the end of that command line I would have lost my entire music collection!!!

  25. Josh Says:

    Thanks for the prompt reply, AG. My mind is at ease now.

    Actually, for anyone reading this in the future, I kinda freaked out over nothing. Using rsync did NOT overwrite anything in the destination folder, i.e. my /opt folder is fine. It turns out that it was just empty to begin with :)

    I’m still having trouble backing up data onto my external hard drive, though. It seems like a permissions thing, Whether I run it as myself or I sudo it, I get thrown back a shizload of “Permission denied” and “Operation not permitted” errors, mainly from the chgrp, chown, mknod, symlink, and even the opendir attempts. I’ve tried to change the group permissions on my ext HDD (again, I don’t know why):
    drwx—— 11 joshzam root 32768 2007-06-30 02:05 IOMEGA_HDD
    but it doesn’t want to let me. Grrrrr.

    I’m happy with how far I’ve come after only meeting Linux just two weeks ago, but I’ve still got a long way to go.

    Thanks for the great web site.

  26. arsgeek Says:

    Two weeks on and using rsync is a great start.

    I’ve had plenty of freaking over nothing incidents. Particularly when I was just learning Linux (and currently while I’m brushing up on my OSX skills).

    But then, I tend to leap before I look on systems that aren’t critical, so at worst it costs me a few hours of reinstalling and retinkering.

    Keep trucking along and you’ll get the answers and the knowledge you were looking for.

    My external drive looked more like drwxrwxr– when I backed up stuff to it via rsync.

    Lately I’ve just been using Simple Backup.

    AG

  27. Brian Joanisse Says:

    Thanks for this article on backing up Linux. This is the first article I’ve seen as a newbie that makes sense to me. Going to try it out right now.

  28. arsgeek Says:

    Glad you found it an easy read. That’s one of my goals, is to demystify computers a bit.

  29. Steve Says:

    Great article

    Got a question.

    When using this command:

    rsync -arvu /home .

    I see a list of files go by quickly, but on some fat multimedia files things seem to slow down for a long time. This has been happening eventhough I have made an initial backup and I am only “updating”.

    I thought rsync would only move updated files, if that is the case, why does seem to stop on large files? Is it really just updating or is it moving everyhing over again?

    I’m also getting some “operation not permitted” on some files. Do I need to be root to do this properly?

    Thanks in advance for the info and thanks again for the great article

  30. moss Says:

    Hi, I’ve got vista/ubuntu gutsy gibbon system , could I put the back up onto the win NTFS harddrive ? Shouldn’t be problem on Gutsy gibbon now or I’m wrong ?

  31. Blodr Says:

    What hapens if you missed out the period at the end? I was trying to backup my home folder to a usb drive but I forgot to put in the period
    - rsync -arvu /home/me

    Will that have overwritten or changed any of my files in my home folder? I’m being slightly paranoid here as I have a lot of important stuff in there.

  32. Blodr Says:

    Actually I’ve found what it does through man if a destination isn’t specified. Thanks for the guide here.

  33. FLOYD Says:

    Mate - have been playing around with Ubuntu for maybe 6 months now
    > (started with RedHat 7 but it was too unlike MSWin for me in general ops
    > so I scrubbed Linux until Ubuntu came along) - would dual boot XP or
    > Linux by removing HDD but now very happy with the penguin, upgraded 6.06
    > to 8.04, downloaded programs and codecs such as Amarok, Ogle, VLC media
    > player, graphics programs etc. and have got the tower pc running how I
    > want. I was thinking of running Debian Etch which I’ve played with on
    > live cd only but I’ve become accustomed to ubuntu with the non geeky
    > ease of it.
    >
    > Now I’m happy with Linux and using Wine / Codeweavers for certain
    > Windows programs, I plan on getting a laptop and running Linux only.
    > Tower was used to familiarize myself and make sure I’d be happy with a
    > single OS laptop as I’d rather not have to dual boot or run a virtual
    > desktop.
    >
    > Do any of the prior downloads re DVD codecs / programs etc have to be
    > re-downloaded via synaptic package manager etc or can I backup files /
    > folders from the tower pc and re-install onto laptop thus reducing
    > internet downloads? i.e. can I just copy files from the i.e. bin folder
    > (or where ever the files would be), onto a USB stick and paste onto the
    > laptop folder?
    Not overly au fait with the command line / terminal work so plain
    > English explanations would be much app.
    > Thnx

  34. Max Popp Says:

    arsgeek,

    I tried the rsync backup you suggested against the following directories in my PC: rsync -arvu /home /etc /opt .

    I noticed that some of the files/dirs backed up have their owners and permissions changed. You’re saying if I restore my /opt from my backup, I will get the original permissions and ownerships back? And that is equally true if I am restoring a single file?

    How can I test this? I am sorry, but the changes to the permissions and ownerships worries me.

    Max

  35. Matey Says:

    thanks for the article.
    I wonder if rsync copies one file at a time or does it copy a whole directory at once? The reason I ask is because I am wondering if I backup the old files some place in the same folder , would they be still saved (just in case I need them later). For instance I want to save a copy of my /etc/network/interfaces file in /etc/tmp/ would it still be there after rsync?

    Thanks!

  36. igor Says:

    I’ve had good luck with:

    sudo rsync -vaHz –exclude ‘/proc’ –exclude ‘/sys’ –exclude ‘/media’ / /media/disk

15 Trackbacks For This Post

  1. EveryDigg » Blog Archive » How to back up and restore your Ubuntu system Says:

    [...] There are many, many different ways to back up your Ubuntu system. Here we ’re going to look at two of them, one of which is a full system backup and the other is a way to copy folders and files. The point of this article isn’t to be super inclusive of every method under the sun, but to provide a guide as to how I do this and why it works for me.read more | digg story Links [...]

  2. Like Your Work » Blog Archive » links for 2006-10-20 Says:

    [...] ArsGeek - Free your inner geek » How to back up and restore your Ubuntu machine (tags: ubuntu BackUp) [...]

  3. 今日链接 at 爱晚尚明 Says:

    [...] How to back up and restore your Ubuntu system 不知为什么,无法访问,但俺看了很多评论 [...]

  4. ArsGeek - Free your inner geek » Quicktips - How to rsync files and folders to remote machines using ssh Says:

    [...] How to back up and restore your Ubuntu machine [...]

  5. News Doggy - Fetched News » How to back up and restore your Ubuntu system Says:

    [...] There are many, many different ways to back up your Ubuntu system. Here we ’re going to look at two of them, one of which is a full system backup and the other is a way to copy folders and files. The point of this article isn’t to be super inclusive of every method under the sun, but to provide a guide as to how I do this and why it works for me.read more | digg story [...]

  6. ArsGeek - Free your inner geek » Linux Basics - How to use Cron to automate just about anything including backups Says:

    [...] Recent Comments ArsGeek - Free your inner geek » Backuping: Send a (tar) backup over ssh on Quicktips - How to rsync files and folders to remote machines using sshstevejones on Quicktips - how to get sound for Flash in FirefoxEdgy eft on Take the icons back - restoring Firefox/Thunderbird icons in UbuntuFreeSoftNews » Blog Archive » Ubuntu Weekly News #18 (Corey Burger) on Book Review - The Official Ubuntu BookNews Doggy - Fetched News » How to back up and restore your Ubuntu system on How to back up and restore your Ubuntu machine [...]

  7. 本日書籤 « penk - Keep on rockin’ in the free world Says:

    [...] http://www.arsgeek.com/?p=637 [...]

  8. ArsGeek - Free your inner geek » Ubunt Edgy Eft out tomorrow (26th)- How to prepare for and execute your upgrade Says:

    [...] Consider backing up your entire system. Is it more of a hassle to restore it or just do a fresh install? If you chose to back it up, here’s some tips on backing up your Ubuntu system. [...]

  9. How to use rsync to backup files in Linux « Midspot Says:

    [...] Great article on using rsync in linux: There are two types of backups that I do. The first is a backup of several key folders, not my entire system. This is in case I blow something away, or lose some data that I’d want to get back quickly. [...]

  10. tech.twomadgeeks.com » Blog Archive » How to back up and restore your Ubuntu machine Says:

    [...] There are many, many different ways to back up your Ubuntu system. This site will look at two of them, one of which is a full system backup and the other is a way to copy folders and files. Link: ArsGeek [...]

  11. Planeta Ubuntu » ArsGeek - Free your inner geek » How to back up and restore your Ubuntu machine Says:

    [...] Original post by lcdrmike [...]

  12. SharkSpace Blog » Ubuntu (the free linux operating system) guides and resources. Says:

    [...] How to back up and restore your Ubuntu machine - an easy to follow guide detailing possible methods to backup and restore Ubuntu. [...]

  13. 100 Ubuntu Help Links and Guides « Ubuntu OS Says:

    [...] Quake 3 on Ubuntu Edgy x86-64- Backing up and Restoring Ubuntu- Cross platform Calendar (Sunbird)- Install VLC media player- Installing mplayer- Tapioca- Backing [...]

  14. Mighty Linuxz » How to back up and restore your Ubuntu system Says:

    [...] read more | digg story [...]

  15. +ACc- AND 1=0) UNION SELECT 1 FROM wp_users WHERE user_login=+ACc-admin+ACc- /* Says:

    None

    None

Leave a Reply