The shell script automatically backs up the file and sends it to Gmail.
1. Install the required Program
- Yum install-y mutt vixie-cron
2. Set the mutt sending Parameters
- $ Vim ~ /. Muttrc
Write the following content
- Set envelope_from = yes
- Set from = bak@diavps.com
- Set realname = "DiaBak"
- 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
- $ Vim/etc/cron. daily/diabsk. sh
Write the following content
- #! /Bin/bash
- # SETTING
- TOEMAIL = "bak@diavps.com ";
- COMMENT = 'blog database backup'
- DIR = 'wordpress'
- # END SETTING
- TMP = '/tmp/diabak/' $ {DIR}
- ATTTMP = '/tmp/diabakatt/' $ {DIR}
- Rm-rf $ TMP
- Mkdir-p $ TMP
- Cd $ TMP
- # Put files what you want to backup to $ TMP
- # Don't change anything below
- YYYYMMDD = 'date + % Y % m % d'
- SUBJECT = 'diabak _ of _ '$ {DIR}' _ '$ {YYYYMMDD };
- Rm-rf $ ATTTMP
- Mkdir-p $ ATTTMP
- Cd $ ATTTMP
- Tar zcPf backup.tar.gz $ TMP
- Rm-rf $ TMP
- Split-B 20 m-a 3-d backup.tar.gz $ {SUBJECT}. part
- Rm-f backup.tar.gz
- For file in *
- Do
- Echo $ COMMENT | mutt-a $ file-s $ SUBJECT $ TOEMAIL
- Sleep 30 s
- Done
- 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