Shell Script: Backup website

Post Reply
User avatar
hiccup
Site Admin
Posts: 19
Joined: Sat Aug 01, 2015 8:44 pm

Shell Script: Backup website

Post by hiccup » Sat Aug 15, 2015 11:09 am

~/.bashrc

Code: bash Select all

function backup () {
	cnt=0
case "$1" in
	site1)
		DB_USER="db_usename"
		DB_PASS="password"
		DB_NAME="db_name"
		DB_HOST="localhost"
		FILE_DIR="directory"
		;;
	site2)
		DB_USER="db_usename"
		DB_PASS="password"
		DB_NAME="db_name"
		DB_HOST="localhost"
		FILE_DIR="directory"
		;;
	site_multi)
		cnt=10
		DB_USER="db_user_1"
		DB_PASS="pass_1"
		DB_NAME="db_1"
		DB_USER2="db_user_2"
		DB_PASS2="pass_2"
		DB_NAME2="db_2"
		DB_HOST="localhost"
		FILE_DIR="directory"
		;;
	custom)
		echo "Enter Directory / App name:"
		read FILE_DIR
		echo "Database Name:"
		read DB_NAME
		echo "Database User Name:"
		read DB_USER
		echo "Database Password:"
		read DB_PASS
		DB_HOST="localhost"
		;;
	*)
		echo $"Invalid site name"
		return 0
		;;
esac

NOW=$(date +"%Y-%m-%d-%H%M")
TREE_FILE="$FILE_DIR.$NOW.tar.gz"
DB_FILE="$DB_NAME.$NOW.sql"
if [ -z "${FILE_DIR}" ];
then
	echo "$(tput setaf 1)empty Directory name$(tput sgr 0)"
	return 0
fi
if [ $FILE_DIR -eq "backups" ]; then
	echo "$(tput setaf 1)Invalid Directory name.$(tput sgr 0)"
	return 0
fi
BACKUP_DIR="/home/deesign/webapps/backups/$FILE_DIR/$NOW"
WWW_DIR="."
if cd /home/deesign/webapps/$FILE_DIR; then
	mkdir -p $BACKUP_DIR
else
	echo
	echo
	echo "$(tput setaf 1)Invalid Directory Name.(tput sgr 0)" 1>&2
	return 0
fi
echo 'Backing up database..'

if mysqldump -h$DB_HOST -u$DB_USER -p$DB_PASS $DB_NAME > $BACKUP_DIR/$DB_FILE; then
	gzip $BACKUP_DIR/$DB_FILE
else
	rm "$BACKUP_DIR/$DB_FILE"
	cd
	echo
	echo
	echo "$(tput setaf 1)Invalid Database/user/pass.(tput sgr 0)" 1>&2
	return 0
fi	

if [ $cnt -eq 10 ]
then
	DB_FILE2="$DB_NAME2.$NOW.sql"
	echo 'Backing up database 2..'
	mysqldump -h$DB_HOST -u$DB_USER2 -p$DB_PASS2 $DB_NAME2 > $BACKUP_DIR/$DB_FILE2
	gzip $BACKUP_DIR/$DB_FILE2
fi	

echo 'Database backup comlete.'
echo 'Backing up files..'
tar -czvf $BACKUP_DIR/$TREE_FILE $WWW_DIR
echo 
echo 
echo 'Files backup comlete.'
echo 
echo 
echo 
echo 'Database saved as '$BACKUP_DIR/$DB_FILE.gz
echo 
echo "$(tput setaf 3)"
echo 'http://deesign.works/backup/'$FILE_DIR/$NOW/$DB_FILE.gz
echo "$(tput sgr 0)"
if [ $cnt -eq 10 ]
then
	cnt=0
	echo 
	echo 'Database 2 saved as '$BACKUP_DIR/$DB_FILE2.gz
	echo 
	echo "$(tput setaf 3)"
	echo 'http://deesign.works/backup/'$FILE_DIR/$NOW/$DB_FILE2.gz
	echo "$(tput sgr 0)"
	echo 
fi
echo 'Files saved as '$BACKUP_DIR/$TREE_FILE
echo 
echo "$(tput setaf 3)"
echo 'http://deesign.works/backup/'$FILE_DIR/$NOW/$TREE_FILE
echo "$(tput sgr 0)"
cd
}
Script to backup database and clear the backup directory of older files.

Code: bash Select all

#!/bin/bash
## Script to backup database and clear the backup directory of older files.
## Author: Shafail Shafa
## Data: 10/10/16

# Database info
	db_name=""
	db_usr=""
	db_pass=""
	db_host="localhost"

#Options
	backup_path="/path/to/folder/db_backup"
	date=$(date +"%d-%b-%Y")
	cd $backup_path

# Dump database
	mysqldump --user=$db_usr --password=$db_pass --host=$db_host $db_name | gzip > $backup_path/$db_name-$date.sql.gz

#delete older files
count=$(find $backup_path -mtime -5 | wc -l )

if [ $count -gt 4 ]; then
	find $backup_path/*.sql.gz -mtime +5 -delete
fi

User avatar
hiccup
Site Admin
Posts: 19
Joined: Sat Aug 01, 2015 8:44 pm

Re: Shell Script: Backup website

Post by hiccup » Sat Aug 15, 2015 12:37 pm

...

Post Reply