转载备份。
本来是想用 shell 脚本的,但是我个人更熟 Python 脚本,所以选择了 Python。
不需要安装 Python 依赖,只需要有 mysqldump 和 gzip 这两个命令就行,毕竟本质还是在拼接 shell。
2022.4.17 更新:既然都写 Python 了,为什么不再封装一个包出来,服务器上直接调包,还能有参数选项自动补全呢?所以就有了我的第一个 PyPI 包:vps-backup-utils (opens new window)。
#!/usr/bin/python3
###########################################################
#
# This python script is used for mysql database backup
# using mysqldump and tar utility.
#
# Written by : Rahul Kumar
# Website: https://tecadmin.net/python-script-for-mysql-database-backup/
# Created date: Dec 03, 2013
# Last modified: Aug 17, 2018
# Tested with : Python 2.7.15 & Python 3.5
# Script Revision: 1.4
#
##########################################################
# Import required python libraries
import os
import time
import datetime
import pipes
# MySQL database details to which backup to be done. Make sure below user having enough privileges to take databases backup.
# To take multiple databases backup, create any file like /backup/dbnames.txt and put databases names one on each line and assigned to DB_NAME variable.
DB_HOST = 'localhost'
DB_USER = 'root'
DB_USER_PASSWORD = '_mysql_user_password_'
#DB_NAME = '/backup/dbnameslist.txt'
DB_NAME = 'db_name_to_backup'
BACKUP_PATH = '/backup/dbbackup'
# Getting current DateTime to create the separate backup folder like "20180817-123433".
DATETIME = time.strftime('%Y%m%d-%H%M%S')
TODAYBACKUPPATH = BACKUP_PATH + '/' + DATETIME
# Checking if backup folder already exists or not. If not exists will create it.
try:
os.stat(TODAYBACKUPPATH)
except:
os.mkdir(TODAYBACKUPPATH)
# Code for checking if you want to take single database backup or assinged multiple backups in DB_NAME.
print ("checking for databases names file.")
if os.path.exists(DB_NAME):
file1 = open(DB_NAME)
multi = 1
print ("Databases file found...")
print ("Starting backup of all dbs listed in file " + DB_NAME)
else:
print ("Databases file not found...")
print ("Starting backup of database " + DB_NAME)
multi = 0
# Starting actual database backup process.
if multi:
in_file = open(DB_NAME,"r")
flength = len(in_file.readlines())
in_file.close()
p = 1
dbfile = open(DB_NAME,"r")
while p <= flength:
db = dbfile.readline() # reading database name from file
db = db[:-1] # deletes extra line
dumpcmd = "mysqldump -h " + DB_HOST + " -P " + DB_PORT + " -u " + DB_USER + " -p" + DB_USER_PASSWORD + " " + db + " > " + pipes.quote(TODAYBACKUPPATH) + "/" + db + ".sql"
os.system(dumpcmd)
gzipcmd = "gzip " + pipes.quote(TODAYBACKUPPATH) + "/" + db + ".sql"
os.system(gzipcmd)
p = p + 1
dbfile.close()
else:
db = DB_NAME
dumpcmd = "mysqldump -h " + DB_HOST + " -P " + DB_PORT + " -u " + DB_USER + " -p" + DB_USER_PASSWORD + " " + db + " > " + pipes.quote(TODAYBACKUPPATH) + "/" + db + ".sql"
os.system(dumpcmd)
gzipcmd = "gzip " + pipes.quote(TODAYBACKUPPATH) + "/" + db + ".sql"
os.system(gzipcmd)
print ("")
print ("Backup script completed")
print ("Your backups have been created in '" + TODAYBACKUPPATH + "' directory")
然后加入 crontab
,凌晨四点执行:
0 4 * * * /etc/profile && cd /path/to/sql && mysql_backup.py
crontab 的语法可以参考 crontab (opens new window)