There are a lot of important data in an application, in order to timely, quickly and accurately recover our application to a more appropriate location after the data disaster, we must regularly back up our data, today we are going to discuss the method of data backup under Linux. The tar command was originally designed for tape backups. It backs up files and directories to tape, and then extracts or restores files from tape. Now we can use tar to back up data to any storage medium. The format of the tar command to backup data is as follows: tar CVF backup.tar /home/www the meaning of the command is to package /home/www/ as a backup.tar file. Using this command, we can manually back up the data on the server, but for automatic and scheduled backups, we need to use the next trick. 2. Automatic backup of local data To implement automatic backup, we first need to write tar command in the script. For example, here is a script: #! /bin/sh tar ZCVF test.tar /home/www/ Save the file as test.sh and add the executable permission. The problem with Removing leading ‘/’ from member names is that the script is executed in the user directory by default. This is where the problem exists, and the solution is to switch the CD to the directory you want to package before executing tar. With a script that can execute commands, and one that can be executed on a regular basis, this is done with the help of scheduled tasks. Linux provides us with cron, a powerful tool that we can use to implement almost any type of timed triggering. Each user has their own cron profile, and generally we run cron tasks as root to ensure that we get enough permissions. If we want to run the script at 4am every day, we can edit the configuration file with crontab -e and add: 0 4 * * * /home/test.sh. The backup script will run automatically at 4am every day. \
Here is an example of a script that automatically backs up:
#! /bin/sh # aim to backup the resorces files and mysql data that used by shenti system # crontab run at 4:00AM every day # procedure: first backup all the data, then check if 3 days ago backup files exist, then delete prior # created by shiqiang at 2009-10-28 BACKDIR=”/home/backup/” LOGFILE=”/home/backup/shenti_backup_log” MYSQLBIN=”/etc/init.d/mysql” RESOURCEPATH=”/home/” # add the path var to avoid the ‘Removing leading / from member names tips’ RESOURCEDIR=”www/” MYSQLDATAPATH=”/var/lib/mysql/” MYSQLDATADIR_1=”wys/” MYSQLDATADIR_2=”wys2/” # cd $BACKDIR YEAR=`date +%Y` MONTH=`date +%m` DAY=`date +%d` echo “———————————–” >> $LOGFILE echo $(date +”%Y-%m-%d %H:%M:%S”)” Shenti backup begin” >> $LOGFILE DIRNAME=$BACKDIR$YEAR-$MONTH-$DAY FILENAME=$DIRNAME/$YEAR-$MONTH-$DAY-shenti-resorces-backup.tar # change dir to correspond folder if [ ! -d $DIRNAME ] then mkdir $DIRNAME fi # backup the resources folder cd $RESOURCEPATH tar zcvf $FILENAME $RESOURCEDIR >> $LOGFILE 2>&1 # backup the mysql data folder, we should be attion about the data consistency $MYSQLBIN stop >> $LOGFILE 2>&1 cd $MYSQLDATAPATH FILENAME=$DIRNAME/$YEAR-$MONTH-$DAY-mysqldata-wys-backup.tar tar zcvf $FILENAME $MYSQLDATADIR_1 >> $LOGFILE 2>&1 FILENAME=$DIRNAME/$YEAR-$MONTH-$DAY-mysqldata-wys2-backup.tar tar zcvf $FILENAME $MYSQLDATADIR_2 >> $LOGFILE 2>&1 $MYSQLBIN start >> $LOGFILE 2>&1 echo “Resources and mysql data backup finished at”$(date +”%Y-%m-%d %H:%M:%S”) >> $LOGFILE # check the old backup folder OLDBACKDIR=”$BACKDIR”$(date +%Y-%m-%d –date=’3 days ago’) if [ -d $OLDBACKDIR ] then `rm -rf $OLDBACKDIR` >> $LOGFILE 2>&1 echo ” [$OLDBACKDIR] Delete Old Backup Folder Success!” >> $LOGFILE else echo “No Old backup folders $OLDBACKDIR !” >> $LOGFILE fi echo “Shenti Backup finished at “$(date +”%Y-%m-%d %H:%M:%S”) >> $LOGFILE cd $BACKDIR\
In terms of security, it is not safe to store backup files on the host machine. In a catastrophic situation, this backup is very fragile, so we need to back up the data to another server. However, what we implement here is a two-machine timed backup, not a real-time backup. Because real-time backup (hot backup) is technically demanding, it is commercial grade. Dual-machine backup, we can back up the files on the local machine and transfer the backup files to other machines. But that’s not a good idea, and it’s not actually done that way. [1], but this also requires the use of third-party software, I think and is a very good way, because Linux at the same time there is a powerful tool SCP. At the same time, we can also consider using Rsync to synchronize data between the two servers, about the use of rsync, in this blog has been introduced, there is no more to say, I hope you can find their own data backup solution. Reference materials: 1. Backup method of data in Linux 2. An automatic backup script for Mysql 3