将vps中home/web目录打包并传到指定VPS的home目录
1.手动备份迁移
按时间戳打包
cd /home/ && tar czvf web_$(date +%Y%m%d%H%M%S).tar.gz web
传输最新的tar压缩包到其他VPS
cd /home/ && ls -t /home/*.tar.gz | head -1 | xargs -I {} scp {} [email protected]:/home/
只保留3个压缩包
cd /home/ && ls -t /home/*.tar.gz | tail -n +4 | xargs -I {} rm {}
远端机器解压最新tar文件
cd /home/ && ls -t /home/*.tar.gz | head -1 | xargs -I {} tar -xzf {}
2.自动备份迁移
下载sh脚本
apt update -y && apt install -y wget sudo sshpass
cd /home
wget beifen.sh https://raw.githubusercontent.com/kejilion/sh/main/beifen.sh
chmod +x beifen.sh
nano beifen.sh
备份脚本如下:
#!/bin/bash
# Create a tar archive of the web directory
cd /home/ && tar czvf web_$(date +”%Y%m%d%H%M%S”).tar.gz web
# Transfer the tar archive to another VPS
cd /home/ && ls -t /home/*.tar.gz | head -1 | xargs -I {} sshpass -p 123456 scp -o StrictHostKeyChecking=no -P 22 {} [email protected]:/home/
# Keep only 5 tar archives and delete the rest
cd /home/ && ls -t /home/*.tar.gz | tail -n +4 | xargs -I {} rm {}
运行sh脚本
./beifen.sh
定时任务
(crontab -l; echo “0 2 * * * /home/backup.sh”) | crontab –
3.注意
如果远端VPS重装系统了或是密码更改了。需要将之前连接的认证清除掉!
ssh-keygen -f “/root/.ssh/known_hosts” -R “0.0.0.0”
0.0.0.0替换之前VPS的IP,清除认证!
(by 科技lion)转载于25.09.2023