Clone or Backup Linux System

From Hack Sphere Labs Wiki
Jump to: navigation, search

Using cp

The cp program can be used to clone a disk, one partition at a time. An advantage to using cp is that the filesystem type of the destination partition(s) may be the same or different than the source. For safety, perform the process from a live environment.

The basic procedure from a live environment will be:

  • Create the new destination partition(s) using fdisk, cfdisk or other tools available in the live environment.
  • Create a filesystem on each of the newly created partitions. Example:
mkfs -t ext3 /dev/sdb1
  • Mount the source and destination partitions. Example:
mount -t ext3 /dev/sda1 /mnt/source
mount -t ext3 /dev/sdb1 /mnt/destination
  • Copy the files from the source partition to the destination
cp -a /mnt/source/* /mnt/destination

-a: preserve all attributes , never follow symbolic links and copy recursively

  • Change the mount points of the newly cloned partitions in /etc/fstab accordingly
  • Finally, install the GRUB bootloader if necessary. (See GRUB)

rsync

#!/bin/sh

if [ $# -lt 1 ]; then 
    echo "No destination defined. Usage: $0 destination" >&2
    exit 1
elif [ $# -gt 1 ]; then
    echo "Too many arguments. Usage: $0 destination" >&2
    exit 1
fi

START=$(date +%s)
rsync -aAXv /* $1 --exclude={/dev/*,/proc/*,/sys/*,/tmp/*,/run/*,/mnt/*,/media/*,/lost+found,/var/lib/pacman/sync/*}
FINISH=$(date +%s)
echo "total time: $(( ($FINISH-$START) / 60 )) minutes, $(( ($FINISH-$START) % 60 )) seconds"

touch $1/"Backup from $(date '+%A, %d %B %Y, %T')"
$ chmod +x backup.sh
 /home/user/Scripts/backup.sh /some/destination