同步与备份用脚本
Posted sjg20010414
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了同步与备份用脚本相关的知识,希望对你有一定的参考价值。
php脚本,ftp将远程主机文件同步到本地
<?php
$remoteServer = 'xxxxx';
$remoteBaseDir = '/C:/wamp/apps/nbjbyb_tracesys';
$username = 'xxxx';
$password = 'xxxxx';
$conn = ftp_connect($remoteServer);
if (!$conn) exit("Connect failed\\n");
ftp_login($conn, $username, $password) or exit("Login failed\\n");
ftp_pasv($conn, 1); // 远程服务器要求被动模式
$filePath = '/data_sqlite/yii2egg.sqlite';
echo date('Y-m-d H:i:s')."\\t";
if (false && filemtime('.' . $filePath) > strtotime('today')) { // false 总是更新
echo "$filePath updated today\\n";
} else {
ftp_get($conn, '.' . $filePath, $remoteBaseDir . $filePath, FTP_BINARY)
or exit("Download $filePath failed\\n");
echo "Download $filePath OK\\n";
}
$imageDirs = [
'/web/images/pic_cyslqd',
'/web/images/pic_gysyyzz',
'/web/images/pic_jdsjt',
'/web/images/pic_jdyyzz',
'/web/images/pic_pckjbg',
'/web/images/pic_photo',
'/web/images/pic_spzs',
'/web/images/pic_wjbg',
];
$cnt = 0;
foreach ($imageDirs as $imageDir) {
$localNames = scandir('.' . $imageDir);
array_shift($localNames);
array_shift($localNames);
// echo "--------local $imageDir -------------------------\\n";
// foreach ($localNames as $localName) echo $localName."\\n";
// echo "--------------------------------------------------\\n\\n";
$remoteNames = ftp_nlist($conn, $remoteBaseDir . $imageDir);
// echo "--------remote $imageDir -------------------------\\n";
// foreach ($remoteNames as $remoteName) echo $remoteName."\\n";
// echo "--------------------------------------------------\\n\\n";
$diffs = array_diff($remoteNames, $localNames); // 找出本地没有的(差集)
foreach ($diffs as $diff) {
echo date('Y-m-d H:i:s')."\\t";
if (ftp_get($conn, '.' . $imageDir . "/$diff", $remoteBaseDir . $imageDir . "/$diff", FTP_BINARY)) {
echo "Download $imageDir/$diff OK\\n";
++$cnt;
} else
echo "Download $imageDir/$diff FAILED!!!\\n";
}
}
echo "\\n\\n$cnt image files downloaded\\n";
ftp_close($conn);
shell脚本,将远程主机备份数据库同步到本地
#!/bin/bash
today=`date "+%Y%m%d"`
db1=gxlq_pig
fromfile=$db1$today.sql.gz
sourcefile=$db1$today.sql
if [ ! -f "/home/zime/$sourcefile" ]; then
rm /home/zime/gxlq_pig*.sql
scp user@server.com:/home/user/$fromfile /home/zime
gunzip $fromfile
fi
mysql -uroot -pjsjxjf <<EOF
drop database gxlq_pig;
create database gxlq_pig;
use gxlq_pig;
source /home/zime/$sourcefile;
EOF
备份mariadb数据库shell脚本
#!/bin/bash
dayofweek=`date "+%u"`
today=`date "+%Y%m%d"`
db1=dbname
user1=username
pwd1=password
dir=/home/sjg/
# 请将此脚本加入 cron,每天凌晨2点执行
cd $dir
#if [ $dayofweek -eq 1 ]; then
if [ ! -f "$db1$today.sql.gz" ]; then
mysqldump -u$user1 -p$pwd1 $db1 | gzip > $db1$today.sql.gz
fi
#fi
# 以上每周一备份
清理较旧的备份php脚本
<?php
$dir = '/home/sjg';
$files = scandir($dir, 1); // reverse order
$reserve = 5; // reserve 5 backup at most
$i = 0;
foreach($files as $k => $file) {
if (preg_match('/^gxlq_pig\\d{8}\\.sql\\.gz$/', $file, $matches)) {
++$i;
if ($i > $reserve) {
@unlink($dir.'/'.$matches[0]);
}
}
}
以上是关于同步与备份用脚本的主要内容,如果未能解决你的问题,请参考以下文章
web服务文件更新自动同步数据库主从复制shell脚本实现网站代码备份和mysql备份
实战案例:用rsync+inotify+shell脚本实现/www目录实时同步