Last update at :2024-03-26,Edit by888u
What is the most important thing about building a website? data! Data is the foundation of a website, and backup is something that every webmaster should pay attention to. But at the same time, backup is also a tedious and repetitive thing. Therefore, these things can definitely be automated. Let’s introduce this one-click backup script backup.sh.
To summarize the features of backup.sh: 1. Support full database backup of MySQL/MariaDB; 2. Support backup of specified directories or files; 3. Support encrypted backup files; 4. Supports one-click upload to Google Drive (gdrive needs to be installed and configured first)
Updated August 21, 2016: 1. New: Specify the database name of MySQL/MariaDB for backup, and you can specify multiple ones at the same time; 2. New: Delete old local backup files for a specified number of days.
Tutorial mode on:
1. Download the script and grant execution permission
wget https://github.com/teddysun/across/raw/master/backup.sh chmod +x backup.sh2. Modify and configure the script Please use vim or nano and other tools to modify.
Some notes on variable names: ENCRYPTFLG (encrypted FLG, true means encryption, false means no encryption, the default is encryption) BACKUPPASS (encrypted password, important, must be changed) LOCALDIR (backup directory, you can specify it yourself) TEMPDIR (temporary directory of the backup directory, you can specify it yourself) LOGFILE (path to the log file generated by running the script) MYSQL_ROOT_PASSWORD (root user password for MySQL or MariaDB) MYSQL_DATABASE_NAME (Specify the MySQL database name, leave it blank to back up all databases) ※ MYSQL_DATABASE_NAME is an array variable and multiple variables can be specified. Examples are as follows:
MYSQL_DATABASE_NAME[0]="phpmyadmin" MYSQL_DATABASE_NAME[1]="test"BACKUP (list of specified directories or files that need to be backed up, leaving blank means no directories or files will be backed up) ※ BACKUP is an array variable and multiple variables can be specified. Examples are as follows:
BACKUP[0]="/data/www/default/test.tgz" BACKUP[1]="/data/www/default/test/" BACKUP[2]="/data/www/default/test2/"LOCALAGEDAILIES (specify how many days after which old local backup files will be deleted, the default is 7 days)
Explanation of some precautions: 1) The script needs to be executed by the root user; 2) The script needs openssl for encryption, please install it in advance; 3) The script backs up all databases by default (full backup); 4) The decryption command of the backup file is as follows:
openssl enc -aes256 -in [ENCRYPTED BACKUP] -out decrypted_backup.tgz -pass pass:[BACKUPPASS] -d -md sha15) After the backup file is decrypted, the decompression command is as follows:
tar -zxPf [DECRYPTION BACKUP FILE]Explain the parameter -P: By default, tar compressed files have relative paths. Adding -P is so that tar can compress files with absolute paths. Therefore, you must also bring a -P parameter when decompressing.
3. Configure gdrive command gdrive is a command line tool used for uploading and downloading operations on Google Drive. Official website: https://github.com/prasmussen/gdrive
Of course, you can use the following command to install gdrive.
x86_64 (64-bit):
wget -O /usr/bin/gdrive http://dl.teddysun.com/files/gdrive-linux-x64 chmod +x /usr/bin/gdrivei386 (32-bit):
wget -O /usr/bin/gdrive http://dl.teddysun.com/files/gdrive-linux-386 chmod +x /usr/bin/gdriveThen, run the following command to start obtaining authorization:
gdrive listAccording to the prompts, use a browser to open the URL given by gdrive, click Accept, and then paste the string displayed on the browser back into the command line to complete the authorization.
4. Run the script to start backup
./backup.shThe script will display the backup progress by default and calculate the required time at the end. If you want to add the script to cron to run automatically, you don't need to display the backup progress in the foreground, just write the log. At this time you need to slightly change the log function in the script.
log() { echo "$(date "+%Y-%m-%d %H:%M:%S")" "$1" echo -e "$(date "+%Y-%m-%d %H:%M:%S")" "$1" >> ${LOGFILE} }Change to:
log() { echo -e "$(date "+%Y-%m-%d %H:%M:%S")" "$1" >> ${LOGFILE} }For information on how to use cron for automatic backup, please read the article dedicated to cron backup
This article is reproduced from: One-click backup script backup.sh
Recommended site searches: Singapore server, Hong Kong vps, dual-line host rental, webmaster IP, registered domain name, how to register a domain name, mainland China registration-free space, PHP space rental, Chinese domain name, free virtual host,
发表评论