以下脚本配合计划任务可以实现文件的自动备份90天,可以根据实际情况自行修改。
计划任务:
crontab -e
0 3 * * * bash /root/backup.sh
shell脚本:路径一定要写对。
cat backup.sh
#!/bin/bash
# 备份源文件路径
source_files=(
"/usr/local/nginx/conf/nginx.conf"
"/usr/local/nginx/conf/tcp.stream"
"/zabbix/script/nginx_status.sh"
"/etc/zabbix/zabbix_agentd.d/check_keepalived.sh"
"/etc/zabbix/zabbix_agentd.d/keepalived.conf"
"/etc/zabbix/zabbix_agentd.conf"
"/etc/zabbix/zabbix_agentd.d/userparameter_nginx.conf"
"/etc/keepalived/keepalived.conf"
"/usr/local/src/check_nginx_pid.sh"
"/home/wwwroot/default/it/"
)
# 备份目标路径
backup_dir="/backup"
# 保留天数
retention_days=90
# 创建备份目录(如果不存在)
mkdir -p $backup_dir
# 备份文件
for source_file in "${source_files[@]}"; do
backup_file=$(basename $source_file).$(date +%Y%m%d%H%M%S)
cp -r $source_file $backup_dir/$backup_file
echo "备份成功: $backup_dir/$backup_file"
done
# 删除过期的备份文件
find $backup_dir -type f -mtime +$retention_days -exec rm {} \;
# 打印备份成功消息
echo "备份完成"
脚本写完后添加执行权限,并执行测试,测试成功后根据业务使用时间设定备份时间,不要在业务高峰期进行备份。