Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Cron job
New on LowEndTalk? Please Register and read our Community Rules.

All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.

Cron job

How would I setup a Cron job on my debian Server to backup my MySQL database once a week?
Using the following

mysqldump -u admin -p journal > journal.sql

I can run it at the command line.

Comments

  • crontab -e

    0 2 * * 1 mysqldump -u admin -p journal > journal.sql

    This should run at 2:00am every monday.

    Thanked by 2melp57 crunchbits
  • JabJabJabJab Member
    edited July 2023

    apt-get install automysqlbackup

  • raindog308raindog308 Administrator, Veteran

    @ceplavia said:
    crontab -e

    0 2 * * 1 mysqldump -u admin -p journal > journal.sql

    Some improvements:

    (1) I'd use the full path to mysqldump because otherwise cron might not find it.

    (2) Also, before this like put

    MAILTO="[email protected]"

    to get email if the job fails.

    (3) journal.sql might not end up where you expect it to be, so I'd full path it

    (4) And you could get fancy with something like:

    0 2 * * 1 /usr/bin/mysqldump -u admin -p journal > /safe/place/journal.$(date '+%Y-%m-%d').sql

    which would put each backup in a separate file like

    journal.2023-07-10.sql

  • melp57melp57 Member

    @raindog308 said:

    @ceplavia said:
    crontab -e

    0 2 * * 1 mysqldump -u admin -p journal > journal.sql

    Some improvements:

    (1) I'd use the full path to mysqldump because otherwise cron might not find it.

    (2) Also, before this like put

    MAILTO="[email protected]"

    to get email if the job fails.

    (3) journal.sql might not end up where you expect it to be, so I'd full path it

    (4) And you could get fancy with something like:

    0 2 * * 1 /usr/bin/mysqldump -u admin -p journal > /safe/place/journal.$(date '+%Y-%m-%d').sql

    which would put each backup in a separate file like

    journal.2023-07-10.sql

    Do I include the password since I would not enter it when the Job runs.
    Thank you for the information!

  • raindog308raindog308 Administrator, Veteran

    @melp57 said: Do I include the password since I would not enter it when the Job runs.

    No you should use a .my.cnf file

    e.g., in /root/.my.cnf (be sure to set to mode 600)

    [client]
    user=root
    password="ILoveLowEndTalk"

    [mysql]
    user=root
    password="ILoveLowEndTalk"

    [mysqldump]
    user=root
    password="ILoveLowEndTalk"

    Maybe only the mysqldump section is needed for mysqldump.

  • might be handy for future use
    https://crontab.guru/

    Thanked by 1melp57
  • LeviLevi Member
    edited July 2023

    If db is bussy, it should be locked before dump to avoid half baked data in the tables. Boy, do I had pleasure to backup zabbix gygabytes of mysql crap...

    Thanked by 1melp57
  • melp57melp57 Member

    @ShadowLurker said:
    might be handy for future use
    https://crontab.guru/

    Very nice thanks!

Sign In or Register to comment.