Last active
October 9, 2015 18:02
-
-
Save omar-yassin/413d1a361c0ab9834e8c to your computer and use it in GitHub Desktop.
Linux: Burn to optical media
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# SUDOERS /etc/sudoers.d/lbbackup | |
# Cmnd_Alias LS_HARDWARE = /usr/bin/lshw | |
# Cmnd_Alias MOUNT_UDF = /bin/mount -oloop\,rw * | |
# Cmnd_Alias UNMOUNT_UDF = /bin/umount /data/TEMP_UDF_DIR/MOUNT_UDF | |
# Cmnd_Alias BURN_UDF = /usr/bin/growisofs | |
# Cmnd_Alias CHOWN_MOUNT = /bin/chown -R lbbackup\:lbbackup /data/TEMP_UDF_DIR/MOUNT_UDF | |
# lbbackup ALL=(ALL) NOPASSWD: LS_HARDWARE, MOUNT_UDF, UNMOUNT_UDF, BURN_UDF, CHOWN_MOUNT | |
backup_date=`date +"%Y%m%d.%H%M%S"` | |
LOG_FILE=/var/log/backup/backup_burn_remote_copy.${backup_date}.log | |
CATALOG_FILE=/var/log/backup/backup_burn_remote_copy.${backup_date}.catalog | |
LOOP_DEVICE=/dev/sr0 | |
UDF_FILE=/data/TEMP_UDF_DIR/backup_${backup_date}.udf | |
UDF_MOUNT=/data/TEMP_UDF_DIR/MOUNT_UDF | |
S3_BACKUP_DIR=/data/backups/s3/ | |
# prints stdout and logs to file | |
function logger () | |
{ | |
echo "`date +"%Y%m%d.%H%M%S"` ${1}" | tee -a $LOG_FILE | |
} | |
# function to wrap errors and exit | |
rc=0 | |
function check_return_value | |
{ | |
if [ $rc -eq 0 ] ; then | |
logger " * [INFO] Success! Return Value: $rc" | |
else | |
logger " * [ERROR] Operation FAILED!!! Please check the logs!" | |
exit 55 | |
fi | |
} | |
function notify_error_exit | |
{ | |
# coming soon - email alerts | |
echo "ERROR: ${1}" | tee -a $LOG_FILE | |
exit 99 | |
} | |
function notify_success_email | |
{ | |
echo "coming soon" | |
} | |
function check_data_mount | |
{ | |
# check if /data mount is mounted | |
if [[ `/bin/mount -v | grep "/dev/mapper/data on /data type ext4 (rw)"` ]] ; then | |
logger " * [INFO] /data mount is mounted" | |
else | |
notify_error_exit "DATA Mount /data NOT mounted" | |
fi | |
} | |
function check_loop_device | |
{ | |
if [[ `sudo lshw | awk '/\*-cd/,/con/' | sed -e 's/^[ \t]*//' | grep "status=ready"` ]] ; then | |
logger " * [INFO] LOOP Device $LOOP_DEVICE is READY (media inside the drive)" | |
else | |
notify_error_exit "Loop device (Bluray Drive) is not in Ready state. Check if blank media is inside the drive." | |
fi | |
} | |
function copy_latest_backups | |
{ | |
# Make catalog of backups from 0-7 days ago | |
# DB FILES | |
for i in `seq 0 7`; do | |
day_i=`date --date="$i days ago" +"%Y%m%d"` | |
/usr/bin/find ${S3_BACKUP_DIR} -name *${day_i}*.bak -type f -print0 | du -hc --files0-from=- | grep -v total >> ${CATALOG_FILE} | |
done | |
# S3 FILES | |
# COMING SOON | |
logger " ** [FILES TO BACKUP UP] **" | |
cat ${CATALOG_FILE} | sort -k2 | tee -a $LOG_FILE | |
/bin/cp -v ${CATALOG_FILE} ${UDF_MOUNT} | tee -a $LOG_FILE | |
/usr/bin/awk '{print $2}' ${CATALOG_FILE} | /usr/bin/xargs -I {} /bin/cp -v --parents {} ${UDF_MOUNT} | tee -a $LOG_FILE | |
rc=${PIPESTATUS[0]} ; check_return_value | |
} | |
function udf_mount | |
{ | |
# check if /data mount is mounted | |
if [[ `/bin/mount -v | grep "/data/TEMP_UDF_DIR/MOUNT_UDF"` ]] ; then | |
notify_error_exit "Temp UDF device already mounted. Please check why that is the case. There should be no UDF loop devices mounted before we attempt to mount a new UDF loop device :)" | |
fi | |
# create UDF file | |
/usr/bin/truncate --size=25GB $UDF_FILE | |
/usr/bin/mkudffs $UDF_FILE | |
# mount UDF file so we can copy things to to it | |
sudo /bin/mount -oloop,rw $UDF_FILE $UDF_MOUNT | |
} | |
function udf_burn | |
{ | |
sudo /bin/umount $UDF_MOUNT | tee -a $LOG_FILE | |
rc=${PIPESTATUS[0]} ; check_return_value | |
sudo /usr/bin/growisofs -speed=4 -Z $LOOP_DEVICE=${UDF_FILE} | tee -a $LOG_FILE | |
} | |
function verify_burn | |
{ | |
# function to check hash of each file | |
echo "" | |
# after verification delete temp UDF file | |
} | |
logger " * [STEP 1 of 6] Check if LOOP Device $LOOP_DEVICE is ready (does the bluray drive have media inside it?)" | |
check_loop_device | |
logger "" ; logger " * [STEP 2 of 6] Check if /data parittion is mounted" | |
check_data_mount | |
logger "" ; logger " * [STEP 3 of 6] Mount temporary UDF file (so we can copy backups files to it)" | |
udf_mount | |
logger "" ; logger " * [STEP 4 of 6] Copy latest backup files to UDF_MOUNT (searching for backups 0-7 days ago)" | |
copy_latest_backups | |
logger "" ; logger " * [STEP 5 of 6] Burn BACKUP!!" | |
udf_burn | |
#verify_burn |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment