Hello.
A lot of words have already been said about the importance of
regular backup. In this article, we offer readers examples of simple scripts for backing up files and MySQL databases, followed by uploading archives to a remote FTP server.
Despite the fact that we at
NQhost offer solutions for saving snapshots of VPS containers, the process of back-ups on our own is certainly the most important thing.
Household
A virtual or physical server with a Linux-OS installed, a web server and MySQL databases.
Web server files are located in directories.
/ home / site1
/ home / site2
/ home / site3
Task
Creating a script to back up files and databases to save on a remote FTP-server and run it every day.
')
Decision
For simplicity of the example, we will work as root, the directory for storing file backups is / root / backup / server, and for MySQL dumps, / root / backup / mysql
Backup files
Here is an example of a script for backup files, for clarity, explanations are given in square brackets in Russian.
#!/bin/sh
### System Setup ###
BACKUP=/root/backup/server
### FTP ###
FTPD="/"
FTPU="username" [ () ftp-c]
FTPP="megapassword" [ ftp-]
FTPS="my_remote_backup.ru" [, ftp- IP]
### Binaries ###
TAR="$(which tar)"
GZIP="$(which gzip)"
FTP="$(which ftp)"
## Today + hour in 24h format ###
NOW=$(date +%Y%m%d) [ , server-YYYYMMDD.tar.gz]
### Create tmp dir ###
mkdir $BACKUP/$NOW
$TAR -cf $BACKUP/$NOW/etc.tar /etc [c /etc ]
$TAR -cf $BACKUP/$NOW/site1.tar /home/site1/
$TAR -cf $BACKUP/$NOW/site2.tar /home/site2/
$TAR -cf $BACKUP/$NOW/site2.tar /home/site3/
ARCHIVE=$BACKUP/server-$NOW.tar.gz
ARCHIVED=$BACKUP/$NOW
$TAR -zcvf $ARCHIVE $ARCHIVED
### ftp ###
cd $BACKUP
DUMPFILE=server-$NOW.tar.gz
$FTP -n $FTPS <<END_SCRIPT
quote USER $FTPU
quote PASS $FTPP
cd $FTPD
mput $DUMPFILE
quit
END_SCRIPT
### clear ###
rm -rf $ARCHIVED
The result of this script will be the file created in the / root / backup / server directory of the type server-YYYYMMDD.tar.gz containing the tar-archives of the / etc, / home / site1, / home / site2 and / home / site3 directories
The same file will be uploaded to the FTP server, which we indicated at the beginning of the script.
Backup databases MySQL
With this script we unload the MySQL databases (we do the so-called “dumps”). Each database is unloaded into a separate file.
#!/bin/sh
# System + MySQL backup script
### System Setup ###
BACKUP=/root/backup/mysql
### Mysql ### [ MySQL]
MUSER="root"
MPASS="megapassword"
MHOST="localhost"
### FTP ###
FTPD="/"
FTPU="username" [ () ftp-c]
FTPP="megapassword" [ ftp-]
FTPS="my_remote_backup.ru" [, ftp- IP]
### Binaries ###
TAR="$(which tar)"
GZIP="$(which gzip)"
FTP="$(which ftp)"
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
## Today + hour in 24h format ###
NOW=$(date +%Y%m%d)
### Create temp dir ###
mkdir $BACKUP/$NOW
### name Mysql ###
DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')"
for db in $DBS
do
### ###
mkdir $BACKUP/$NOW/$db
FILE=$BACKUP/$NOW/$db/$db.sql.gz
echo $i; $MYSQLDUMP --add-drop-table --allow-keywords -q -c -u $MUSER -h $MHOST -p$MPASS $db $i | $GZIP -9 > $FILE
done
ARCHIVE=$BACKUP/mysql-$NOW.tar.gz
ARCHIVED=$BACKUP/$NOW
$TAR -zcvf $ARCHIVE $ARCHIVED
### ftp ###
cd $BACKUP
DUMPFILE=mysql-$NOW.tar.gz
$FTP -n $FTPS <<END_SCRIPT
quote USER $FTPU
quote PASS $FTPP
cd $FTPD
mput $DUMPFILE
quit
END_SCRIPT
### clear ###
rm -rf $ARCHIVED
The result of the script is a file in the / root / backup / server directory of the type mysql-YYYYMMDD.tar.gz containing tar archives with dumps of all databases and uploading it to an FTP server.
Automation
We save these scripts to the /etc/cron.daily directory, having previously checked in the / etc / crontab file that it is from this directory that the scripts are run every day.
Conclusion
Of course, in each case, the scripts can vary and the examples given are just one of the many options for organizing backup.
We hope that after reading this article, you will think about your own backup solution, if for some reason you have not organized this crucial process.