Tuesday, April 17, 2012

Clone Arch root

This script will make a clone of my root directory onto a mounted partition. It is far from perfect!
It requires my root /etc/fstab plus a "clone-version" /etc/fstabClone (see below)
It requires folders /mnt/ssdRootClone on the original and /mnt/ssdRoot on the Clone.
I run it as root.

##############################################################################

#!/bin/bash
# fileinfo.sh
LOG="/var/log/cloneArch.log"
date=`date "+%a %b %e %H:%M:%S %Y"`
DESTINATION="/mnt/ssdRootClone"
#DESTINATION="/home/david/Clone"
FSTAB="/etc/fstab"
FSTABCLONE="/etc/fstabClone"


FILES="/bin
/boot
/dev
/etc
/lib
/lib64
/opt
/root
/run
/sbin
/srv
/tmp
/usr
/var"


FOLDERTOREMOVE="$DESTINATION/boot/grub/grub"


echo "$date : " >>$LOG
echo "$date : System is being cloned." >>$LOG
echo "$date : Clone mounted at $DESTINATION" >>$LOG
echo "$date : " >>$LOG


# Clear the variable directpries from the destination first
#rm -rf $DESTINATION/tmp
#rm -rf $DESTINATION/var


for file in $FILES
do
  
  if [ ! -e "$file" ]       # Check if file exists.
  then
    echo "$date : $file does not exist and has not been cloned." >>$LOG
  else


    # Clone the system here
    echo "$date : $file is next." >>$LOG
    # rsync options used
    #
    # -a, --archive
    # This is equivalent to -rlptgoD. It is a quick way of saying  you
    # want  recursion  and want to preserve almost everything (with -H
    # being a notable omission).  The  only  exception  to  the  above
    # equivalence  is when --files-from is specified, in which case -r
    # is not implied.
    #
    # Note that -a does not preserve hardlinks, because finding multiply
    # linked  files is expensive.  You must separately specify -H.
    #
    # -u, --update
    # This  forces rsync to skip any files which exist on the destination 
    # and have a modified time that  is  newer  than  the  source file.
    # (If an existing destination file has a modification time equal to
    # the source file's, it will be updated if the sizes  are different.)
    #
    # Note  that this does not affect the copying of symlinks or other
    # special files.  Also, a difference of file  format  between  the
    # sender  and receiver is always considered to be important enough
    # for an update, no matter what date is on the objects.  In  other
    # words, if the source has a directory where the destination has a
    # file, the transfer would occur regardless of the timestamps.
    #
    # This option is a transfer rule, not an exclude,  so  it  doesn't
    # affect  the  data  that  goes  into  the file-lists, and thus it
    # doesn't affect deletions.  It just limits  the  files  that  the
    # receiver requests to be transferred.
    #
    # -H, --hard-links
    # This tells rsync to look for hard-linked files in the source and
    # link together the corresponding files on the destination.  With-
    # out  this option, hard-linked files in the source are treated as
    # though they were separate files.
    #
    # This option does NOT necessarily ensure that the pattern of hard
    # links  on  the  destination  exactly matches that on the source.
    # Cases in which the destination may end up with extra hard  links
    # include the following:
    #
    #
    # o      If  the  destination contains extraneous hard-links (more
    #        linking than what is present in the  source  file  list),
    #        the  copying  algorithm  will  not break them explicitly.
    #        However, if one or more of the paths have content differ-
    #        ences,  the  normal  file-update process will break those
    #        extra links (unless you are using the --inplace  option).
    #
    # o      If you specify a --link-dest directory that contains hard
    #        links, the linking of the destination files  against  the
    #        --link-dest files can cause some paths in the destination
    #        to become linked together due to the --link-dest associa-
    #        tions.
    #
    #
    # Note  that  rsync  can only detect hard links between files that
    # are inside the transfer set.  If rsync updates a file  that  has
    # extra  hard-link connections to files outside the transfer, that
    # linkage will be broken.  If you are tempted to use the --inplace
    # option to avoid this breakage, be very careful that you know how
    # your files are being updated so that you  are  certain  that  no
    # unintended  changes  happen due to lingering hard links (and see
    # the --inplace option for more caveats).
    #
    # If incremental recursion is active (see --recursive), rsync  may
    # transfer a missing hard-linked file before it finds that another
    # link for that contents exists elsewhere in the hierarchy.   This
    # does  not  affect the accuracy of the transfer (i.e. which files
    # are hard-linked together), just its efficiency (i.e. copying the
    # data for a new, early copy of a hard-linked file that could have
    # been found later in  the  transfer  in  another  member  of  the
    # hard-linked  set  of files).  One way to avoid this inefficiency
    # is to disable incremental recursion using the --no-inc-recursive
    # option.
    rsync -aqHu --delete --stats --log-file=$LOG --log-file-format="%n" $file $DESTINATION
    # make use of "$ du" command
    # du -s $file | awk '{ print $2 "is " $1 }'
    echo "$date : $file successfully cloned." >>$LOG
  fi
done  


# After cloning, fstab is going to mount the wrong partition
# Use the fstabClone for the corrected mounts.
if [ ! -e "$FSTABCLONE" ]       # Check if file exists.
then
  echo "$date : $FSTABCLONE does not exist and has not been copied." >>$LOG
else
  rm $DESTINATION$FSTAB
  cp -Rp $FSTABCLONE "$DESTINATION$FSTAB"
  echo "$date : /etc/fstab corrected for the clone." >>$LOG
fi


# After cloning ac opy of grub needs to be removed
rm -rf $FOLDERTOREMOVE
echo "$date : grub has been cleaned up for the clone." >>$LOG
echo "$date : " >>$LOG


echo "$date : System cloning is complete." >>$LOG
echo "$date : " >>$LOG


exit 0


##############################################################################


Here are the /etc/fstab and /etc/fstabClone files, respectively.


##############################################################################


# /etc/fstab: static file system information
#
# <file system> <dir> <type> <options> <dump> <pass>
tmpfs /tmp tmpfs nodev,nosuid 0 0
#
LABEL=htpcHome /home ext4 defaults 0 1
LABEL=ssdRoot / ext4 defaults 0 1
LABEL=ssdRootClone /mnt/ssdRootClone ext4 defaults 0 1
LABEL=slackRoot /mnt/slackware ext4 defaults 0 1
LABEL=grubPartition /boot/grub ext4 defaults 0 1
#LABEL=ssdSwap swap swap defaults 0 0
/dev/sda2 swap swap defaults 0 0
LABEL=mythGlenelg /home/david/MythTV/Glenelg xfs defaults,noatime,nodiratime,nosuid,nodev,allocsize=64m 0 1
LABEL=mythSturt /home/david/MythTV/Sturt xfs defaults,noatime,nodiratime,nosuid,nodev,allocsize=64m 0 1
# NFS client
#david-desktop:/ /mnt nfs4 rw,hard,intr,bg 0 0
#david-desktop:/music /home/david/Music nfs4 rw,hard,intr,bg 0 0
#david-desktop:/movies /home/david/Movies nfs4 rw,hard,intr,bg 0 0
# Samba
//david-desktop/movies /home/david/Movies cifs noatime,nodiratime,bg,username=david,password=david 0 0
//david-desktop/music /home/david/Music cifs noatime,nodiratime,bg,username=david,password=david 0 0
//david-desktop/tvseries        /home/david/TVseries    cifs    noatime,nodiratime,bg,username=david,password=david        0 0
##############################################################################

and 

##############################################################################

# /etc/fstab: static file system information
#
# <file system> <dir> <type> <options> <dump> <pass>
tmpfs /tmp tmpfs nodev,nosuid 0 0
#
LABEL=htpcHome /home ext4 defaults 0 1
LABEL=ssdRoot /mnt/ssdRoot ext4 defaults 0 1
LABEL=ssdRootClone / ext4 defaults 0 1
LABEL=slackRoot         /mnt/slackware  ext4    defaults        0 1
LABEL=grubPartition /boot/grub ext4 defaults 0 1
#LABEL=ssdSwap swap swap defaults 0 0
/dev/sda2 swap swap defaults 0 0
LABEL=mythGlenelg /home/david/MythTV/Glenelg xfs defaults 0 1
LABEL=mythSturt /home/david/MythTV/Sturt xfs defaults 0 1
# NFS client
#david-desktop:/ /mnt nfs4 rw,hard,intr,bg 0 0
#david-desktop:/music /home/david/Music nfs4 rw,hard,intr,bg 0 0
#david-desktop:/movies /home/david/Movies nfs4 rw,hard,intr,bg 0 0
# Samba
//david-desktop/movies /home/david/Movies cifs noatime,bg,username=david,password=david 0 0
//david-desktop/music /home/david/Music cifs noatime,bg,username=david,password=david 0 0
//david-desktop/tvseries        /home/david/TVseries    cifs    noatime,bg,username=david,password=david        0 0
##############################################################################


No comments:

Post a Comment