我正在使用 rsync 将更改从远程服务器 (MT DV4.0) 克隆到本地服务器 (ubuntu 11.10)。然后我使用以下复制/粘贴脚本来压缩并存档文件。
#!/bin/bash
####################################
#
# Backup to NFS mount script with
# grandfather-father-son rotation.
#
####################################
# What to backup.
backup_files="/home/server-backups/var/www/vhosts /home/server-backups/var/www/vhosts"
# Where to backup to.
dest="/home/bryan/server-tar"
# Setup variables for the archive filename.
day=$(date +%A)
hostname=$(hostname -s)
# Find which week of the month 1-4 it is.
day_num=$(date +%d)
if (( $day_num <= 7 )); then
week_file="$hostname-week1.tgz"
elif (( $day_num > 7 && $day_num <= 14 )); then
week_file="$hostname-week2.tgz"
elif (( $day_num > 14 && $day_num <= 21 )); then
week_file="$hostname-week3.tgz"
elif (( $day_num > 21 && $day_num < 32 )); then
week_file="$hostname-week4.tgz"
fi
# Find if the Month is odd or even.
month_num=$(date +%m)
month=$(expr $month_num % 2)
if [ $month -eq 0 ]; then
month_file="$hostname-month2.tgz"
else
month_file="$hostname-month1.tgz"
fi
# Create archive filename.
if [ $day_num == 1 ]; then
archive_file=$month_file
elif [ $day != "Saturday" ]; then
archive_file="$hostname-$day.tgz"
else
archive_file=$week_file
fi
# Print start status message.
echo "Backing up $backup_files to $dest/$archive_file"
date
echo
# Backup the files using tar.
tar czf $dest/$archive_file $backup_files
# Print end status message.
echo
echo "Backup finished"
date
# Long listing of files in $dest to check file sizes.
ls -lh $dest/
每日备份似乎运行良好,但我只获得 6 次每日备份。缺少星期六(这应该是每周备份运行的日子)。每月备份也丢失了。
我是一名服务器管理员业余爱好者,因此非常感谢您的帮助。谢谢!
答案1
您的脚本似乎运行良好,但有点难以理解;我已经删除了一些小错误(无法在测试中使用 ==,它是 =)并使用更简单的解决方案删除了级联 if;它现在可以与普通 sh 一起使用。
#!/bin/sh
####################################
#
# Backup to NFS mount script with
# grandfather-father-son rotation.
#
####################################
# What to backup.
backup_files="/home/server-backups/var/www/vhosts /home/server-backups/var/www/vhosts"
# Where to backup to.
dest="/home/bryan/server-tar"
# Setup variables for the archive filename.
day=$(date +%A)
hostname=$(hostname -s)
# Find which week of the month 1-4 it is.
day_num=$(date +%d)
# Integer arithmetic to replace cascading ifs, but now can have 5th week
week_file="$hostname-week$(expr $day_num / 7 + 1).tgz"
# Find if the Month is odd or even.
month_num=$(date +%m)
month_file="$hostname-month$(expr 2 - $month_num % 2).tgz"
# Create archive filename.
if [ $day_num = 1 ]; then
archive_file=$month_file
elif [ $day != Saturday ]; then
archive_file="$hostname-$day.tgz"
else
archive_file=$week_file
fi
# Print start status message.
echo "Backing up $backup_files to $dest/$archive_file"
date
echo
# Backup the files using tar.
tar czf $dest/$archive_file $backup_files
# Print end status message.
echo
echo "Backup finished"
date
# Long listing of files in $dest to check file sizes.
ls -lh $dest/
实际问题是什么?今天(星期六)运行时,我发现它尝试创建一个名为 /home/bryan/server-tar/pegasus-week2.tgz 的 tar 文件。
您是否对周六没有 tarfile 感到惊讶?如果您想要一个,您可以进行一些链接,因为它与 $hostname-week${x}.tar 相同
您能提供 /home/bryan/server-tar 的 ls 吗?
[变化的差异在于http://www.bayofrum.net/~crees/patches/sf-469087-1.diff]
答案2
抱歉,我迟到了。也许你的“day”变量在终端中显示不同?
elif [ $day != Saturday ]; then
到
elif [ $day != saturday ]; then
这是日期的一个小变化。你可以在终端中看到日期的样子:
echo day=$(date +%A)