Linux automatically backs up websites and databases to another server and creates scheduled tasks for the current user.
The two servers are 111 and 117, and the website and database on the 111 server are automatically backed up to the 117 server every day.
1: The current user is on my 111 server: sx
A: view the scheduled tasks of the current user: crontab-l is used to view the tasks of the current user. vi crontab-e is used to create A scheduled task for the sxw user.
Line 1: run the/home/sxw/rsync201.sh shell script at every day,
Line 2: And line 3 run the tar package, gaokaotong and phpcms files at seven o'clock P.M. every day.
Row 3: run the/homesxw/back_up.sh shell script at 07:22 every day,
Note: This is the current user sxw, so the shell script path must have the user sxw permission. Here, the/home/sxw/back_up shell script is the home directory of the current user sxw.
B: 111/home/sxw/back_up.sh script on the server
Code:
-Bash-3.2 $ vim/home/sxw/back_up.sh
#! /Bin/sh
Echo "Start database backup"
Mysqldump-u jeecms-pjeecms1234 gktcms> gktcms. SQL
Mysqldump-u jeecms-pjeecms1234 phpcms> phpcms. SQL
Echo "the database is backed up and copied to 117 server"
Scp-P 10022 gktcms. SQL zk@114.113.145.117:/home/zk
Scp-P 10022 phpcms. SQL zk@114.113.145.117:/home/zk
Scp-r-P 10022/opt/www/www2/phpcms.tar.gz zk@114.113.145.117:/home/zk/
Scp-r-P 10022/opt/www/www2/gaokaotong.tar.gz:/home/zk/
Echo "sent successfully"
C: You need to perform ssh trust on 111. For details, refer to ssh information. It's easy. You only need to create two keys.
2: On the 117 server,/homezk/will automatically generate two more tarpackages, phpcms.tar.gz and gaokaotong.tar.gz
Two files, phpcms.tar.gz and gaokaotong.tar.gz, are automatically saved every day.
A shell script is run to save the files from the previous two days. It is the same as creating a scheduled task on the above 111 Server:
A: View 117 scheduled tasks: crontabl-l
B: Compile the command in the/home/zk/back. sh shell.
Code:
[Zk @ DB2 ~] $ Vi/home/zk/back. sh
#! /Bin/sh
TARFILE = "/home/zk/phpcms.tar.gz"
If [-f $ TARFILE]; then
Mv/home/zk/phpcms.tar.gz/home/zk/phpcms _ $ (date when policy?m=d=.tar.gz
# Remove date file
DATE_ B = $ (date-d "2 day ago" + % Y % m % d)
FILENAME = "/home/zk/phpcms _" $ DATE_ B ".tar.gz"
If [-f $ FILENAME]; then
Rm-rf/home/zk/phpcms_1_date_ B .tar.gz
Else
Echo "no file can be deleted"
Fi
Else
Echo "no source file"
Fi
~
End.