Skip to content

Instantly share code, notes, and snippets.

@nathandarnell
Created May 3, 2014 03:08
Show Gist options
  • Save nathandarnell/ff49504cdd3ebf2b6185 to your computer and use it in GitHub Desktop.
Save nathandarnell/ff49504cdd3ebf2b6185 to your computer and use it in GitHub Desktop.
#!/bin/bash
## Author: RonB123123
## Created: Sept 24, 2008
## Description:
## Checks if the MySQL + Apache services are running, if not
## not then start each service.
##From http://ubuntuforums.org/showthread.php?t=928475
echo "Options"
echo "1 - Start"
echo "2 - Stop"
read option
SERVICES="avahi-daemon deluge-daemon cron"
# check if each service is running
for service in $SERVICES
do
if [ "$option" == 2 ]
then
echo "Stopping $service..."
sudo /etc/init.d/$service stop
elif [ "$option" == 1 ]
then
echo "Starting $service..."
sudo /etc/init.d/$service start
fi
if (ps ax | grep -v grep | grep $service > /dev/null)
then
echo "$service is running."
else
echo "$service is not running."
fi
done
# Setting up backup directories
SUBDIR=RaspberryPi2_backups
DIR=/media/1TB/$SUBDIR
# Setting up echo fonts
red='\e[0;31m'
green='\e[0;32m'
cyan='\e[0;36m'
yellow='\e[1;33m'
purple='\e[0;35m'
NC='\e[0m' #No Color
bold=`tput bold`
normal=`tput sgr0`
#Screen clear
clear
echo -e "${green}${bold}Starting RaspberryPi backup process!${NC}${normal}"
echo ""
# First check if pv package is installed, if not, install it first
PACKAGESTATUS=`dpkg -s pv | grep Status`;
if [[ $PACKAGESTATUS == S* ]]
then
echo -e "${cyan}${bold}Package 'pv' is installed${NC}${normal}"
echo ""
else
echo -e "${yellow}${bold}Package 'pv' is NOT installed${NC}${normal}"
echo -e "${yellow}${bold}Installing package 'pv' + 'pv dialog'. Please wait...${NC}${normal}"
echo ""
sudo apt-get -y install pv && sudo apt-get -y install pv dialog
fi
# Check if backup directory exists
if [ ! -d "$DIR" ];
then
echo -e "${yellow}${bold}Backup directory $DIR doesn't exist, creating it now!${NC}${normal}"
sudo mkdir $DIR
fi
# Create a filename with datestamp for our current backup (without .img suffix)
OFILE="$DIR/backup_$(date +%Y%m%d_%H%M%S)"
# Create final filename, with suffix
OFILEFINAL=$OFILE.img
# First sync disks
sync; sync
# Shut down some services before starting backup process
echo ""
echo -e "${purple}${bold}Stopping services before backup${NC}${normal}"
sudo pkill deluged
sudo service deluge-daemon stop
sudo service cron stop
sudo avahi-daemon stop
# Begin the backup process, should take about 45 minutes hour from 8Gb SD card to HDD
echo ""
echo -e "${green}${bold}Backing up SD card to img file on HDD${NC}${normal}"
SDSIZE=`sudo blockdev --getsize64 /dev/mmcblk0`;
sudo pv -tpreb /dev/mmcblk0 -s $SDSIZE | dd of=$OFILE bs=1M conv=sync,noerror iflag=fullblock
# Wait for DD to finish and catch result
RESULT=$?
# Start services again that where shutdown before backup process
echo ""
echo -e "${purple}${bold}Starting the stopped services${NC}${normal}"
sudo service deluge-daemon start
sudo deluged
sudo service cron start
sudo avahi-daemon start
# If command has completed successfully, if not, delete created files
if [ $RESULT = 0 ];
then
sudo mv $OFILE $OFILEFINAL
echo ""
echo -e "${green}${bold}RaspberryPI backup process completed! FILE: $OFILEFINAL${NC}${normal}"
echo -e "${yellow}Removing backups older than 5 days${NC}"
sudo find $DIR -maxdepth 1 -name "*.img" -mtime +5 -exec rm {} \;
echo -e "${cyan}If any backups older than 5 days were found, they were deleted${NC}"
exit 0
# Else remove attempted backup file
else
echo ""
echo -e "${red}${bold}Backup failed!${NC}${normal}"
sudo rm -f $OFILE
echo ""
echo -e "${purple}Last backups on HDD:${NC}"
sudo find $DIR -maxdepth 1 -name "*.img" -exec ls {} \;
echo ""
echo -e "${red}${bold}RaspberryPI backup process failed!${NC}${normal}"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment