Home

Wiki (private)

 Kornel 

 Blog Kornela 

Linux

Drugs

Berlin

Outside

Free Time

Contact

Impressum


Backup and Restore with cpio
Motivation:   linux tar cannot copy every file (does not like fifos for example)
Solution:       find and cpio

Nice: cpio is
  • really fast
  • reads every file
  • features crc-checks

Doing the backup

Backup one filesystem with the archive on the same filesystem:
Name of the archive (example): backup_crc.cpio
   find . -depth -mount ! -name backup_crc.cpio | cpio --create --format=crc \
      --force-local --verbose > 'backup_crc.cpio'
        
To exclude a whole directory, e.g. /tmp, substitute the above find by
   find . -mount -path './tmp' -prune -o -print
        
(prune does not work with -depth)

If your filesystem does not support files bigger than 2 GB, you could use split:
   find . -depth -mount ! -name backup_crc.cpio | cpio --create --format=crc \
      --force-local --verbose | split -b1024m - 'backup_crc.cpio'
        

Doing the restore

Go to the destination directory
   cat 'backup_crc.cpio' | cpio --extract --preserve-modification-time \
      --make-directories --format=crc --force-local --verbose
        
In the case you splitted the archive, just do
   cat 'backup_crc.cpio'.* | cpio --extract --preserve-modification-time \
      --make-directories --format=crc --force-local --verbose
        

Testing the archive

List contents:
   cat 'backup_crc.cpio' | cpio -t
        
CRC check (does not show emtpy files, links, etc.):
   cat 'backup_crc.cpio' | cpio --extract --only-verify-crc --verbose
        
Some infos about cpio (in german) on Linux-Magazin
Backup and Restore with afio
Another variant to do the above is to use the tool "afio".
You can get it via Freshmeat.
Advantages of afio are
  • does not stop at corrupted input files
  • can compress every file in archive seperately
  • can do multivolumes with prompt for "tape change"
It works similar to cpio. Use the find command to get a list of files and pipe the output to afio.

A backup with compression would look like (not tested yet!)
   cd /;
   find . -mount -path './tmp' -prune -o -print |
        afio -o -v -Z -P bzip2 - |
        split -b1024m - /tmp/backup.afio
        
Do a restore by
        cat /tmp/backup.afio.* | afio -i -v -Z -P bzip2 -
        
Incremental Backups - find files newer than a backup archive
        find . -mount -path './tmp' -prune -o -newer /tmp/backup.afio -print | ...
        
Alternative to split (directly)
        find . -path './tmp' -prune -o -print | 
             afio -v -Z -P bzip2 -o /tmp/hostname.backup.`date +%F`.afio 
             > /tmp/hostname.backup.`date +%F`.log 2>&1
        
Hint - do every backup as root!
Hint - do not overwrite the system you are working with, afio will stop after bzip2 is overridden, use Knoppix