Karal Max
May 5, 2016
Most of the sites rely on codebase & database which we can't afford to lose. We definitely need to implement a backup procedure in that case, otherwise, we may lose our valuable data due to some kind of mishap (manually deleting some data by mistake, software errors, hardware errors, server compromise, etc).
We can use the following shell script to make a backup (codebase and database).
First, let's create the backup script file:
nano /path/to/script/backup.sh
and add the following code:
#!/bin/bash
BACK_DIR="/home/ubuntu/backup"
PUBLIC_DIR="/var/www"
SITE_DIR="Justright"
DB_NAME="justright"
DB_USER="root"
DB_PASSWD="root"
DB_HOST="knackforge.com"
DATE="$(date +'%Y-%m-%d-%H-%M-%S')"
SQL_FILE_NAME="Database"
CODE_FILE_NAME="Codebase"
# Delete all old files, when creating new one
#Cleanup
cd $BACK_DIR;
rm -rf $SQL_FILE_NAME*;
rm -rf $CODE_FILE_NAME*;
# Backup "justright" site directory
cd $PUBLIC_DIR;
tar -czf $BACK_DIR/$CODE_FILE_NAME-$DATE.tar.gz $SITE_DIR;
# Backup "justright" databases
mysqldump --opt --protocol=TCP --user=$DB_USER --password=$DB_PASSWD --host=$DB_HOST $DB_NAME > $BACK_DIR/$SQL_FILE_NAME-$DATE.sql
cd $BACK_DIR;
tar -czf $SQL_FILE_NAME-$DATE.tar.gz $SQL_FILE_NAME-$DATE.sql;
rm $SQL_FILE_NAME-$DATE.sql;
Save the file and exit. Let's make that script executable:
chmod +x /path/to/script/backup.sh
To make the backup happen daily, we can set up our cronjob.
crontab -e
#Daily backup at 00:00
0 0 * * * /bin/sh /path/to/script/backup.sh >> /dev/null
Just like how your fellow techies do.
We'd love to talk about how we can work together
Take control of your AWS cloud costs that enables you to grow!