Bash Scripts.

I run a web server that contains multiple websites and multiple SQL databases. I decided to use bash scripts to manage my backup’s and for any file restores that were needed. I know the argument will be, “Why bash scripts? there are software options that do the same thing”. The answer is, I couldn’t find one that fit my specific needs and so I decided to write something that fit my needs.

I read several forum threads of people trying to accomplish parts of what I wanted, but nobody had meshed everything together. If you are looking for a bash script that does a grandfather-father-son archive rotation of each user directory into it’s own archive and also does the same thing for databases, then these may work for you. The archives are stored in Daily, Weekly and Monthly. The rotation will do a daily backup Sunday through Friday. On Saturday a weekly backup is done giving you four weekly backups a month. The monthly backup is done on the first of the month rotating two monthly backups based on if the month is odd or even.

I hope you find these as useful as I did.

So this first script I found on the Ubuntu server Archive page.

This one archives the specified files/directories

#!/bin/bash
####################################
#
# Backup to NFS mount script with
# grandfather-father-son rotation
# of specified directory
#
####################################

# What to backup.
backup_files=”/etc /root”

# Where to backup to.
dest=”/backup/system”

# Setup variables for the archive filename.
day=$(date +%A)
hostname=$(hostname -s)

# Find which week of the month 1-4 it is.
day_num=$(date +%d)
if (( $day_num week_file=”$hostname-week1.tgz”
elif (( $day_num > 7 && $day_num week_file=”$hostname-week2.tgz”
elif (( $day_num > 14 && $day_num week_file=”$hostname-week3.tgz”
elif (( $day_num > 21 && $day_num < 32 )); then
week_file=”$hostname-week4.tgz”
fi

# Find if the Month is odd or even.
month_num=$(date +%m)
month=$(expr $month_num % 2)
if [ $month -eq 0 ]; then
month_file=”$hostname-month2.tgz”
else
month_file=”$hostname-month1.tgz”
fi

# Create archive filename.
if [ $day_num == 1 ]; then
archive_file=$month_file
elif [ $day != “Saturday” ]; then
archive_file=”$hostname-$day.tgz”
else
archive_file=$week_file
fi

# Print start status message.
echo “Backing up $backup_files to $dest/$archive_file”
date
echo

# Backup the files using tar.
tar czf $dest/$archive_file $backup_files

# Print end status message.
echo
echo “Backup finished”
date

# Long listing of files in $dest to check file sizes.
ls -lh $dest/

 

This one keeps the same concept except that instead of defining which file/directory the script grabs all the directories within /home

#!/bin/bash
####################################
#
# Backup to NFS mount script with
# grandfather-father-son rotation
# of each home directory within
# it’s own archive
#
####################################

# Where to backup to.
dest=”/backup/users”

# Setup variables for the archive filename.
day=$(date +%A)
#folder=$(backup)

# Find which week of the month 1-4 it is.
day_num=$(date +%d)
if (( $day_num week_file=”-week1.tgz”
elif (( $day_num > 7 && $day_num week_file=”-week2.tgz”
elif (( $day_num > 14 && $day_num week_file=”-week3.tgz”
elif (( $day_num > 21 && $day_num < 32 )); then
week_file=”-week4.tgz”
fi

# Find if the Month is odd or even.
month_num=$(date +%m)
month=$(expr $month_num % 2)
if [ $month -eq 0 ]; then
month_file=”-month2.tgz”
else
month_file=”-month1.tgz”
fi

# Create archive filename.
if [ $day_num == 1 ]; then
archive_file=$month_file
elif [ $day != “Saturday” ]; then
archive_file=”-$day.tgz”
else
archive_file=$week_file
fi

# Print start status message.
echo “Backing up This may take a few minutes.”

# Backup the files using tar.
for folder in $(ls /home); do
sudo -u $folder tar czf “$dest/$folder$archive_file” /home/”$folder”

# Print end status message.
echo
echo “Backup $folder complete.”
#date
done

# Long listing of files in $dest to check file sizes.
ls -lh $dest/
echo
echo “Backup is complete”
exit

 

Now, if you multiple SQL databases on your sever, this will benefit you a lot.

#!/bin/bash
####################################
#
# Backup to NFS mount script with
# grandfather-father-son rotation
# of all your SQL Databases.
#
####################################

# Where to backup to.
dest=”/backup/sql-backup”

# Setup variables for the archive filename.
day=$(date +%A)

# Find which week of the month 1-4 it is.
day_num=$(date +%d)
if (( $day_num week_file=”-week1.sql.tgz”
elif (( $day_num > 7 && $day_num week_file=”-week2.sql.tgz”
elif (( $day_num > 14 && $day_num week_file=”-week3.sql.tgz”
elif (( $day_num > 21 && $day_num < 32 )); then
week_file=”-week4.sql.tgz”
fi

# Find if the Month is odd or even.
month_num=$(date +%m)
month=$(expr $month_num % 2)
if [ $month -eq 0 ]; then
month_file=”-month2.sql.tgz”
else
month_file=”-month1.sql.tgz”
fi

# Create archive filename.
if [ $day_num == 1 ]; then
archive_file=$month_file
elif [ $day != “Saturday” ]; then
archive_file=”-$day.sql.tgz”
else
archive_file=$week_file
fi

# Backup the files.
MYSQL=’/usr/bin/mysql’

MYSQLDUMP=’/usr/bin/mysqldump’
DUMPOPTS=’–opt –hex-blob –skip-extended-insert’

user=”CHANGEME”
pass=”CHANGEME”
# Get the names of the database tables
databases=`$MYSQL -u$user -p$pass –skip-column-names -e’SHOW DATABASES’`

# Write the compressed dump for each table
for db in $databases; do
filename=`date +”$dest/$db$archive_file”`
echo “creating $filename”
$MYSQLDUMP $DUMPOPTS -u$user -p$pass –database $db
| gzip -9 > $filename

done

echo “Backup of SQL Datases Complete”
exit

 

I currently have not written a restore for the SQL backup, if you have phpMyAdmin, you can use the import function.

Okay, so we have backed up all the home directories. What if someone needs something specific from a specific backup. I know that this script doesn’t account for a failure. I’m working on that.

#!/bin/bash
##############################
#
# Script written by Dan Walker
# for Merval.Org Hosting to
# restore a specific file
# from a specific backup.
#
##############################

# Specify Backup Directory
echo -n “Where are we restoring from? (default is /backup/users): ”
while read -e inputline
do
backup_path=”$inputline”
# Display what user typed
if [ -z “${backup_path}” ]
then
echo “You didn’t type anything”
backup_path=”/backup/users”
echo “Using $backup_path”
else

if [ -n “${backup_path}” ]
then
backup_path=”$inputline”
echo “Using custom location: $backup_path”
fi
fi

# Lets ask what user to restore
echo -n “Which user?: ”
read -e user

echo -n “What are we restoring? (leave out /home/): ”
read -e source

# Now lets figure out what backup to restore
echo -n “Which backup? (Daily, Weekly or Monthly): ”
read -e choice
if [ $choice = “Daily” ];
then
echo -n “Which day? (Sunday – Friday): ”
read -e date
echo “You chose $date”
echo “Starting restore process. This may take a moment”
cd $backup_path
sudo -u $user tar -xzf $user-$date.tgz -C / home/$user/$source
echo “Restored /home/$user/$source from the $date backup”
fi

if [ $choice = “Weekly” ];
then
echo -n “Which week? (1-4): ”
read -e week
echo “You chose to restore to $week(s) ago”
echo “Starting restore process. This may take a moment”
cd $backup_path
sudo -u $user tar -xzf $user-week$week.tgz -C / home/$user/$source
echo “Restored /home/$user/$source to the back from $week week(s) ago”
fi

if [ $choice = “Monthly” ]
then
echo -n “Which Month? (1 or 2): ”
read -e month
echo “You chose to restore to $month(s) ago”
echo “Starting restore process. This may take a moment.”
cd $backup_path
sudo -u $user tar -xzf $user-month$month.tgz -C / home/$user/$source
echo “Restored /home/$user/$source from $month month(s) ago”
fi

exit
done

 

Suggestions are always appreciated!

Thanks for reading!

NYPD.. Are you for real right now?

I am disgusted with the NYPD. Ruthless Cowards who abuse their power as police officers. Disgusting. Ruthlessly beat and assaulted peaceful American Citizens for demonstrating their 1st Amendment Rights to Peacefully Gather. Your excuse this time? AGAIN “The Park Needed To Be Cleaned” So when the Parks need to be cleaned do you send Police in and beat the homeless senseless too? Do you go in there with batons and start beating and knocking people to the ground and dragging them out??
Hey NYPD! What about that poor young lady who had a seizure AFTER YOU ARRESTED HER AND YOU LET HER LAY THERE FOR 10 MINUTES BEFORE GIVING HER MEDICAL ATTENTION!?? You can’t deny this one, a live stream from a cell phone shows the young lady violently having a seizure and police did NOTHING. 10 minutes in Police and Paramedics shows up and started treating her! Can you say Police State? Regardless of your views on Occupy, What the NYPD did last night was Violent, Ruthless Aggression and was Completely uncalled for! After seeing so many attacks on Peaceful Americans, I am embarrassed because in the Land Of The FREE and Home Of The BRAVE, YOU CAN’T EVEN ASSEMBLE WITHOUT POLICE BEATING THE SNOT OF YOU!!

This needs to stop. I will make it my mission in life, Police have no right to execute that kind of violence on a peaceful crowd! If people are smashing windows and attacking other people, then it is a different story. But to walk into Zucatti Park and just start beating people and dragging them out. Simply uncalled for. Oh, and AGAIN the NYPD forced all Media OUT of the park and started harassing them about why they were covering the clearing of the park.

I can not sit by and watch Police Officers who are suppose to Protect and Serve, senselessly beat and attack peacefully gathering Americans! I have said it before and I will say it again, The Police are No longer Protecting and Serving the American People, The Police are now Protecting and Serving the Corporate Power and Money that runs the City they are employed by.

Well, I have a problem with that. When did the Police Department become Big Corporations personal Security? What happened to the Police being on the side of the Protecting the People? Police Officers are under this impression they are not affected by the same stuff citizens are. Scary part is, Police are underpaid and over worked just like the rest of us. The police don’t realize they truly are one layoff away from being part of the ever growing Unemployment line. The police may think they are protecting the interest of the City they work for but in fact they are hurting themselves. We are fighting for EVERYONE to have an equal playing field, not just a select few. We see a problem in the system and local and big government see us as a problem and proceed to send the police to attack.