Linux从Webmin执行MySQL备份到基于时间的路径

时间:2020-12-24 12:37:50

I was trying to setup a scheduled task in Linux Ubuntu Server 12.04 (CronJob) to perform a daily backup of all my MySQL Databases on midnight.

我试图在Linux Ubuntu Server 12.04(CronJob)中设置一个计划任务,以便在午夜执行我所有MySQL数据库的每日备份。

I have installed the known Webmin (A nice web interface for managing the Web Servers).

我已经安装了已知的Webmin(一个用于管理Web服务器的漂亮的Web界面)。

So my issue is: whenever the backup is being performed, the files are getting overwritten!

所以我的问题是:每当执行备份时,文件都会被覆盖!

That means: The backup of the day before yesterday are LOST, only the "Yesterday" backup is being saved!

这意味着:前天的备份很少,只保存“昨天”备份!

I have tried something like setting dynamic file path like:

我试过像设置动态文件路径之类的东西:

/var/www/mysqlbackups/%d-%m-%y

but I had no success with that :(

但我没有成功:(

Can anybody help me.

有谁能够帮助我。

Thanks alot guys.

非常感谢你们。

4 个解决方案

#1


2  

I made a shell script (not for webmin). Put it in /etc/cron.daily.

我制作了一个shell脚本(不适用于webmin)。把它放在/etc/cron.daily中。

The scripts makes a backup of the database (stores it as .gz), then uploads it by ssh to another server. For the auth. i setup ssh keys, so no password is needed. The backup files have a unique name, so you don't overwrite the backup files.

脚本对数据库进行备份(将其存储为.gz),然后通过ssh将其上载到另一台服务器。对于auth。我设置了ssh密钥,因此不需要密码。备份文件具有唯一名称,因此您不会覆盖备份文件。

This is how you can create a filename within script:

这是您在脚本中创建文件名的方法:

now=`date  +%Y%m%d_%H%M`
dst_path=/var/local/backups
filename="$dst_path/$database.$now.sql.gz"

Then you should write a a small script that removes all backup files that are older then x days.

然后你应该写一个小脚本,删除所有早于x天的备份文件。

#!/bin/sh

#
# Creates a backup of a MySQL databases and uses ssh (sFTP) to send if  to another server
# This script shouldbe called from the crontab

PATH=/usr/sbin:/usr/bin:/sbin:/bin

# MySQL user and password
mysql_cmd=/opt/bitnami/mysql/bin/mysqldump
mysql_usr=user_name
mysql_pass=password

# destination ssh
dst_user=user_name
dst_hostname=192.168.1.1

# Database to backup
database=test

# create timestamp
now=`date  +%Y%m%d_%H%M`

# where we store the files
dst_path=/var/local/backups

# backup filename
filename="$dst_path/$database.$now.sql.gz"
dst_filename="$database.$now.sql.gz"

# run backup
$mysql_cmd -u $mysql_usr --password=$mysql_pass  $database | gzip > $filename

# upload to sever (ssh)
scp $filename $dst_user@$dst_hostname:

#2


11  

You can use dynamic path, but before you must enable it in module config:

您可以使用动态路径,但在必须在模块配置中启用它之前:

System > Filesystem Backup > Module Config > Do strftime substitution of backup destinations? > Yes

(If you are not sure about placeholders, just click on the text "Do strftime substitution of backup destinations?" in the config and help will show you.)

(如果您不确定占位符,只需单击配置中的“备份目标的strftime替换?”文本,帮助将显示给您。)

#3


11  

MySQL Database Server > Module Config > Chose Yes on "Do strftime substitution of backup destinations?"

MySQL数据库服务器>模块配置>选择“执行备份目标的strftime替换吗?”

It work for me! :)

它对我有用! :)

#4


6  

I had the same problem and solved like this :

我有同样的问题并解决了这样的问题:

From Server select MySQL Database Server. Go to Module config (top left), and select Do strftime substitution of backup destinations? to Yes.

从服务器选择MySQL数据库服务器。转到Module config(左上角),然后选择Do strftime替换备份目标?是的。

I use db_%d-%m-%Y.sql format for backups. (db_13-03-2013.sql)

我使用db_%d-%m-%Y.sql格式进行备份。 (db_13-03-2013.sql)

#1


2  

I made a shell script (not for webmin). Put it in /etc/cron.daily.

我制作了一个shell脚本(不适用于webmin)。把它放在/etc/cron.daily中。

The scripts makes a backup of the database (stores it as .gz), then uploads it by ssh to another server. For the auth. i setup ssh keys, so no password is needed. The backup files have a unique name, so you don't overwrite the backup files.

脚本对数据库进行备份(将其存储为.gz),然后通过ssh将其上载到另一台服务器。对于auth。我设置了ssh密钥,因此不需要密码。备份文件具有唯一名称,因此您不会覆盖备份文件。

This is how you can create a filename within script:

这是您在脚本中创建文件名的方法:

now=`date  +%Y%m%d_%H%M`
dst_path=/var/local/backups
filename="$dst_path/$database.$now.sql.gz"

Then you should write a a small script that removes all backup files that are older then x days.

然后你应该写一个小脚本,删除所有早于x天的备份文件。

#!/bin/sh

#
# Creates a backup of a MySQL databases and uses ssh (sFTP) to send if  to another server
# This script shouldbe called from the crontab

PATH=/usr/sbin:/usr/bin:/sbin:/bin

# MySQL user and password
mysql_cmd=/opt/bitnami/mysql/bin/mysqldump
mysql_usr=user_name
mysql_pass=password

# destination ssh
dst_user=user_name
dst_hostname=192.168.1.1

# Database to backup
database=test

# create timestamp
now=`date  +%Y%m%d_%H%M`

# where we store the files
dst_path=/var/local/backups

# backup filename
filename="$dst_path/$database.$now.sql.gz"
dst_filename="$database.$now.sql.gz"

# run backup
$mysql_cmd -u $mysql_usr --password=$mysql_pass  $database | gzip > $filename

# upload to sever (ssh)
scp $filename $dst_user@$dst_hostname:

#2


11  

You can use dynamic path, but before you must enable it in module config:

您可以使用动态路径,但在必须在模块配置中启用它之前:

System > Filesystem Backup > Module Config > Do strftime substitution of backup destinations? > Yes

(If you are not sure about placeholders, just click on the text "Do strftime substitution of backup destinations?" in the config and help will show you.)

(如果您不确定占位符,只需单击配置中的“备份目标的strftime替换?”文本,帮助将显示给您。)

#3


11  

MySQL Database Server > Module Config > Chose Yes on "Do strftime substitution of backup destinations?"

MySQL数据库服务器>模块配置>选择“执行备份目标的strftime替换吗?”

It work for me! :)

它对我有用! :)

#4


6  

I had the same problem and solved like this :

我有同样的问题并解决了这样的问题:

From Server select MySQL Database Server. Go to Module config (top left), and select Do strftime substitution of backup destinations? to Yes.

从服务器选择MySQL数据库服务器。转到Module config(左上角),然后选择Do strftime替换备份目标?是的。

I use db_%d-%m-%Y.sql format for backups. (db_13-03-2013.sql)

我使用db_%d-%m-%Y.sql格式进行备份。 (db_13-03-2013.sql)