简体   繁体   English

您知道我如何自动创建备份文件吗?

[英]Do you know how I can create backup files automatically?

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/ . 我每天在Ubuntu / Linux上使用命令tar -cpvzf ~/Backup/backup_file_name.tar.gz directory_to_backup/备份文件几次。 I want to create a script that will create the file name automatically - check: 我想创建一个将自动创建文件名的脚本-检查:

~/Backup/backup_file_name_`date +"%Y-%m-%d"`_a.tar.gz

if it exists, if it exists then replace "_a" with "_b" and then checks all the letters up to z. 如果存在,则将“ _a”替换为“ _b”,然后检查所有字母,直到z。 Create the first backup file that doesn't exist. 创建第一个不存在的备份文件。 If all the files up to z exist then add "_1" to the file name (with "_z") and check all the numbers until the file doesn't exist. 如果z之前的所有文件都存在,则在文件名中添加“ _1”(带有“ _z”)并检查所有数字,直到文件不存在为止。 Never change an existing file but only create new backup files. 切勿更改现有文件,而仅创建新的备份文件。 Do you know how to create such a script? 您知道如何创建这样的脚本吗?

You can do something like 你可以做类似的事情

for l in {a..z} ; do
    [[ -f ~/Backup/backup_file_name_`date +"%Y-%m-%d"`_${l}.tar.gz ]] && continue
    export backupname=-f ~/Backup/backup_file_name_`date +"%Y-%m-%d"`_${l}.tar.gz && break
done

# test if $backupname is properly set, what if `z` is used? I'm leaving this to you
# then backup as usual

tar -cpvzf $backupname directory_to_backup/

This iterates over the letters and if the required file exists skips setting the backupname variable. 这会遍历字母,如果所需文件存在,则跳过设置backupname变量的操作。

OK, I found a solution. 好的,我找到了解决方案。 I created the file ~/scripts/backup.sh : 我创建了文件~/scripts/backup.sh

#!/bin/bash

working_dir=`pwd`
backupname=""

if [ -z "$backupname" ]; then
    for l in {a..z} ; do
    if [ ! -f ~/Backup/backup_file_name_`date +"%Y-%m-%d"`_${l}.tar.gz ]; then
        backupname=~/Backup/backup_file_name_`date +"%Y-%m-%d"`_${l}.tar.gz
        break
    fi
    done
fi

if [ -z "$backupname" ]; then
    l="z"
    for (( i = 1 ; i <= 1000; i++ )) do
    if [ ! -f ~/Backup/backup_file_name_`date +"%Y-%m-%d"`_${l}_${i}.tar.gz ]; then
        backupname=~/Backup/backup_file_name_`date +"%Y-%m-%d"`_${l}_${i}.tar.gz
        break
    fi
    done
fi

if [ ! -z "$backupname" ]; then
    cd ~/projects/
    ~/scripts/tar.sh $backupname directory_to_backup/
    cd $working_dir
else
    echo "Oops! can't create backup file name."
fi

exit

The file ~/scripts/tar.sh contains this script: ~/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

Now I just have to type ~/scripts/backup.sh and the script backs up my files. 现在,我只需要键入~/scripts/backup.sh ,脚本就会备份我的文件。

Create a script which saves file with date like, 创建一个脚本来保存日期为

~/Backup/backup_file_name_${date}.tar.gz 〜/备份/ backup_file_name _ $ {}日期名为.tar.gz

and run that script a cron job if you want to take backup after some specific interval or run it manually if you dont have such requirement. 如果要在某个特定间隔后进行备份,请运行该脚本的cron作业,如果没有此要求,请手动运行该脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM