The shell script automatically backs up the file and sends it to Gmail.

Source: Internet
Author: User

The shell script automatically backs up the file and sends it to Gmail.
1. Install the required Program

  1. Yum install-y mutt vixie-cron
2. Set the mutt sending Parameters
  1. $ Vim ~ /. Muttrc

Write the following content

  1. Set envelope_from = yes
  2. Set from = bak@diavps.com
  3. Set realname = "DiaBak"
  4. Set use_from = yes

Then set Gmail to receive the mail and add the bak@diavps.com to the White List.

3. Set scripts

Note these directories
/Etc/cron. hourly
/Etc/cron. daily
/Etc/cron. weekly
/Etc/cron. monthly
Files in these directories can be automatically executed every hour, every day, every week, and every month.
If you want to place the script program in the preceding directory, you 'd better set the permission to 0700.
You can use the crontab-e command to set more complex execution conditions. For more information, see Baidu.

Take daily execution as an Example

  1. $ Vim/etc/cron. daily/diabsk. sh

Write the following content

  1. #! /Bin/bash
  2. # SETTING
  3. TOEMAIL = "bak@diavps.com ";
  4. COMMENT = 'blog database backup'
  5. DIR = 'wordpress'
  6. # END SETTING
  7. TMP = '/tmp/diabak/' $ {DIR}
  8. ATTTMP = '/tmp/diabakatt/' $ {DIR}
  9. Rm-rf $ TMP
  10. Mkdir-p $ TMP
  11. Cd $ TMP
  12. # Put files what you want to backup to $ TMP
  13. # Don't change anything below
  14. YYYYMMDD = 'date + % Y % m % d'
  15. SUBJECT = 'diabak _ of _ '$ {DIR}' _ '$ {YYYYMMDD };
  16. Rm-rf $ ATTTMP
  17. Mkdir-p $ ATTTMP
  18. Cd $ ATTTMP
  19. Tar zcPf backup.tar.gz $ TMP
  20. Rm-rf $ TMP
  21. Split-B 20 m-a 3-d backup.tar.gz $ {SUBJECT}. part
  22. Rm-f backup.tar.gz
  23. For file in *
  24. Do
  25. Echo $ COMMENT | mutt-a $ file-s $ SUBJECT $ TOEMAIL
  26. Sleep 30 s
  27. Done
  28. Rm-rf $ ATTTMP

The content to be changed has been highlighted
TOEMAIL is the email address for receiving emails.
TOEMAIL is a comment email sent in the body of the email.
DIR is the temporary directory name. We recommend that you only use English letters and numbers.
# Put files what you want to backup to $ TMP. the following commands in this line can be supplemented as needed. For example, you can back up the database and export the SQL file to the $ TMP directory, backup files can be copied to the $ TMP directory.

4. Export database commands.

1. Back up a single database
Mysqldump -- user = user -- password = password -- lock-all-tables dbname> backup. SQL
2. Back up multiple databases
Mysqldump -- user = user -- password = password -- lock-all-tables -- databases dbname1 dbname2> backup. SQL
3. Back up all databases
Mysqldump -- user = user -- password = password -- lock-all-tables -- all-databases> backup. SQL

The -- lock-all-tables option can lock the database during backup to prevent changes, resulting in the synchronization of multiple related tables. For example, table A must reference table B, after Table A is backed up, table B is changed, and table B is backed up.
It takes only a few seconds to back up a database of hundreds of MB, so this option has little impact on website operation.

5. recombine backup files

Linux
$ Cat DiaBak *> bak.tar.gz
Windows
Copy DiaBak_of_testfile_20100421.part001/B + DiaBak_of_testfile_20100421.part002/B bak.tar.gz
Note that there will be three digits after the split, and the cat command will re-combine the files in the order of these digits, so the numbers cannot be messy.
The copy command is the same. Pay attention to the numerical order.

Vi. Others

I tested a file with a size of more than 600 mb and divided it into 31 packages at a size of 20 mb. The file was sent through a script and took 10 minutes. During this period, the network speed remained between 5 Mbps and 10 Mbps, the local machine still has enough bandwidth. This should be the GMAIL ceiling.
It may take more than an hour for some emails to be displayed in Gmail. All 31 packages are received.
If anyone finds a mailbox that is more suitable for backup than Gmail, I hope to share it with you.
Source: http://www.hostloc.com/thread-15597-1-1.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.