Differences
This shows you the differences between two versions of the page.
tech:linux:backup_directory_script [2018/09/10 06:27] |
tech:linux:backup_directory_script [2018/09/10 06:27] (current) |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Bash script to backup ====== | ||
+ | ===== Directory ===== | ||
+ | |||
+ | Bash script to backup regular files in current directory. The default backup directory is archive inside the current directory. Or you may specify a backup directory as the 1st argument to the script. | ||
+ | |||
+ | <code bash> | ||
+ | #!/bin/bash | ||
+ | # | ||
+ | # Backup Regular files to archive/timestamp | ||
+ | # | ||
+ | NOW=$(date +"%Y-%m-%d-%H-%M-%S") | ||
+ | # | ||
+ | # | ||
+ | if [[ -z $1 ]] | ||
+ | then | ||
+ | BACKUPDIR=archive/$NOW | ||
+ | else | ||
+ | BACKUPDIR=$1/$NOW | ||
+ | fi | ||
+ | mkdir -p $BACKUPDIR | ||
+ | # | ||
+ | for file in $(ls -p | grep -v /$) | ||
+ | do | ||
+ | cp -p $file $BACKUPDIR/ | ||
+ | done | ||
+ | # | ||
+ | exit 0 | ||
+ | </code> | ||
+ | |||
+ | |||
+ | ===== File ===== | ||
+ | <code bash> | ||
+ | #!/bin/bash | ||
+ | # | ||
+ | if [[ ! ("$#" == 1) ]]; then | ||
+ | echo "File name required!" | ||
+ | exit 1 | ||
+ | fi | ||
+ | filename=$1 | ||
+ | # | ||
+ | NOW=$(date +"%Y-%m-%d-%H-%M-%S") | ||
+ | mkdir -p archive | ||
+ | cp -p $filename archive/$filename.${NOW} | ||
+ | </code> | ||