如何防止tar覆盖现有档案? [英] How do I prevent tar from overwriting an existing archive?
问题描述
我每天在Ubuntu/Linux上使用命令 tar -cpvzf〜/Backup/backup_file_name.tar.gz directory_to_backup/
备份文件几次(文件名包含日期,格式为YYYY-MM-DD格式以及从a到z的字母-a是该日期的第一个备份,等等),但是我想创建一个新的归档文件,如果归档文件已经存在,则不要覆盖它.如何防止tar覆盖现有档案?如果存档存在,我希望tar不做任何事情就退出(如果可能,显示错误消息).
I backup files a few times a day on Ubuntu/Linux with the command tar -cpvzf ~/Backup/backup_file_name.tar.gz directory_to_backup/
, (the file name contains the date in YYYY-MM-DD format and a letter from a to z - a is the first backup for this date etc.) but I want to create a new archive, not overwrite the archive if it already exists. How do I prevent tar from overwriting an existing archive? If the archive exists, I want tar to exit without doing anything (and if possible, display an error message).
推荐答案
我创建了文件〜/scripts/tar.sh
:
#!/bin/bash
if [ -f $1 ]; then
echo "Oops! backup file was already here."
exit
fi
tar -cpvzf $1 $2 $3 $4 $5
现在我只需要输入:
~/scripts/tar.sh ~/Backup/backup_file_name_`date +"%Y-%m-%d"`_a.tar.gz directory_to_backup/
如果该文件不存在,则会创建备份文件.
And the backup file is created if the file doesn't exist.
这篇关于如何防止tar覆盖现有档案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!