remove encryption option, make it work and push stuff to github

This commit is contained in:
Von Random 2014-12-20 18:11:59 +03:00
parent c0b5413e3b
commit 2e0cd8f7af
2 changed files with 76 additions and 37 deletions

View file

@ -1,7 +1,9 @@
protocol=ssh # ftp, sftp, ftps, ssh or local protocol=ssh # ftp, sftp, ftps, ssh or local
#port_remote=22 source_dir=/home/user/source
snap_file=/var/backup/snapshot.list remote_host=hostname.tld
exclude_list=/usr/local/etc/backup/excludes.list backup_dir=relative_or_full_path
incremental_backup=true #if var exists, content does not matter #snap_file=/var/backup/snapshot.list
gnupg_key=/usr/local/etc/backup/gpg_key #exclude_list=/usr/local/etc/backup/excludes.list
compress_format=xz compress_format=xz # gz, bz2, xz or empty for non-compressed
remote_user=username
remote_pass=PassWd

99
backup.zsh Normal file → Executable file
View file

@ -1,9 +1,6 @@
#!/usr/bin/env zsh #!/usr/bin/env zsh
# Default config file set -e
default_cfg='/usr/local/etc/backup.cfg' self_name=$0
# Default date postfix
default_postfix=$(date +%F-%H%M)
function err function err
{ {
@ -11,30 +8,51 @@ function err
return $1 return $1
} }
function default_ports function cfg_err
{ {
if [[ -z $port_remote ]]; then [[ -n $1 ]] && echo "$1 is not set in configuration, but is required by $0 to work." >&2
case $protocol in return 5
('ftp') remote_port='21';; }
('sftp'|'ssh') remote_port='22';;
(*) err 1 "$protocol is not a valid value for the protocol option.";; function usage
esac {
echo "usage: $self_name [--help|--config]
--help -h - show this message
--config -c - use config from the specified path
Default config path /usr/local/etc/backup.cfg will be used if invoked without options"
}
function read_config
{
source $cfg || err 15 'Config file does not exist'
[[ -z $source_dir ]] && cfg_err 'Backup source'
[[ -z $remote_host ]] && cfg_err 'Remote host'
[[ -z $protocol ]] && cfg_err 'Backup protocol'
[[ -z $backup_dir && $protocol != 'ssh' ]] && cfg_err 'Target directory'
if [[ -z $local_host ]]; then
local_host=$HOST
fi fi
src_basename=${source_dir:t}
src_basedir=${source_dir:h}
} }
function generate_fullpath function generate_fullpath
{ {
src_basename=${src_fullpath:t} local backup_type
src_basedir=${src_fullpath:h} local postfix
if [[ -z $outfile_postfix ]]; then
postfix=$default_postfix
else
postfix=$outfile_postfix
fi
if [[ -s $snap_file ]]; then if [[ -s $snap_file ]]; then
backup_type='incr' backup_type='incr'
else else
backup_type='full' backup_type='full'
fi fi
if [[ -z $backup_directory && $protocol != 'ssh' ]]; then if [[ -z $backup_filename ]]; then
err 1 "You have not set the backup directory path." outfile="$backup_dir/${local_host}-${src_basename}_${postfix}_${backup_type}.t${compress_format:-'ar'}"
elif [[ -z $backup_filename ]]; then
outfile="$backup_directory/$host_local\-$src_basename\_$postfix\_$backup_type.t${compress_format:-ar}"
else else
outfile=$backup_filename outfile=$backup_filename
fi fi
@ -42,6 +60,9 @@ function generate_fullpath
function compress # compress to stdout function compress # compress to stdout
{ {
local compress_flag
local exclude_option
local snapshot_option
case $compress_format in case $compress_format in
('xz') compress_flag='J' ;; ('xz') compress_flag='J' ;;
('bz2') compress_flag='j' ;; ('bz2') compress_flag='j' ;;
@ -56,7 +77,7 @@ function compress # compress to stdout
err 1 "Exclusion list $exclude_list is either unreadable or does not exist." err 1 "Exclusion list $exclude_list is either unreadable or does not exist."
fi fi
fi fi
if [[ -n $snap_file && -n $incremental_backup ]]; then if [[ -n $snap_file ]]; then
if printf '' >> $snap_file; then if printf '' >> $snap_file; then
snapshot_option='-g' snapshot_option='-g'
else else
@ -66,22 +87,38 @@ function compress # compress to stdout
tar cf$compress_flag - -C $src_basedir $src_basename $snapshot_option $snapshot_file $exclude_option $exclude_list --ignore-failed-read tar cf$compress_flag - -C $src_basedir $src_basename $snapshot_option $snapshot_file $exclude_option $exclude_list --ignore-failed-read
} }
function encrypt # encrypt if encryption is needed
{
if [[ -n $gnupg_key ]]; then
gpg -r $gnupg_key -e -
else
read _
print $_
fi
}
function store # store to local or remote function store # store to local or remote
{ {
if [[ -z $remote_port ]]; then
case $protocol in
('ftp'|'ftps') remote_port='21';;
('sftp'|'ssh') remote_port='22';;
('local') unset remote_port;;
(*) err 1 "$protocol is not a valid value for the protocol option.";;
esac
fi
case $protocol in case $protocol in
('local') dd of=$outfile ;; ('local') dd of=$outfile ;;
('ssh') ssh -p$port_remote $user_remote@$host_remote "dd of=$outfile" ;; ('ssh') ssh -p$remote_port $remote_user@$remote_host "dd of=$outfile" ;;
('sftp'|'ftp'|'ftps') curl -ksS -T - $protocol://$host_remote:$port_remote/$outfile -u $user_remote:$pass_remote ;; ('sftp'|'ftp'|'ftps') curl -ksS -T - $protocol://$remote_host:$remote_port/$outfile -u $remote_user:$remote_pass ;;
(*) err 1 'Wrong protocol!' ;; (*) err 1 'Wrong protocol!' ;;
esac esac
} }
function main
{
while [[ -n $1 ]]; do
case $1 in
('--help'|'-h') usage; return 0;;
('--config'|'-c') shift; opt_cfg=$1; shift;;
esac
done
cfg=${opt_cfg:-'/usr/local/etc/backup.cfg'}
default_postfix=$(date +%F-%H%M)
read_config
generate_fullpath
compress | store
return 0
}
main $@