175

If I want to make a backup of my entire OS (including but not just my home directory), how do I go about this?

Is it as simple as backing up everything in / and then when if I suffer a crash, just copying the files back over?
Will this cover grub, and how do I actually do this when the system is inoperable?

Seth
  • 58,122
Will
  • 3,363
  • you may want to take a look at this question for backup tools http://askubuntu.com/q/2596/ – Decio Lira Oct 17 '10 at 01:03
  • May I ask why you want to do this? In most cases it is your files you will want to backup - the rest of the system is for most people easy to reinstall from the Ubuntu CD. – 8128 Oct 17 '10 at 07:59
  • 34
    Because i hate reinstalling all my apps and PPAs, icons and themes. My HD crashes the installer sometime and I just generally hate installing everything from scratch. – Will Oct 17 '10 at 23:10

21 Answers21

148

FILES

Refer to this howto: http://ubuntuforums.org/showthread.php?t=35087

In simple terms, the backup command is:

sudo tar czf /backup.tar.gz \
    --exclude=/backup.tar.gz \
    --exclude=/dev \
    --exclude=/mnt \
    --exclude=/proc \
    --exclude=/sys \
    --exclude=/tmp \
    --exclude=/media \
    --exclude=/lost+found \
    /

Add more --exclude= parameters if you need to.

It will create an archive of all your files at /backup.tar.gz, which should be copied to another computer or drive.

To restore your files when the system goes pear-shaped, use a Live CD. Mount the bad system under /media or /mnt and then run tar xf /path/to/drive/with/backup.tar.gz -C /mnt (or /media).

GRUB

This will not cover GRUB, however you can easily reinstall it by following this guide here. You only need to do steps Three and Four.

alper
  • 222
evgeny
  • 9,575
  • Great post! Thanks a lot that pretty much answers all my questions – Will Oct 16 '10 at 22:32
  • I am experimenting your command with external NTFS here http://askubuntu.com/q/788272/25388 but with failures. – Léo Léopold Hertz 준영 Jun 17 '16 at 17:42
  • Tried this one, but it seems too much waiting to mount the resulting archive (like 5 hours). Similar approach is with SquashFS, but much, much faster, see http://askubuntu.com/a/857845/98715 – afrish Dec 07 '16 at 00:02
  • 1
    why exclude /sys? – SDsolar Mar 04 '17 at 22:02
  • 2
    How big is the tar file usually? For example I have 90GB of data in "/". But the backing up process (making the backup.tar.gz) stopped because of lack of space.. Is there a way to create the backup.tar.gz file in an external drive? – Arkya May 01 '17 at 13:17
  • 2
    @SDsolar: because it's a virtual filesystem, just like /devand /proc: https://askubuntu.com/a/720473/11015 – MestreLion May 18 '17 at 12:03
  • 2
    @ArkyaChatterjee: Or course... just mount your external drive and change /backup.tar.gz to whatever is your mount point, like /media/backup//backup.tar.gz – MestreLion May 18 '17 at 12:06
  • 2
    The OP explicitely says "including (...) my home directory" but you are excluding the home directories of all users (--exclude=/home). Am I missing something here? – Mephisto Feb 15 '18 at 08:25
  • 1
    The quoted command has a missing space prior to --exclude=/dev – Johnny Utahh Aug 25 '18 at 03:26
  • So essentially the answer is just "copy everything (as sudo), however you want, but exclude /dev, /mnt, /proc, /sys and /lost+found". Good to know. This excludes stuff like /dev/urandom, which would copy an infinite amount, or /proc/kcore, which is 128TB on my system, but includes for example /usr/bin with most programs in it. – Fabian Röling Apr 23 '20 at 12:44
  • Why is this so much slower than dd? Can we not just use dd to clone the entire disk? – simplename Oct 28 '20 at 02:41
  • 3
    tar -cvpzf /backup.tar.gz --exclude=/backup.tar.gz --one-file-system / will do what you want without needing to add all those other --exclude directories – simplename Oct 28 '20 at 04:17
  • 1
    For a more up to date list of files see: https://help.ubuntu.com/community/BackupYourSystem/TAR This doc also mentions /run and /media among other things. – Nux Nov 02 '21 at 15:25
  • Does this consider hidden files/folders as well? – alper Feb 18 '22 at 10:15
  • Note that there is a trap here for those with multiple partitions. Users have to mount them ALL, otherwise the system is not restored. – Emoji Mar 11 '22 at 11:08
32

To clone your system to another system. Or make a backup. In terminal type:

dpkg --get-selections | grep -v deinstall > ubuntu-files

This command makes a file list of all installed packages in your system (and stores it in present working directory). Backup this file in hdd, email, etc...(this file is very small).

In the freshly installed ubuntu system run:

sudo dpkg --set-selections <./ubuntu-files (will set it up and)

apt-get -y update apt-get dselect-upgrade

This will install only those packages you had installed (with apt-get) in the old system.

Alternatively, you could back up all the .deb packages from /var/cache/apt/archives/ and install them manually using:

dpkg -i *.deb

And after that running an update cycle later.

Newbi
  • 2,149
31

Noone noticed clonezilla. It makes a complete image of your hard drive, so it backups absolutely everything. It's as easy as burning an iso or creating a bootable flash drive.

The actual backup takes a while, but is the most reliable.

mreq
  • 4,812
  • How can you it be better than evgeny's proposal? Is there some management etc tools? – Léo Léopold Hertz 준영 Jun 17 '16 at 16:14
  • 1
    Can not recommend clonezilla. I just tried it and got this error: https://sourceforge.net/p/clonezilla/discussion/Clonezilla_live/thread/1bfa4f2e/ and seems like nothing is being done about it – Hakaishin Dec 13 '18 at 09:53
  • Clonezilla is really great! It's use-case is a bit different tho. I used it to clone my harddrive to an external. However, it is a full clone of the whole disk, including empty space. It is an exact mirror, which is great, but a different use-case for backups. – Mike Bell May 15 '21 at 17:58
  • I can also comment to say that Clonezilla is broken and not useful as it clones empty space. – Andrew Hardy Aug 03 '21 at 16:20
  • 1
    I've been using Clonezilla for some years. Works well for me. – u628898 Dec 05 '21 at 05:53
  • 1
    Clonezilla does not clone empty space. It's a great tool with the only downside being that you have to boot into it. – Organic Marble Mar 11 '22 at 12:51
  • Also called a disk snapshot or a system image. – Clockwork Apr 04 '23 at 08:59
22

Here is a solution I use with SquashFS. It is quite similar to TAR.GZ solution proposed earlier, but has some major benefits.

SquashFS is a compressed file system, which is completely stored in one file. This file can be mounted to an existing system and accessed in a usual way, like any other partition. The difference to TAR.GZ is that SquashFS is a full-blown file system with random access to files, while TAR is just one big concatenated file.

This means that if you want to mount some large backup of your whole file system, for TAR.GZ it would take like 5 hours (in my experience) and for SquashFS it would take just minutes/seconds. The same is true also for the compression/backup operation, SquashFS is many times faster.

UPDATE 2017-01-31: It appears that not only can you mount squashfs file, but also open it as a usual archive with familiar apps like File Roller on Linux and 7-Zip on Windows, etc.

So here is a command I use to back up my root folder:

sudo mksquashfs / /path/to/backup/hdd/root-backup.sqsh -e home media dev run mnt proc sys tmp

where "-e" switch excludes folders you want to exclude (like virtual and external Linux folders in my example).

After the backup is done, I can now mount it:

sudo mkdir /mnt/root_backup
sudo mount /path/to/backup/hdd/root-backup.sqsh /mnt/root_backup -t squashfs -o loop

Now just wait couple minutes (depending on size of the archive) and enjoy all your files at /mnt/root_backup folder.

Same can be done for /home/myname folder, e.g.

sudo mksquashfs /home/myname /path/to/backup/hdd/home-backup.sqsh -e Dropbox GoogleDrive

I exclude Dropbox and GoogleDrive here to avoid any potential problems in the future, in case I restore those folders from backup and they become messing with the actual files in the cloud.

Check more info at http://tldp.org/HOWTO/SquashFS-HOWTO/creatingandusing.html

afrish
  • 510
  • 3
    Excellent answer. I used this to create my own script that lives on my NAS drive which is connected via NFS. Answered separately below since it has code. – robmsmt May 18 '20 at 00:26
  • 1
    Note that SquashFS does not support ACLs (see the section titled "Xattr Table" here for an explanation). Attempting to run mksquashfs on directories that have ACLs defined results in an Unrecognised xattr prefix system.posix_acl_access error. More details here and a possible patch here. – Jim Walker Nov 22 '20 at 20:20
  • It might be obvious but the sample command specifically excludes the home folder, which isn't what the OP wanted. But it's a great solution, nonetheless! – bitsmack Jun 13 '22 at 16:19
13

We can also do backup of system with rsync & exclude files & folders that we don't want. Here is the following command to do this :

#rsync -aAXv --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"} /* /path/to/backup/folder

Using the -aAX set of options, the files are transferred in archive mode, ensuring that symbolic links, devices, permissions and ownerships, modification times, ACLs and extended attributes are preserved

The --exclude option will cause files that match the given patterns to be excluded.

REFERENCE : Full system backup with rsync

d a i s y
  • 5,511
8

You can use Remastersys to create a bootable live CD/DVD image. This will install like a normal Ubuntu CD.

To install Remastersys, you first need to add a repository:

deb http://www.geekconnection.org/remastersys/repository karmic/

You can then install it from the Software Centre as usual.

Once installed, use it to make a 'dist' backup. This means that user data will be excluded from the ISO image.

remastersys UI

This is often used to create custom distributions but is still useful for the backup task you have in mind. One caveat is that it may fail if the contents of / (minus user data in /home) takes up too much memory because the ISO file format can only hold ~4GB data. Remastersys uses a file system called squashfs to compress your data so you should be OK with up to ~8GB.

dv3500ea
  • 37,204
  • 1
    I tried this but last time it got too big so taring / excluding the directories im not interested in is better for me, then when i backup i dont have to wait for the CD as my HD will be quicker thanks for the advice though – Will Oct 16 '10 at 22:47
6

Try Remastersys.
With that program you can do liveCD (just the same as Ubuntu installation iso file) of your system with ability to install it on hard drive. Works pretty simple (if you know how to burn iso to USB/CD/DVD).
Works perfectly on my 11.10 and 11.04, and older ones as well.

Installation:
edit file /etc/apt/sources.list
add: # Remastersys
deb http://www.geekconnection.org/remastersys/repository karmic/ and save
Then run in terminal:
sudo apt-get update
sudo apt-get install remastersys

foxy
  • 613
5

I turned afrish's excellent answer into a script for myself. Thought it could be useful for those wishing to automate it. The nice thing is that since the script can live on the NAS it will only execute if it's connected.

The script (can be copied to any location - preferably the NAS itself) and will build the backups in the same folder.

Backups Script

make_backups_root_and_home.sh

#!/usr/bin/env bash

set -e

if [ "$EUID" -ne 0 ]
  then echo "Please run as root"
  exit
fi

log="log.txt"
power=$(cat /sys/class/power_supply/AC/online)
start_date=$(date +%Y%m%d_%H%M%S)

if (( $power == 1 )); then
    echo "Starting backup $log at $start_date" >> $log

    # ROOT IMAGE
    root_backup_file=$start_date-root-backup.sqsh
    sudo mksquashfs / $root_backup_file -e home media dev run mnt proc sys tmp nfs

    # ALL HOMES IMAGE
    home_backup_file=$start_date-home-backup.sqsh
    sudo mksquashfs /home $home_backup_file -e Dropbox GoogleDrive

    finish_date=$(date +%Y%m%d_%H%M%S)
    echo "Completed $root_backup_file and $home_backup_file at $finish_date" >> $log
else
    echo "Power is not plugged in... skipped on $start_date" >> $log
fi

Don't forget to chmod +x make_backups_root_and_home.sh!


Linux Cronjob

To enable the cronjob:

sudo su
crontab -e

For Sunday at 1am, append:

00 01 * * sun  /nas_path/to/your/make_backups_root_and_home.sh >> /nas_path/to/your/log.txt 2>&1

Testing Backups!

As with all good backups, they should be tested! Finally, we can create a script to mount the last available backup (since they are ordered by date).

mount_last_backup.sh

#!/usr/bin/env bash

sudo mkdir -p /mnt/root_backup
sudo mkdir -p /mnt/root_home

#UMOUNT ANY
sudo umount /mnt/root_backup
sudo umount /mnt/home_backup

last_root=$(ls -Xrt -1 *root-backup* | head -n 1)
last_home=$(ls -Xrt -1 *home-backup* | head -n 1)

sudo mount $last_root /mnt/root_backup -t squashfs -o loop
sudo mount $last_home /mnt/home_backup -t squashfs -o loop
robmsmt
  • 421
5

TimeShift.

TimeShift protects your system by taking incremental snapshots of the file system at regular intervals. These snapshots can be restored later to bring your system to the exact state it was in at the time when the snapshot was taken.

Open the terminal and run the following command

sudo apt-add-repository -y ppa:teejee2008/ppa
sudo apt-get update
sudo apt-get install timeshift

Screenshots

enter image description here

enter image description here

m3asmi
  • 336
  • 3
  • 7
2

Here is a good tutorial using PartImage.

Isaiah
  • 59,344
2

If you hard-disk where Ubuntu is installed isn't too big, you can try booting from a live cd (any linux distro will do) and running:

    dd if=/dev/sda of=/path/to/external/hardisk/mybackupfile

This makes a backup of your whole hard-disk, not just Ubuntu, and it'll also do all the empty bytes, but it's the only solution I can think of. To restore the backup, use:

  dd if=/path/to/external/harddisk/mybackupfile of=/dev/sda
Hippo
  • 517
  • so how would I restore my Ubuntu distro from a copy om my whole HDD? Because you can't do that... – Alvar Nov 19 '11 at 13:54
  • @Alvar by storing the backup on an external/cloud drive and running it from a live CD/DVD/USB image. – MacroMan Mar 05 '18 at 11:16
1

You can use tools like Bacula Community or Bacula Enterprise. The difference between them is that community version doesn't support bare metal restores when you don't need to re-install the system and just purely recover it after the major crash.

1

You can backup your entire Ubuntu installation to another partition with this answer:

Advantages of this technique:

  • You can continue using the system while it is being backed up. Care must be taken that you are only using web browser apps, office apps, watching videos, etc. and not database apps like accounting or SQL.
  • Virtual file systems are not backed up. They are recreated on each boot up so they are useless to backup in the first place.
  • The first backup may take an hour but daily backups do not copy files that haven't changed so they may only take a few minutes.
  • You can reboot and use the backup. This is beneficial if you want to try something dangerous that might break the system such as a Ubuntu Version upgrade untested in your environment.
  • If you totally break your production environment you can reboot into your clone and copy it back over using the same script.
  • Grub is automatically updated with proper UUID entries.
  • /etc/fstab is automatically updated with proper UUID entries.
1

Systemback – Restore Ubuntu Desktop and Server To Previous State (Free tool)

"Systemback is a free utility that can be used to backup and restore Ubuntu-like systems. It is somewhat similar to MS Windows’s “System restore” option. Using Systemback, we can easily create backup of a Ubuntu desktop or server system, and restore them to the previous state whenever we want. It creates one or more restore points date and time-wise, and displays the most recent restore points at the top. Not only restore points, we can also create the complete backup of your live system including user configuration files and data as ISO, and save them in an external hard disk."

  • Backup the system with or without user configuration files and data
  • Restore system to any previous working state
  • Copy files/folders from one partition to another
  • Create the live Ubuntu system as ISO, and boot your system using it in-case of any problems
  • Copy files/folders from one partition to another
  • Create the live Ubuntu system as ISO, and boot your system using it in-case of any problems
  • Upgrade our system to next available version etc.

Link to website describing the install procedure.

1

go to ubuntu software center find Déjà Dup backup manger install it`` run it and give path where to backup your data select files to be backup make a back up

later on you can chose backup by giving location of your external hard drive gud luck source [https://launchpad.net/deja-dup]

paru38
  • 684
  • I want to backup my entire system not just my music or videos. – Alvar Nov 19 '11 at 12:27
  • @Alvar you can select the "/" or "File System" and can back that up (that is the ENTIRE SYSTEM). – jaorizabal Mar 23 '12 at 22:27
  • how do I install it? how do I backup my system with it? – Alvar Mar 23 '12 at 22:32
  • It does seem to be reliable at all. Just read some user reviews here https://apps.ubuntu.com/cat/applications/deja-dup/ – ILIV Nov 04 '16 at 09:04
  • @ILIV well yeah, the review sucks. That's good to know. Guess it's could be used for single files and music stuff maybe. – M J Jul 10 '17 at 21:42
0

This works for me on a Ubuntu 18.04 desktop, and only requires 'Disks' (gnome-disk-utility), gparted and standard grub commands.

Backup

  • Create an 8GB partition on a drive and install Ubuntu (minimal install) - call it utilities
    • Install gparted
  • Within this system ..
    • Run Disks, pick production system partition, and choose Create partition image
    • Save the image to ddMMMYYYY.img on any partition on the computer

Restore

  • Within the utilities system, run 'Disks' and click on the partition to restore to

    • choose Restore partition image, and choose the image
  • Run gparted

    • There might be unallocated space in the new partition
      • Partition -> Check (and repair), regardless
    • The UUID will be the same as the original partition, so if the original partition still exists on the computer, you need ..
      • Partition -> New UUID
    • Take note of the UUID - eg. ba5b7f3a-54d2-4325-80e5-e7a159900d3f
    • Take note of the restored partition - eg. /dev/sda6

fstab update

  • Skip to grub update below if you have not created a new UUID
  • Run Files (ie. nautilus)
    • Click + Other Locations
    • Click on the device/partition - eg./dev/sda6
  • Run ..
      df -hT
      # you will get something like ..
      /dev/sda6 xx xx /media/fred/ba5b7f3a-54d2-4325-80e5-e7a159900d3f/etc/fstab
  • paste in the last entry to produce a command like so ..
  sudo vi /media/fred/ba5b7f3a-54d2-4325-80e5-e7a159900d3f/etc/fstab
  • Change the ''/'' entry in ''fstab'' to use this UUID

grub update

  • Run the following (assuming that your master boot record is on /dev/sda) ..
  # ensure all systems are found
  sudo os-prober
  # generate /boot/grub/grub.cfg
  sudo update-grub
  # search for root=UUID and check that the new UUID is used in some of these
  # if not, try sudo grub-mkconfig, seems to get it right eventually
  less /boot/grub/grub.cfg
  # update the master boot record
  sudo grub-install /dev/sda
  • Reboot and go into the restored system
  • Check that you really are in the restored system - note the ''/'' line
df -hT | grep ext4
/dev/sda6      ext4       29G  6.1G   22G  23% /
/dev/sdb6      ext4      112G   86G   22G  81% /data
/dev/sda4      ext4      1.6T  377G  1.1T  26% /sata

final grub update from the new system

  • Run this from the new system, so that this system becomes the default in the boot menu (assuming that your master boot record is on /dev/sda) ..
  sudo os-prober
  sudo update-grub
  sudo grub-install /dev/sda
Tybion
  • 1
  • 1
0

A side note: Make sure permissions and owernship are preserved when copying or your backed up copy will be unusable (annoyingly, reinstalling is then the fastest way to recover).

A tar file like that used in the accepted answer will preserve permissions and ownership (see tar manpage and this answer). NTFS drives support permissions if mounted with permissions. FAT has no support. For other filesystems, rather than messing around with mounting options and /etc/fstab, you should use tar and rest assured permissions/ownership is preserved.

qwr
  • 2,802
0

I can recommend my solution which was created with the goal of minimizing space requirements: https://github.com/vbence/osbackup

It can restore a bootable system to an empty media as long as you only use the following technologies.

It covers:

  • Partition tables of block devices (through sfdisk).
  • Raid arrays compatible with mdadm.
  • Logical volumes using LVM.
  • Encrypted devices using dm_crypt (the user will be prompted for the new passphrases).
  • File systems, as long as they are ext* or vfat.
  • Grub boot loader.
vbence
  • 111
  • 4
0

Crashplan will back up all your system files and data and allow you to recover it - either on your current PC or allow you to adopt it to another install.

You can back up to their online system, another computer or any attached hard drive.

Mark Rooney
  • 6,289
0

I have found you a detailed and recent how to on using Clonezilla. Clonezilla will let you make an image of your complete system including all hard drive partitions etc and later restore it.

The tutorial is at http://geekyprojects.com/cloning/how-to-use-clonezilla-tutorial/

lpanebr
  • 1,277
-2

I use a program called Back In Time that's similar to Apple's Time Machine.

It's easy to tell it where you want your backups to go and how often to do the backup.

Back In Time is in the Ubuntu repos.

gamerchick02
  • 1,682
  • That doesn't explain how to backup my system and how to restore it at all. When i can't even boot up back in time won't be able to help. If you can suggest how to use backup in time in this way I would be appreciative. Cheers – Will Oct 17 '10 at 09:11
  • Back in time uses is a frontend for rsync. You'll have to have Ubuntu installed to use it. I agree with fluteflute's comment above that it's easier to backup your files. That's what I thought you wanted. – gamerchick02 Oct 17 '10 at 16:21
  • I already backup my files ;) The amount of times i render ubuntu inoperable its easier to either reinstall or revert to a backup than fix the problem (generally i need it fixed as I have work to do in between fiddling and breaking!) – Will Oct 17 '10 at 23:11
  • Right. I tried back in time to setup an automatic backup but i get a whole bunch of errors. Most like this: (its just a small fraction, and im running in root mode) – Will Oct 18 '10 at 15:55
  • [E] Error: rsync: read errors mapping "/sys/module/videodev/sections/.altinstr_replacement": No data available (61) – Will Oct 18 '10 at 15:55
  • 1
    The backup commands noted above would probably work best for you. I only suggested Back in Time as a frontend for the rsync, etc. You can choose what you want to make a backup of, including all of the system files. – gamerchick02 Oct 18 '10 at 23:46
  • Back In Time is just awful, especially the GUI. It routinely crashes for me and doesn't show any progress or status information, so it's unclear when it's actually running. – Cerin Nov 06 '16 at 19:00