All checks were successful
Gitea Actions Official-website / deploy-dev (push) Successful in 3s
4.7 KiB
4.7 KiB
图片迁移脚本快速开始指南
概述
这是一个通过SSH将本地图片迁移到远程服务器的Python脚本。支持保持相对路径结构,自动创建目录,批量处理图片文件。
环境要求
- Python 3.6+
- paramiko库 (SSH客户端库)
快速开始
1. 安装依赖
cd /var/www/html/orico-official-website/scripts
pip install -r requirements.txt
2. 创建配置文件
# 创建示例配置文件
python image_migrate.py --create-config config.json
编辑生成的 config.json 文件,填写目标服务器信息:
- 修改
target.ssh部分的目标服务器地址、用户名、密码/密钥 - 设置
source.base_dir为本地图片目录路径
3. 准备图片路径列表
创建 images.txt 文件,每行一个相对路径:
uploads/products/2024/01/product1.jpg
uploads/products/2024/01/product2.jpg
assets/images/logo.png
uploads/banners/main-banner.jpg
4. 执行迁移
python image_migrate.py --config config.json --input images.txt --verbose
配置文件示例(本地源)
{
"source": {
"type": "local",
"base_dir": "/var/www/html/orico-official-website/public/uploads"
},
"target": {
"type": "ssh",
"base_dir": "/var/www/html/images",
"ssh": {
"host": "your-server.com",
"port": 22,
"username": "deploy",
"password": "your_password",
"key_file": "/path/to/private/key"
}
}
}
常用命令
基本迁移
python image_migrate.py --config config.json --input images.txt
详细输出模式
python image_migrate.py --config config.json --input images.txt --verbose
覆盖已存在的文件
python image_migrate.py --config config.json --input images.txt --overwrite
直接从命令行指定文件
python image_migrate.py --config config.json \
"uploads/test1.jpg" \
"uploads/test2.jpg" \
"assets/logo.png"
不使用配置文件,直接指定参数
python image_migrate.py \
--source-type local \
--source-dir /path/to/local/images \
--target-host server.example.com \
--target-user username \
--target-dir /remote/path/images \
--input images.txt \
--verbose
常见场景
场景1:迁移网站上传目录
# 配置文件中的源目录设置为:
"source": {
"type": "local",
"base_dir": "/var/www/html/orico-official-website/public/uploads"
}
# 执行迁移
python image_migrate.py --config config.json --input uploads_list.txt
场景2:从数据库导出路径并迁移
# 从数据库导出图片路径
mysql -u username -p database -e "SELECT image_path FROM products" --skip-column-names > products.txt
# 清理路径(如果需要)
sed -i 's/^\.\///' products.txt
# 迁移图片
python image_migrate.py --config config.json --input products.txt --verbose
场景3:迁移整个目录结构
# 使用find命令生成所有图片文件列表
find /path/to/local/images -type f \( -name "*.jpg" -o -name "*.png" -o -name "*.gif" -o -name "*.webp" \) | sed 's|^/path/to/local/images/||' > all_images.txt
# 迁移所有图片
python image_migrate.py --config config.json --input all_images.txt
路径说明
- 相对路径:相对于配置文件中指定的
base_dir - 示例:如果
base_dir是/var/www/html/images,相对路径uploads/test.jpg对应完整路径/var/www/html/images/uploads/test.jpg - 目标路径:在目标服务器上保持相同的相对路径结构
故障排查
1. 连接失败
# 测试SSH连接
ssh -p 22 username@server.example.com
# 检查配置文件中的主机、端口、用户名是否正确
2. 文件不存在
# 检查本地文件是否存在
ls -la /var/www/html/orico-official-website/public/uploads/uploads/test.jpg
# 使用详细模式查看完整路径
python image_migrate.py --config config.json --input images.txt --verbose
3. 权限不足
# 检查本地文件读取权限
ls -la /path/to/local/images
# 检查目标目录写入权限(通过SSH)
ssh username@server.example.com "ls -la /remote/path/images"
注意事项
- 建议使用SSH密钥认证而非密码(更安全)
- 首次使用前,建议用少量文件测试
- 大文件传输可能需要较长时间,建议分批处理
- 使用
--verbose参数可查看详细传输信息 - 脚本会自动创建目标服务器上的目录结构
获取帮助
# 查看完整帮助
python image_migrate.py --help
# 查看详细使用说明
cat README.md
快速开始指南更新日期:2024年 脚本位置:/var/www/html/orico-official-website/scripts/