Skip to content

Instantly share code, notes, and snippets.

@progress44
Created August 4, 2016 20:09
Show Gist options
  • Save progress44/f3444d13b12bb40877575d9af421c435 to your computer and use it in GitHub Desktop.
Save progress44/f3444d13b12bb40877575d9af421c435 to your computer and use it in GitHub Desktop.
Make a mysql dump and back it up on a different server
#!/bin/bash
# Server credentials
$account=""
$server2=""
$remote=""
# Create ssh key
# So, basically, run `ssh-keygen -t dsa` on the machine that will run your script.
# When it asks you for a passphrase, hit ENTER to accept a blank. You will get two files.
# If you followed the default suggestions, the files will be ~/.ssh/id_dsa
# and ~/.ssh/id_dsa.pub. The first one is the private key.
# The second one is the public key.
ssh-keygen -t dsa
# Copy the public key to the second server using ssh-copy-id user@server2.
# This will add the public key to the authorized_keys file of the user on server2.
ssh-copy-id $account@$server2
#!/bin/bash
# Server credentials
$account=""
$server=""
$remote=""
# Database credentials
user=""
password=""
host=""
db_name=""
# Other options
backup_path="/var/backups/mysql/backup"
date=$(date +"%d-%b-%Y")
# Set default file permissions
umask 177
# Dump database into SQL file
mysqldump --user=$user --password=$password --host=$host $db_name > $backup_path/$db_name-$date.sql
# Compress the backup file
# 7z a $backup_path/$db_name-$date.7z $backup_path/$db_name-$date.sql
zip $backup_path/$db_name-$date.zip $backup_path/$db_name-$date.sql
# Remove the original sql file
rm -I $backup_path/*.sql
# Send via email
file=$backup_path/$db_name-$date.zip
mailalert(){
sendmail -F Backup -it <<END_MESSAGE
To: info@clubsmade.com ## Your email here
Subject: Production DB backup
$(cat $file)
END_MESSAGE
}
# Send file to demo server
scp $file $account@$server:$remote
# Delete files older than 5 days
find $backup_path/* -mtime +5 -exec rm {} \;
#!/bin/bash
# Server credentials
$backuppath="/var/backups/mysql"
# Make the file executable
chmod +x $backuppath/backup.sh
# Open crontab with this comand
crontab -e
# Open the file using nano editor and then add this line to run
# the command every night at 23:59
59 23 * * * $backuppath/backup.sh
# Check crontab to see if the command is done
crontab -l
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment