Mysqldump and crontab(linux) [closed]

Asked
Active3 hr before
Viewed126 times

9 Answers

closed
90%

In your case you could insert that mysqldump command to a script then source the profile of the user who is executing the mysqldump command for eg: ,So one solution to avoid this condition is to use --no-defaults option for mysqldump. The option file then won't be read. However, you provide credentials via command line, so anyone who can issue a ps can actually see the password once the backup runs. So it's best if you create an own option file with user name and password and pass this to mysqldump via --defaults-file.,This file is an option file read automatically when using mysql or mysqldump. So basically you then had login credentials given twice - in that file and on command line. This was the problem I had.,I am using Percona Server (a MySQL fork) on Ubuntu. The package (very likely the regular MySQL package as well) comes with a maintenance account called debian-sys-maint. In order for this account to be used, the credentials are created when installing the package; and they are stored in /etc/mysql/debian.cnf.

You need to escape % character with \

mysqldump - u 'username' - p 'password'
DBNAME > /home/eric / db_backup / liveDB_`date +\%Y\%m\%d_\%H\%M`.sql
load more v
88%

I want to make an automatic backup of my database, so i wrote like this

I want to make an automatic backup of my database, so i wrote like this

00 10 * * * root mysqldump - u root - ppasswordD billing "/home/backup/database_`date '+%m-%d-%Y'`.sql"
load more v
72%

I am trying to do a mysqldump on the cron for every 5 minutes but it seems that it is not working. I try to execute the mysqldump and it works fine., Do I tell a prospective employer that I'm going to continue looking for jobs in my field if they hire me? ,Server Fault is a question and answer site for system and network administrators. It only takes a minute to sign up.,I think I found the problem, it is in date +"%T", because if I replace it with date -I it now works. However, I need to append a date & time on the filename.

mysqldump

mysqldump - uroot - ppassword--single_transaction--opt dbname | gzip > /home/myhome / backup / dbname.
`date +"%T"`.sql.gz
load more v
65%

In the file script you must add option passs( -p PASSWORD) on command mysqldump because when run script you can enter password so script not running. You re-add command to script with option -p. , Why, because if you not add option -p<Password>, you cannot run script on cron – Tannetto Aug 28 '18 at 4:04 , NEVER use -p; it will be plain text shown in ps . Mysql had an option where you can store username and password. user and password should be in --defaults-file so -u and -p are useless it the command. – Rinzwind Aug 27 '18 at 19:12 ,I've been going through all the various questions about why a specific script run in cron and nothing has helped me thus far. So I'm just trying to run a mysqldump script that now looks like this:

In the file script you must add option passs( -p PASSWORD) on command mysqldump because when run script you can enter password so script not running. You re-add command to script with option -p.

mysqldump --defaults-file=/home/<user>/.my.cnf --databases 360_projects --single-transaction --add-drop-database --triggers --routines -u BACKUPUSER -p PASSWORD > $1pmbackup${today}.sql
load more v
75%

As stated in man mysqldump: see 6.1.2.1. End-User Guidelines for Password Security in the MySQL reference manual.,Making statements based on opinion; back them up with references or personal experience., Are there any gaps in the range of gravitational wave frequencies we can detect? , Questions

Store your password in an option file. For example, on Unix, you can list your password in the [client] section of the .my.cnf file in your home directory:

[client]
password = your_pass

To keep the password safe, the file should not be accessible to anyone but yourself. To ensure this, set the file access mode to 400 or 600. For example:

shell > chmod 600.my.cnf

To name from the command line a specific option file containing the password, use the --defaults-file=file_name option, where file_name is the full path name to the file. For example:

shell > mysql--defaults - file = /home/francis / mysql - opts
load more v
40%

It’s important to make frequent automated backups of your MySQL databases should you ever accidentally alter data or suffer a hack. In this guide we will learn how to use mysqldump to export databases and use crontab to automate the entire process.,cron is a service in Linux used to schedule automated commands. These are stored in a cron table called crontab.,The mysqldump client utility performs logical backups, producing a set of SQL statements that can be executed to reproduce the original database object definitions and table data. It dumps one or more MySQL databases for backup or transfer to another SQL server.,Scroll to the bottom of the file and add your cron schedule and mysqldump command. In the example below, we are backing up a database daily with gzip compression. Gzip will reduce the size of large .sql files suitable for storing backups.

Begin by creating your backup folder.

sudo mkdir /
   var / www_backups /

If you’re not currently logged in to Linux as root, you should change the owner of the backup folder otherwise your mysqldump tests will fail with a permissions error. $(whoami) will fetch the currently logged in user. You can always change the owner back to root once you’ve finish testing commands.

sudo chown $(whoami): $(whoami) /
   var / www_backups

Here’s the syntax of a typical mysqldump command.

mysqldump - u[username]p[password][database_name] > /path/to / [database_name].sql

We can use the --all-databases option to dump all MySQL databases. In the following example, we are dumping all databases to /var/www_backups/ using the root user. The dump file is called all-databases.sql but you can change this to whatever you want. Usually you will need to use the root MySQL account to gain access to all databases.

mysqldump - u root - p[password]--all - databases > /var/www_backups / all - databases.sql

To back up a single MySQL database, simply replace [database_name] with your own. The dump file in this example is called [database_name].sql but you can change this to whatever you want. We are using the root user in this example, though you could use any user that has access to that particular database.

mysqldump - u root - p[password][database_name] > /var/www_backups / [database_name].sql

To back up more than one MySQL database, use the --databases option followed by the database names, separated by a space. The dump file in this example is called [database_names].sql but you can change it to whatever you want.

mysqldump - u root - p[password][database_1_name][database_2_name] > /var/www_backups / [database_names].sql

To back up a single table from a MySQL database, simply enter the table name after the database name. The dump file in this example is called [table_name].sql but you can change it to whatever you want.

mysqldump - u root - p[password][database_name][table_name] > /var/www_backups / [table_name].sql

To back up multiple tables from a MySQL database, simply enter the table names after the database name, separated by a space. The dump file in this example is called [table_names].sql but you can change this to whatever you want.

mysqldump - u root - p[password][database_name][table_1_name][table_2_name] > /var/www_backups / [table_names].sql

To back up a remote MySQL database, simply use the -h option followed by the remote IP address or host name.

mysqldump - h[ip_or_hostname] - u root - p[password][database_name] > /var/www_backups / [database_name].sql

To back up a MySQL database with compression, we can ‘pipe’ the output to gzip using | gzip -c >.

mysqldump - u root - p[password][database_name] | gzip - c > /var/www_backups / [database_name].sql.gz

Use mysql to restore .sql files to the database. Here’s the syntax of a typical MySQL restore.

mysql - u[username]p[password][database_name] < /path/to / [database_name].sql

To open crontab, run:

sudo crontab - e
00 01 * * * mysqldump - u root - p[password][database_name] | gzip - c > /var/www_backups / [database_name].sql.gz

Wait for your test cron to run and list the backup folder until you see your .sql.gz file.

ls - l /
   var / www_backups /
   -rw - r--r--1 root root 66 M Dec 3 16: 33[database_name].sql.gz

If your .sql.gz file doesn’t appear after a while, make sure your command and crontab time are correct. You can also check the cron log with:

sudo grep CRON /
   var / log / syslog

The default crontab entry begins with 5 stars followed by a command, which will run once a minute. You can change these to suit your exact schedule by minute, hour, day of month, month, and day of week.

.-- -- -- -- -- --minute(0 - 59)( * = every minute) |
   .-- -- -- -- --hour(0 - 23)( * = every hour) |
   | .-- -- -- --day of month(1 - 31)( * = every day) |
   | | .-- -- --month(1 - 12 or jan - dec)( * = every month) |
   | | | .-- --day of week(0 - 6 or mon - sun)(Sunday = 0)( * = every day) |
   | | | |
   *
   * * * * command_to_run

Examples:

30 23 * * * Every day at 11.30 pm
0 0 * * * Every day at midnight(00: 00) *
   /10 * * * *      Every 10 mins
0 * /12 * * *      Every 12 hours 
0 17 * * sun Every Sunday at 5 pm
0 17 * * sun, mon Every Sunday and Monday at 5 pm
0 5, 17 * * * At 5 am and 5 pm daily
0 12 1 jan, feb * At 12 pm on the 1 st of every Jan and Feb
0 0 1 * * The 1 st day of every month at midnight

Let’s test the date variable using echo. To echo the current date in the format YYYY-MM-DD:

echo `date +%Y-%m-%d`
2018 - 12 - 07

To get the number of the day of the week (0 to 6, where 0 is Sunday and 6 is Saturday)

echo `date +%w`
2

You can also echo the name of day of the week:

echo `date +%a`
Tue
00 01 * * * mysqldump - u root - p[password][database_name] | gzip - c > /var/www_backups / [database_name].sql.gz
00 01 * * * mysqldump - u root - p[password][database_name] | gzip - c > /var/www_backups / [database_name].
`date +\%a`.sql.gz

Here is a list of our folder showing the DevAnswers’ WordPress database backup for each day of the week. Older backups are overwritten automatically.

ls - l /
   var / www_backups /
   -rw - r--r--1 root root 69 M Dec 3 01: 00 wp_devanswers.Mon.sql.gz -
   rw - r--r--1 root root 70 M Dec 4 01: 00 wp_devanswers.Tue.sql.gz -
   rw - r--r--1 root root 70 M Dec 5 01: 00 wp_devanswers.Wed.sql.gz -
   rw - r--r--1 root root 70 M Dec 6 01: 00 wp_devanswers.Thu.sql.gz -
   rw - r--r--1 root root 72 M Dec 7 01: 00 wp_devanswers.Fri.sql.gz -
   rw - r--r--1 root root 73 M Dec 8 01: 00 wp_devanswers.Sat.sql.gz -
   rw - r--r--1 root root 73 M Dec 9 01: 00 wp_devanswers.Sun.sql.gz
0 0 1 * * mysqldump - u root - p[password][database_name] | gzip - c > /var/www_backups / [database_name].
`date +\%w`.sql.gz

Here is a list of our backup folder showing the monthly backup. As the years roll on, the older sql.gz files will simply be overwritten.

ls - l /
   var / www_backups /
   -rw - r--r--1 root root 69 M Jan 1 00: 00 wp_devanswers.Jan.sql.gz -
   rw - r--r--1 root root 70 M Feb 1 00: 00 wp_devanswers.Feb.sql.gz -
   rw - r--r--1 root root 70 M Mar 1 00: 00 wp_devanswers.Mar.sql.gz -
   rw - r--r--1 root root 70 M Apr 1 00: 00 wp_devanswers.Apr.sql.gz -
   rw - r--r--1 root root 72 M May 1 00: 00 wp_devanswers.May.sql.gz -
   rw - r--r--1 root root 73 M Jun 1 00: 00 wp_devanswers.Jun.sql.gz -
   rw - r--r--1 root root 73 M Jul 1 00: 00 wp_devanswers.Jul.sql.gz
sudo nano~/.my.cnf
[mysqldump]
password = YOUR_PASSWORD_HERE
sudo chmod 600~/.my.cnf
mysqldump - u root[database_name] | gzip - c > /var/www_backups / [database_name].sql.gz
0 01 * * * mysqldump--defaults - extra - file = /home/username / .my.cnf - u root[database_name] | gzip - c > /var/mysql_backups / [database_name].sql.gz
load more v
22%

Cant i jast edit the syntax from the crontab -e : * 2 * * * /opt/bitnami/mysql/bin/mysqldump -u root -password--databases BDname>~/Backup/DBname – tuneonlinux Oct 6 '17 at 18:16 , sorry i don't understand, – tuneonlinux Oct 6 '17 at 18:16 ,You would get a more manageable operation if you put the dump command into its own script and scheduled that. This crontab entry is a borderline case of when something gets slightly too "not simple" for a cron job. ,Personal Finance & Money

You could simply write to a date-stamped dump file:

* 2 * * * /opt/bitnami / mysql / bin / mysqldump - u root - password--databases BDname > "$HOME/Backup/DBname_$(date +\%F).sql"
load more v
60%

To export all of the databases in MariaDB using mysqldump, the following would be entered from the filesystem command-line:,To export only one database and not all, enter something like the following from the command-line:, Restoring Data from Dump Files ,The only significant difference in this line is that the -A option has been replaced with -B and the database to be exported has been given. To export multiple databases, just enter them after the -B option, separated by spaces (e.g., -B db1 db2).

To export all of the databases in MariaDB using mysqldump, the following would be entered from the filesystem command-line:

mysqldump - u admin_backup - p - x - A > /data/backup / dbs.sql
load more v
48%

Description:The Vixie-Cron package is the main program of cron;The CRONTABS package is a program for installing, unloading, or enumerating the table to drive the cron daemon.Cron is Linux's built-in service, but it does not automatically, you can start with the following method, close this service:,   The crontab command is used to set the periodic executed instructionSimilar to the scheduled task under Windows, when the operating system is installed, the service tool is installed by default, an...,note: MySQLDUMP will lock on the backup, if in the production environment, it will cause the business to rush. How to solve it?,In MySQL, the maximum advantage of logical backup is that various storage engines can be backed up with the same method, using mysqldump to back up data in the database as a text file, which can be vi...

yum install vixie - cron

yum install crontabs
load more v

Other "closed-undefined" queries related to "Mysqldump and crontab(linux) [closed]"