Skip to content

Instantly share code, notes, and snippets.

@hhaswan
Last active June 12, 2021 07:32
Show Gist options
  • Select an option

  • Save hhaswan/cac4933e45d32047bfe5ef6df37fc844 to your computer and use it in GitHub Desktop.

Select an option

Save hhaswan/cac4933e45d32047bfe5ef6df37fc844 to your computer and use it in GitHub Desktop.
Script auto backup mysql linux
#inisiasi variabel
backupfolder=/backup-db1
user=root01
password=P@ssw0rd
keep_day=14
tanggal=$(date +%Y-%m-%d_%H-%M-%S)
hari=$(date +%Y-%m-%d)
sqlfile=all-database-$tanggal.sql
zipfile=all-database-$tanggal.zip
#backup
cd $backupfolder
sudo mysqldump -u $user -p$password --all-databases > $sqlfile
if [ $? == 0 ]; then
echo 'Backup SQL berhasil pada '$hari >> log-db.txt
else
echo 'Backup SQL gagal pada '$hari >> log-db.txt
exit
fi
#zip file backup
zip $zipfile $sqlfile
if [ $? == 0 ]; then
echo 'File backup terkompres pada '$hari >> log-db.txt
else
echo 'File gagal kompres pada '$hari >> log-db.txt
exit
fi
#hapus file sql
rm $sqlfile
#masukkan report ke log
echo $zipfile ' berhasil backup' >> log-db.txt
#delete file lama dan copy file baru ke server sebelah
find $backupfolder -mtime +$keep_day -delete
scp log-db.txt root@192.168.1.111:/backup-db1
scp $zipfile root@192.168.1.111:/backup-db1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment