Skip to content

Instantly share code, notes, and snippets.

@Hemant-Mann
Last active June 1, 2016 08:04
Show Gist options
  • Save Hemant-Mann/bdf2c220270f34cdb46634a888636902 to your computer and use it in GitHub Desktop.
Save Hemant-Mann/bdf2c220270f34cdb46634a888636902 to your computer and use it in GitHub Desktop.
Automated Script to take backup of projects
#!/bin/bash
# Initialize Global Variables
db_name=
mysql_root=
server_ip=
server_user=
server_pass=
server_path=
project=
project_dir=
# Read the values parsed from json file
read -p "DB Name: " db_name
read -p "Mysql root pass: " mysql_root
read -p "Backup Server IP: " server_ip
read -p "Backup Server User: " server_user
read -p "Backup Server User-pass: " server_pass
read -p "Backup Server path: " server_path # path can be relative to user or full-path
# path should exist on remote server otherwise error will be thrown
read -p "Project: " project
read -p "Project Dir: " project_dir
# Check for expect
type expect 2> /dev/null || { sudo apt-get install -y expect; }
cd $project_dir
push_backup() {
expect -c "
set timeout -1
spawn scp -o StrictHostKeyChecking=no ../${project}.tar ${server_user}@${server_ip}:${server_path}
expect password: { send $server_pass\r }
expect '100%'
sleep 1
exit
"
rm ../${project}.tar
}
backup_mysql() {
if [[ ! $db_name ]]; then
return 1;
fi
mysqldump -u root -p${mysql_root} ${db_name} > ${db_name}.sql
}
make_tar() {
cd ..
tar -cvzf ${project}.tar ${project_dir##*/}
rm $project_dir/${db_name}.sql
cd $project_dir
}
logging() {
output=`date`
filename="`date +%Y-%m-%d`.txt"
mkdir -p ~/backup_logs
touch ~/backup_logs/$filename
echo "Backup: '$project' (Server: $server_ip, path: $server_path) Completed on $output" >> ~/backup_logs/$filename
}
backup_mysql > /dev/null
make_tar
push_backup
logging
@Hemant-Mann
Copy link
Author

Hemant-Mann commented Apr 7, 2016

curl -o ~/parse_json.py -L http://bit.ly/22yusia && python ~/parse_json.py ~/automate/credentials.json | bash <(curl -s https://gist.github.com/Hemant-Mann/bdf2c220270f34cdb46634a888636902/raw/b6c0de614b777ecafeb508e3eb36d16410276db0/automated_backup_script.sh)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment