Bash Script

Membuat Backup dari Centos ke Synology

— Misal kita ingin membackup cacti dari centos ke Synology

1.buat dahulu script nya di server centos

mkdir -p  /usr/local/scripts/

-backup mysql

nano mysql-dump.sh

#!/bin/bash

TIME=`date +%F` # This Command will add date in Backup File Name.
FILENAME=games-mysql-$TIME.sql # Here i define Backup file name format.
DESDIR=/home/backup # Destination of backup file.

find $DESDIR/games-mysql-* -mtime +7 -exec rm {} \;
/usr/bin/mysqldump -u root -pkucing cacti > $DESDIR/$FILENAME
cd $DESDIR
if [ -f $FILENAME ]
then
echo “Successed `date`” >> /var/log/scripts/mysql.log
else
echo “Failed `date`” >> /var/log/scripts/mysql.log
fi

ket : pass sql = kucing

chmod a+x mysql-dump.sh

-backup rra

nano backup-rra.sh

#!/bin/bash

TIME=`date +%F` # This Command will add date in Backup File Name.
FILENAME=games-rra-$TIME.tar.gz # Here i define Backup file name format.
SRCDIR=rra/ # Location of Important Data Directory (Source of backup).
DESDIR=/home/backup # Destination of backup file.

find $DESDIR/games-rra-* -mtime +7 -exec rm {} \;
cd /usr/share/nginx/html/cacti/ && tar -cpzf $DESDIR/$FILENAME $SRCDIR
echo “Tar Success!!”
cd $DESDIR
if [ -f $FILENAME ]
then
echo “Successed `date`” >> /var/log/scripts/rra.log
else
echo “Failed `date`” >> /var/log/scripts/rra.log
fi

chmod a+x backup-rra.sh

-backup full web

nano backup-full.sh

#!/bin/bash

TIME=`date +%F` # This Command will add date in Backup File Name.
FILENAME=games-Web-$TIME.tar.gz # Here i define Backup file name format.
SRCDIR=cacti/ # Location of Important Data Directory (Source of backup).
DESDIR=/home/backup # Destination of backup file.

echo “” > /usr/share/nginx/html/cacti/log/cacti.log

find /home/backup/games-Web-* -mtime +60 -exec rm {} \;
echo “File not found!!”
cd /usr/share/nginx/html/ && tar -cpzf $DESDIR/$FILENAME $SRCDIR
echo “Tar Success!!”
cd $DESDIR
if [ -f $FILENAME ]
then
echo “Successed `date`” >> /var/log/scripts/web.log
else
echo “Failed `date`” >> /var/log/scripts/web.log
fi

Baca Juga :  Membuat Email alert spam di cpanel

chmod a+x nano backup-full.sh

2. Setting Crond di centos

### Backup Cacti ###
# Monthly (backup full cacti and mysql)
@monthly /usr/local/scripts/backup-full.sh

# Daily (backup rra and mysql)
@daily /usr/local/scripts/backup-rra.sh
10 00 * * * /usr/local/scripts/mysql-dump.sh

3. Siapkan server ubuntu / debian

apt-get install ftp-upload

mkdir /usr/local/scripts

mkdir /home/backup/

mkdir /home/backup/server/mrtg/

nano backup-mrtg

chmod a+x backup-mrtg

#!/bin/bash

d=$(date ‘+%F’)
pass=”kucing123″

echo -e ‘\n[ ‘$d’ ]’ >> /var/log/backup/server-mrtg.log

#traffic
sshpass -p $pass ssh -p2225 -o StrictHostKeyChecking=no -o ConnectTimeout=10 root@games.com stat /home/backup/*$d* > /dev/null 2>&1
[ $? -eq 0 ] && sshpass -p $pass scp -P2225 -o StrictHostKeyChecking=no -o ConnectTimeout=10 root@games.com:/home/backup/*$d* /home/backup/server/mrtg/. || echo -e ‘traffic \e[91mfailed\e[0m’ >> /var/log/backup/server-mrtg.log

sleep 1m

ftp-upload -h lapo.games.com:221 -u server –password 1Bo38xPE8@ -d /Provisioning/Server/MRTG /home/backup/server/mrtg/*$d* > /dev/null

[ $? -ne 0 ] && echo -e ‘ftp \e[91mfailed\e[0m’ >> /var/log/backup/server-mrtg.log || echo -e ‘ftp \e[32msuccess\e[0m’ >> /var/log/backup/server-mrtg.log

find /home/backup/server/mrtg/* -mtime +180 -exec rm {} \;

echo “Done”

ket : kucing123 = password login ke server cacti

1Bo38xPE8@ = password login ke synology

4.Buat crond nya

########## mrtg
30 5 * * * /usr/local/scripts/backup-mrtg > /tmp/backup-mrtg 2>&1

Klik untuk berbagi dengan orang lain

Similar Posts