1

I tried fabric with a '>' in the command string. It always gives out an error code 2. Currently dabbling with subprocess.call, subprocess.check_output and keeping stdout="filesocket". Not working. The only thing that gets written in the file is the USAGE for mysqldump. Using shlex to parse 'mysqldump -uroot -ppassword database table1 table2'

All this because I don't know shell scripting with string variables from the 'date' utility. How do I take the current date and use it to name the backup file in shell script. OR how do I get this thing done in python?

Thanks in advance.

regards.

aniketd
  • 385
  • 1
  • 3
  • 15

3 Answers3

2

You can get a custom date out of date using the following syntax.

CUSTOM_DATE=$(date "+%Y-%m-%d_%H_%M_%S")

The easiest way to accomplish this is to put a script on the remote end that does 'everything'

#!/bin/bash

CUSTOM_DATE=$(date "+%Y-%m-%d_%H_%M_%S")
mysqldump -u admin -p password database table1 table2 >/path/to/backups/mysqldump.${CUSTOM_DATE}.db
synthesizerpatel
  • 27,321
  • 5
  • 74
  • 91
1

My Answer from related stackoverflow post.

In Microsoft Windows, run below command in CMD

mysqldump -u USERNAME -pYOURPASSWORD --all-databases > "C:/mysql_backup_%date:~-10,2%-%date:~-7,2%-%date:~-4,4%-%time:~0,2%_%time:~3,2%_%time:~6,2%.sql"

Output file will look like,

mysql_backup_21-02-2015-13_07_18.sql

If you want to automate the backup process, then you can use Windows Task Scheduler, and put above command in .bat file - task scheduler will run the .bat file at specified interval.

Community
  • 1
  • 1
Piyush Patel
  • 1,765
  • 1
  • 16
  • 29
1

"How do I take the current date and use it to name the backup file in shell script. OR how do I get this thing done in python?"

from datetime import datetime

filename = 'mysql_backup_{0:%Y%m%d_%H%M}.sql'.format(datetime.now())

# filename == 'mysql_backup_20120227_0952.sql'
eumiro
  • 207,213
  • 34
  • 299
  • 261