Today, I will show you simple bash script I’m using to backup data in CentOS server. Before We beginning, please ensure you have “rsync” package installed.
1. Setting up Backup script
Make a new directory with following command
# mkdir -p /bak # chown root.root # chmod 700 /bak
Create a bash script
# vi /bak/fbk.sh # chmod +x /bak/fbk.sh
Add the following code
#!/bin/bash # Full Backup Server # Support backup multiple directories, multiple databases and files configuration # Copyright (c) 2010-2013 LongVNIT # This script is licensed under GNU GPL version 2.0 or above ### SERVER BACKUP SERVER="1.1.1.1" USERNAME="username" IDENTITY="id_rsa" SSH_PORT="22" ### MYSQL INFO MHOST="localhost" MUSER="root" MPASS="password" ### BACKUP DIRECTORY BAK="/bak" DATA="/home" MYSQL=$BAK"/db" CONF=$BAK"/conf" EXCLUDE=$BAK"/exclude.txt" TEMP_DIR=$BAK"/tmp" ### BACKUP DATA TYPE: 1 FULL, 2: 1-3-5 TYPE=2 DAILY=$(date +"%u") ### MYSQL BACKUP FUNCTION doMySQL() { DBS="$(mysql -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')" [ ! -d $MYSQL/$DAILY ] && mkdir -p $MYSQL/$DAILY || rm -f $MYSQL/$DAILY/mysql* for db in $DBS do mysqlcheck -or -u$MUSER -p$MPASS $db FILE=$MYSQL/$DAILY/mysql-$db.gz mysqldump -u $MUSER -h $MHOST -p$MPASS $db | gzip -9 > $FILE done ### BACKUP rsync -av --delete -e "ssh -p $SSH_PORT -i $BAK/$IDENTITY -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" $MYSQL/$DAILY/* $USERNAME@$SERVER:db/$DAILY } doConfig() { rm -rf $TEMP_DIR/* mkdir -p $TEMP_DIR/etc # Backup system configuration cp /etc/sysctl.conf $TEMP_DIR/etc # Backup iptables iptables-save > $TEMP_DIR/iptables.txt > /dev/null 2>&1 # Backup my.cnf if [ -e /etc/my.cnf ]; then cp /etc/my.cnf $TEMP_DIR/etc fi if [ -d /etc/lcache ]; then cp -R /etc/lcache $TEMP_DIR/etc fi # Backup sshd cp -R /etc/ssh $TEMP_DIR/etc # Backup crontab crontab -l > $TEMP_DIR/crontab.txt > /dev/null 2>&1 # Backup logrotate cp -R /etc/logrotate.d $TEMP_DIR/etc # Backup init.d cp -R /etc/init.d $TEMP_DIR/etc # Backup fstab cp /etc/fstab $TEMP_DIR/etc # Backup webserver configuration if [ -d /webserver ]; then mkdir -p $TEMP_DIR/webserver fi # Backup PHP if [ -d /webserver/php ]; then mkdir -p $TEMP_DIR/webserver/php cp -R /webserver/php/etc $TEMP_DIR/webserver/php fi # Backup Apache if [ -d /webserver/apache ]; then mkdir -p $TEMP_DIR/webserver/apache cp -R /webserver/apache/conf $TEMP_DIR/webserver/apache fi # Backup Lighttpd if [ -d /webserver/lighttpd ]; then mkdir -p $TEMP_DIR/webserver/lighttpd cp -R /webserver/lighttpd/conf $TEMP_DIR/webserver/lighttpd fi # Backup Nginx if [ -d /webserver/nginx ]; then mkdir -p $TEMP_DIR/webserver/nginx cp -R /webserver/nginx/conf $TEMP_DIR/webserver/nginx fi # Backup proftpd if [ -d /webserver/proftpd ]; then mkdir -p $TEMP_DIR/webserver/proftpd cp -R /webserver/proftpd/etc $TEMP_DIR/webserver/proftpd fi # Backup Pure-FTPD if [ -f /etc/pure-ftpd.conf ]; then mkdir -p $TEMP_DIR/pure-ftpd cp -R /etc/pure* $TEMP_DIR/pure-ftpd fi # Backup webserver etc if [ -d /webserver/etc ]; then cp -R /webserver/etc $TEMP_DIR/webserver/ fi # BACKUP [ ! -d $CONF ] && mkdir -p $CONF || rm -f $CONF/conf-$DAILY.tar.gz tar cvfz $CONF/conf-$DAILY.tar.gz $TEMP_DIR/* ### BACKUP rsync -av --delete -e "ssh -p $SSH_PORT -i $BAK/$IDENTITY -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" $CONF/* $USERNAME@$SERVER:conf/ } doData() { ### BACKUP rsync -av --delete -e "ssh -p $SSH_PORT -i $BAK/$IDENTITY -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" --exclude-from $EXCLUDE $DATA/* $USERNAME@$SERVER:data/$DAILY } ### PROCESS doConfig; doMySQL; if [ $TYPE=="2" ]; then if [[ $DAILY=="1" || $DAILY=="3" || $DAILY=="5" ]]; then doData; fi else doData; fi
Setting up Crontab
Now I will show you how to schedule our backup process. In CentOS we use cron jobs in order to schedule task. For setting up cron jobs we use “crontab -e” command in command line.
Example: I want to run this backup process on daily at 3:00 PM.
01 03 * * * /bak/fbk.sh