CentOS website backup shell scripts
It’s vitally important to backup your website on another machine. If you have a website running on a machines and have a FTP account on another server or even a computer at your home, Follow this step-by-step guide, you will be setting up a reliable automatic regular backup.
Step 1: SSH to your VPS console and install lftp
yum install lftp
Step 2: Create a folder on your ftp server that will hold the backup files.
Step 3: Edit the following script and unload it to your VPS, for example, /root/backup.sh
You will need to set the values like your root mysql account and ftp account, web folder to backup.
#!/bin/bash
#Author: licess
#Website: http://lnmp.org
#IMPORTANT!!!Please Setting the following Values!
######~Set Directory you want to backup~######
#website dir under /home/web
$www_base_dir = /home/web
$local_backup_dir = /home/backup/out
Backup_Dir1=xxx.com
######~Set MySQL UserName and password~######
MYSQL_UserName=root
MYSQL_PassWord=your_root_password
######~Set MySQL Database you want to backup~######
Backup_Database_Name1=_database_to_backup
#Uncomment these if you have more databases to backup
#Backup_Database_Name2=_gt4tcn
#Backup_Database_Name3=_dallascaoen
#Backup_Database_Name4=_dallascaocn
######~Set FTP Information~######
#setup your FTP where you backup your websites to
FTP_HostName1=hostname_or_IP
FTP_UserName1=FTP_username
FTP_PassWord1=FTP_password
#you need to create dir on your ftp first.
FTP_BackupDir1=/backup_dir/
#Values Setting END!
#TodayWWWBackup=www-*-$(date +"%Y%m%d").tar.gz
#* is wildcard.
TodayDBBackup=db-*-$(date +"%Y%m%d").sql
OldWWWBackup=www-*-$(date -d -3day +"%Y%m%d").tar.gz
OldDBBackup=db-*-$(date -d -3day +"%Y%m%d").sql
tar zcf /home/backup/www-$Backup_Dir1-$(date +"%Y%m%d").tar.gz -C $www_dir $Backup_Dir1 --exclude=soft
/usr/local/mysql/bin/mysqldump -u$MYSQL_UserName -p$MYSQL_PassWord $Backup_Database_Name1 > $local_backup_dir/db-$Backup_Database_Name1-$(date +"%Y%m%d").sql
/usr/local/mysql/bin/mysqldump -u$MYSQL_UserName -p$MYSQL_PassWord $Backup_Database_Name2 > $local_backup_dir/db-$Backup_Database_Name2-$(date +"%Y%m%d").sql
/usr/local/mysql/bin/mysqldump -u$MYSQL_UserName -p$MYSQL_PassWord $Backup_Database_Name3 > $local_backup_dir/db-$Backup_Database_Name3-$(date +"%Y%m%d").sql
/usr/local/mysql/bin/mysqldump -u$MYSQL_UserName -p$MYSQL_PassWord $Backup_Database_Name4 > $local_backup_dir/db-$Backup_Database_Name4-$(date +"%Y%m%d").sql
#will delete multiple files db-*-$(date +"%Y%m%d").sql
rm -f $local_backup_dir$OldWWWBackup
rm -f $local_backup_dir$OldDBBackup
cd $local_backup_dir
lftp $FTP_HostName1 -u $FTP_UserName1,$FTP_PassWord1 << EOF
cd $FTP_BackupDir1
mrm $OldDBBackup
mput $TodayDBBackup
bye
EOF
#mput $TodayWWWBackup
4. Test out the script to see if it works as expected. You shall have files like www-xxxx-20120101-tar.gz and db-xxxx-20120101.sql on both your local backup dir and remote ftp backup dir.
Also notice that the script will delete backup files 3 days old.
5. Setup a crontab entry to make this script run automatically everyday.
crontab -e
Enter:
0 0 * * * bash /root/backup.sh
Save the file and exit.
Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to my feed and get articles like this delivered automatically to your feed reader.
3 Responses to “CentOS website backup shell scripts”
Leave a Reply
You must be logged in to post a comment.
Twitter
Email
RSS
March 1st, 2012 at 1:40 am
I do believe all the concepts you have presented in your post. They’re really convincing and can certainly work. Nonetheless, the posts are very short for newbies. May just you please lengthen them a little from subsequent time? Thank you for the post.
May 21st, 2012 at 12:11 am
I setup this script on my server, but for a reason that i don’t know it don’t works.
May 21st, 2012 at 5:45 pm
First of all you must
chmod +x backup.sh