One-Click Backup Script: backup.sh

What is the most important thing when running a website? Data! Data is the foundation of a website, and backups are something every webmaster should take seriously. At the same time, backups are also tedious and repetitive. So naturally, these tasks can definitely be automated.
Below is an introduction to this one-click backup script, backup.sh.

Summary of backup.sh features:

1. Supports full database backups or selective backups for MySQL/MariaDB/Percona;
2. Supports backing up specified directories or files;
3. Supports encrypting backup files (optional; requires the openssl command to be installed);
4. Supports uploading to Google Drive (optional; requires gdrive to be installed and configured first);
5. Supports deleting old local backup files older than a specified number of days, while also deleting files with the same name on Google
Drive (optional).

Updated on August 21, 2016:
1. Added: backup by specifying MySQL/MariaDB
database names; multiple names can be specified at the same time;
2. Added: delete old local backup files older than a specified number of days.
Updated on September 8, 2016:
1. Added: while deleting old local backup files older than a specified number of days, optionally delete files with the same name on Google
Drive as well.
Updated on November 29, 2016:
1. Added: option to upload backup files to FTP;
2. Added: while deleting old local backup files older than a specified number of days, optionally delete files with the same name on Google
Drive and FTP as well.

Tutorial mode on:

1. Download the script and grant execute permission

wget --no-check-certificate https://github.com/teddysun/across/raw/master/backup.sh
chmod +x backup.sh

2. Edit and configure the script

Please use tools such as vim or nano to edit it.

Some notes about the variable names:

ENCRYPTFLG (encryption flag; true means encrypted, false
means not encrypted; encrypted by default)
BACKUPPASS (encryption password; important, be sure to change it)
LOCALDIR (backup directory; you can specify it yourself)
TEMPDIR (temporary directory for the backup directory; you can specify it yourself)
LOGFILE (path to the log file generated when the script runs)
MYSQL_ROOT_PASSWORD (root
user password for MySQL/MariaDB/Percona)
MYSQL_DATABASE_NAME
(specify the database name of MySQL/MariaDB/Percona; leave blank to back up all databases)
※ MYSQL_DATABASE_NAME
is an array variable and can specify multiple values. Examples are as follows:

MYSQL_DATABASE_NAME[0]="phpmyadmin"
MYSQL_DATABASE_NAME[1]="test"

BACKUP
(list of specified directories or files to back up; leave blank to skip backing up directories or files)
※ BACKUP is an array variable and can specify multiple values. Examples are as follows:

BACKUP[0]="/data/www/default/test.tgz"
BACKUP[1]="/data/www/default/test/"
BACKUP[2]="/data/www/default/test2/"

LOCALAGEDAILIES (specifies after how many days old local backup files are deleted; default is
7 days)
DELETE_REMOTE_FILE_FLG (the flag for deleting backup files on Google Drive or FTP;
true means delete, false means do not delete)

FTP_FLG (the flag for uploading files to FTP; true means upload, false
means do not upload)
FTP_HOST (the FTP domain name or IP address to connect to)
FTP_USER (the username for the FTP connection)
FTP_PASS (the password for the FTP user to connect with)
FTP_DIR (the remote directory on the FTP server, for example: public_html)

Notes and important points:

1) The script must be run as the root user;
2) The script uses openssl for encryption, so please install it in advance;
3) By default, the script backs up all databases (full backup);
4) The command to decrypt the backup file is as follows:

openssl enc -aes256 -in [ENCRYPTED BACKUP] -out decrypted_backup.tgz -pass pass:[BACKUPPASS] -d -md sha1

5) After decrypting the backup file, use the following command to extract it:

tar -zxPf [DECRYPTION BACKUP FILE]

Explanation of the -P parameter:
tar archives use relative paths by default. Adding -P allows tar
to archive files using absolute paths. Therefore, you also need to include the -P parameter when extracting.

3. Configure the gdrive command

gdrive is a command-line tool used for uploading, downloading, and other Google Drive
operations. Official website:
https://github.com/prasmussen/gdrive

Of course, you can also install gdrive using the following commands.

x86_64 (64-bit):

wget -O /usr/bin/gdrive http://dl.lamp.sh/files/gdrive-linux-x64
chmod +x /usr/bin/gdrive

i386 (32-bit)

wget -O /usr/bin/gdrive http://dl.lamp.sh/files/gdrive-linux-386
chmod +x /usr/bin/gdrive

Next, run the following command to begin authorization:

gdrive about

Follow the prompts to open the
URL provided by gdrive in your browser, click Accept, and then paste the string displayed in the browser back into the command line to complete authorization.

4. Run the script to start the backup

./backup.sh

By default, the script displays the backup progress and reports the total time required at the end.
If you want to add the script to cron
for automatic execution, then you do not need to display the backup progress in the foreground; writing logs is enough.
In that case, you need to slightly modify 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 it to:

log() {
    echo -e "$(date "+%Y-%m-%d %H:%M:%S")" "$1" >> ${LOGFILE}
}

As for how to use cron for automatic backups, I won’t go into that again here will use
CentOS 6 as an example.

Modify the file /etc/crontab as follows:

SHELL=/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
MAILTO=root
HOME=/root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name command to be executed
30  1  *  *  * root bash /root/backup.sh

The above means that the root user executes the backup.sh
script once every day at 1:30 a.m.
Note:
Be sure to modify the values of the PATH and HOME variables.
The HOME variable in particular—whether the gdrive
command can run correctly depends on its configuration file. If you use the root
configuration by default, its configuration directory should be /root/.gdrive/ , so you need to change
the value of HOME.

Finally, comments and suggestions are welcome.

Leave a Comment

Your email address will not be published. Required fields are marked *

中文 EN
🚀

RedGate VPN

免费节点太挤太慢?
升级高速稳定专线

立即体验 →

告别卡顿

RedGate VPN
全球高速节点

免费下载 →
Scroll to Top