Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save geoffreyanderson/1004950 to your computer and use it in GitHub Desktop.
Save geoffreyanderson/1004950 to your computer and use it in GitHub Desktop.
A script to rsync a running Linux OS to an image file (specifically, a 10GB image file for deployment to AWS EC2).
#!/bin/bash
# Run this script on a running Linux OS that you want to be put into an image file.
# Ensure that the system you run this on is less than 10GB in size if you wish to
# deploy the image file to AWS EC2.
# Note: This is based on Michael Fairchild's instance-to-ebs-ami.sh script.
# -https://gist.github.com/249915
imageFile=${1:-"awsImage-$(date +%m%d%y-%H%M).img"}
imageMountPoint=${2:-'/mnt/image'}
extraFilesArchive=${3:-'awsInstanceFiles.tar.gz'}
extraScript=${4:-'awsInstanceChanges.sh'}
echo "Creating empty 10GB image in ${imageFile}"
# Create an empty 10GB image file
dd if=/dev/zero of=${imageFile} bs=1M count=10240
echo "Creating filesystem in ${imageFile}"
# Create a filesystem on the image file
/sbin/mke2fs -F -j ${imageFile}
echo "Mounting ${imageFile} loopback at ${imageMountPoint}"
# Create the directories needed for imaging
mkdir -p ${imageMountPoint}
mount -o loop ${imageFile} ${imageMountPoint}
echo "Beginning rsync..."
rsync --stats -av --exclude=/root/.bash_history --exclude=/home/*/.bash_history --exclude=/etc/ssh/ssh_host_* --exclude=/etc/ssh/moduli --exclude=/etc/udev/rules.d/*persistent-net.rules --exclude=/mnt/* --exclude=/proc/* --exclude=/tmp/* --exclude=/sys/* --exclude=/dev/* / ${imageMountPoint}
echo "rsync finished. Flushing copied log data..."
#clear out any remaining log data
cd ${imageMountPoint}/var/log
for i in `ls ./**/*`; do
echo $i && echo -n> $i
done
if [[ -r "${extraFilesArchive}" ]]; then
#tar -pPxzvf ${extraFilesArchive}
echo "Extracting extra files in ${extraFilesArchive} to ${imageMountPoint}"
tar -pxzvf ${extraFilesArchive} -C ${imageMountPoint}
echo "Finished extracting extra files in ${extraFilesArchive} to ${imageMountPoint}"
fi
if [[ -x "${extraScript}" ]]; then
echo "Preparing to run ${extraScript} under chroot in ${imageMountPoint}"
# Strip the leading path from the file passed in.
scriptFileName=$(echo ${extraScript} | awk -F/ '{ print $NF }')
cp ${extraScript} ${imageMountPoint}
# Mount proc so we can chroot
mount -t proc none ${imageMountPoint}/proc
# chroot into the mount point for the image and run the bash script
# with additional changes.
chroot ${imageMountPoint} /bin/bash /${scriptFileName}
# Unmount /proc from the image mount point and remove the copied script.
umount ${imageMountPoint}/proc
rm -f ${imageMountPoint}/${scriptFileName}
echo "Finished running ${extraScript} under chroot in ${imageMountPoint}"
fi
# Create base devices (console, null, zero) under the completed image
for i in console null zero ; do MAKEDEV -d ${imageMountPoint}/dev -x $i ; done
# get out of the image mount point so we can successfully unmount
cd /
# Sync changes and unmount the image
sync
umount ${imageMountPoint}
rmdir ${imageMountPoint}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment