LINUXSHELL小作業

來源:互聯網
上載者:User

題目

Write a script called homebackup that automates tar so the person executing the script always uses the desired options (cvp) and backup destination directory (/var/backups) to make a backup of his or her home directory. Implement the following features:

Test for the number of arguments. The script should run without arguments. If any arguments are present, exit after printing a usage message.

Determine whether the backups directory has enough free space to hold the backup.

Ask the user whether a full or an incremental backup is wanted. If the user does not have a full backup file yet, print a message that a full backup will be taken. In case of an incremental backup, only do this if the full backup is not older than a week.

Compress the backup using any compression tool. Inform the user that the script is doing this, because it might take some time, during which the user might start worrying if no output appears on the screen.

Print a message informing the user about the size of the compressed backup.

參考實現
#!/bin/bashARG_NUM=$#if [ $ARG_NUM -gt 0 ]then        echo "Usage: ./homebackup.sh "        exitfiFREE_SPACE=`df /home/backup | tail -1|awk '{print $4}'`USER_CHOICE=0echo "Please input your choice:"echo "0,full backup"echo "1,incremental backup"BACKUP_FILE_NAME="backup"$(date +%Y-%m-%d)".tar.gz"read USER_CHOICEcase $USER_CHOICE in0)        echo "start to backup in full mode";;1)        echo "start to backup in incremental mode"        if [ -f "/home/backup/backup.tar.gz" ]        then                LAST_BACK_TIME=`stat /home/backup/backup.tar.gz |grep Modify|awk -F. '{print $1}' |awk '{print $2" "$3}'`                LAST_BACK_TIME=`date -d "$LAST_BACK_TIME" +%s`                NOW=`date -d "-7 day" +%s`                if [ $LAST_BACK_TIME -gt $NOW ]                then                        echo "You cannot do backup operation"                        echo "The last backup is on " `date -d @"$LAST_BACK_TIME" +%Y-%m-%d`                        exit                fi        fi;;*) echo "wrong operation"        exit;;esactar -cvf $BACKUP_FILE_NAME *.shBACK_UP_SIZE=`ls -la $BACKUP_FILE_NAME|awk '{print $5}' `if [ $BACK_UP_SIZE  -gt $FREE_SPACE ]thenecho "Insufficient space in /home/backup, it need $BACK_UP_SIZE bytes"exitfimv $BACKUP_FILE_NAME /home/backup/backup.tar.gz

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.