Blog

Simple backup script for remotely backing up databases

Tags: backupbackupscPanelcpanel mysql backupdatabaseMySQLmysqldumpremoteremote mysql backupscript

Published on: May 25, 2011 by George K.

Simple backup script for remotely backing up databases

Scenario:

This database backup script is actually a small modification of another backup script available in Google. I am not able to get that URL to mark/link reference. The only modification I added is a four liner to remove a week old backup automatically. You may also see https://www.supportsages.com/2011/05/applying-wildcards-in-grant-option-of-mysql/ as well. Indenting in python is lost, will fix tomorrow 🙂


#!/usr/bin/env python
import ConfigParser
import os
import time

# Variable Definition
username = 'cpdbbackups'
password = 'p@ssw0Rd'
hostname = 're.mo.te.ip
backupfolder = '/home/dbbackups/servername'

filestamp = time.strftime('%Y-%m-%d-%H')
deletetime = time.time() - 7 * 86400

# Delete old files

for backup_file in os.listdir (backupfolder):
 full_file_path = os.path.join(backupfolder, backup_file)
 if os.path.getmtime(full_file_path) < deletetime:
 os.unlink(full_file_path)

# Get a list of databases with :
database_list_command="mysql -u%s -p%s -h %s --silent -N -e 'show databases'" % (username, password, hostname)
for database in os.popen(database_list_command).readlines():
 database = database.strip()

 if database == 'information_schema':
 continue
 filename = "%s/%s-%s.sql" % (backupfolder, database, filestamp)
 os.popen("mysqldump -u%s -p%s -h %s -e --opt -c %s | gzip -c > %s.gz" % (username, password, hostname, database, filename))

Category : General, Howtos, Linux, MySQL, Special Offers

George K.

George K.

George started his career in web hosting and Linux technical support in the year 2004 and is with SupportSages since 2009. He has keen interest in server optimizations, custom security solutions, hacked server recovery, cyber forensic and high availability fail over system design and implementation. George loves long drives and is passionate about art and literature.

You may also read:

Comments

Add new commentSIGN IN

Let's Connect

Categories

Your Cart

Cart is empty.

Subtotal
₹0.00
APPLY
0
Send this to a friend