Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
7684bc43b0
|
|||
a07fce1b6c
|
|||
11c1ab0952
|
|||
a42ade4f2b
|
5
PKGBUILD
5
PKGBUILD
@ -3,7 +3,7 @@
|
||||
# Maintainer: Daniele Fucini <dfucini@gmail.com>
|
||||
|
||||
pkgname=simple_backup
|
||||
pkgver=3.1.2.r1.ga4c4b88
|
||||
pkgver=3.2.3.r1.ga42ade4
|
||||
pkgrel=1
|
||||
pkgdesc='Simple backup script that uses rsync to copy files'
|
||||
arch=('any')
|
||||
@ -17,7 +17,8 @@ makedepends=('git'
|
||||
depends=('python'
|
||||
'rsync'
|
||||
'python-dotenv')
|
||||
optdepends=('python-systemd: use systemd log')
|
||||
optdepends=('python-systemd: use systemd log'
|
||||
'python-dbus: for desktop notifications')
|
||||
install=${pkgname}.install
|
||||
source=(git+https://github.com/Fuxino/${pkgname}.git)
|
||||
sha256sums=('SKIP')
|
||||
|
@ -1,3 +1,3 @@
|
||||
"""Init."""
|
||||
|
||||
__version__ = '3.2.2'
|
||||
__version__ = '3.2.5'
|
||||
|
@ -1,14 +0,0 @@
|
||||
#Example config file for simple_backup
|
||||
|
||||
[default]
|
||||
#Input directories. Use a comma to separate items
|
||||
inputs=/home/my_home,/etc
|
||||
|
||||
#Output directory
|
||||
backup_dir=/media/Backup
|
||||
|
||||
#Exclude patterns. Use a comma to separate items
|
||||
exclude=.gvfs,.local/share/gvfs-metadata,.cache,.dbus,.Trash,.local/share/Trash,.macromedia,.adobe,.recently-used,.recently-used.xbel,.thumbnails
|
||||
|
||||
#Number of snapshots to keep (use -1 to keep all)
|
||||
keep=-1
|
@ -19,12 +19,17 @@ try:
|
||||
except ImportError:
|
||||
journal = None
|
||||
|
||||
try:
|
||||
import dbus
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
|
||||
load_dotenv()
|
||||
euid = os.geteuid()
|
||||
|
||||
if euid == 0:
|
||||
user = os.getenv("SUDO_USER")
|
||||
user = os.getenv('SUDO_USER')
|
||||
homedir = os.path.expanduser(f'~{user}')
|
||||
else:
|
||||
homedir = os.getenv('HOME')
|
||||
@ -71,12 +76,13 @@ class MyFormatter(argparse.RawTextHelpFormatter, argparse.ArgumentDefaultsHelpFo
|
||||
|
||||
class Backup:
|
||||
|
||||
def __init__(self, inputs, output, exclude, keep, options):
|
||||
def __init__(self, inputs, output, exclude, keep, options, remove_before=False):
|
||||
self.inputs = inputs
|
||||
self.output = output
|
||||
self.exclude = exclude
|
||||
self.options = options
|
||||
self.keep = keep
|
||||
self.remove_before = remove_before
|
||||
self._last_backup = ''
|
||||
self._output_dir = ''
|
||||
self._inputs_path = ''
|
||||
@ -159,6 +165,11 @@ class Backup:
|
||||
def run(self):
|
||||
logger.info('Starting backup...')
|
||||
|
||||
try:
|
||||
notify('Starting backup...')
|
||||
except NameError:
|
||||
pass
|
||||
|
||||
self.create_backup_dir()
|
||||
self.find_last_backup()
|
||||
|
||||
@ -178,6 +189,9 @@ class Backup:
|
||||
fp.write(e)
|
||||
fp.write('\n')
|
||||
|
||||
if self.keep != -1 and self.remove_before:
|
||||
self.remove_old_backups()
|
||||
|
||||
logger.info('Copying files. This may take a long time...')
|
||||
|
||||
if self._last_backup == '':
|
||||
@ -219,7 +233,7 @@ class Backup:
|
||||
logger.error('Failed to create last_backup link. Permission denied')
|
||||
self._err_flag = True
|
||||
|
||||
if self.keep != -1:
|
||||
if self.keep != -1 and not self.remove_before:
|
||||
self.remove_old_backups()
|
||||
|
||||
os.remove(self._inputs_path)
|
||||
@ -230,6 +244,13 @@ class Backup:
|
||||
if self._err_flag:
|
||||
logger.warning('Some errors occurred (check log for details)')
|
||||
|
||||
try:
|
||||
notify('Backup finished with errors (check log for details)')
|
||||
except NameError:
|
||||
pass
|
||||
else:
|
||||
notify('Backup finished')
|
||||
|
||||
|
||||
def _parse_arguments():
|
||||
parser = argparse.ArgumentParser(prog='simple_backup',
|
||||
@ -245,6 +266,8 @@ def _parse_arguments():
|
||||
parser.add_argument('-k', '--keep', type=int, help='Number of old backups to keep')
|
||||
parser.add_argument('-s', '--checksum', action='store_true',
|
||||
help='Use checksum rsync option to compare files (MUCH SLOWER)')
|
||||
parser.add_argument('--remove-before-backup', action='store_true',
|
||||
help='Remove old backups before executing the backup, instead of after')
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
@ -270,6 +293,24 @@ def _read_config(config_file):
|
||||
return inputs, output, exclude, keep
|
||||
|
||||
|
||||
def notify(text):
|
||||
euid = os.geteuid()
|
||||
|
||||
if euid == 0:
|
||||
uid = os.getenv('SUDO_UID')
|
||||
else:
|
||||
uid = os.geteuid()
|
||||
|
||||
os.seteuid(int(uid))
|
||||
os.environ['DBUS_SESSION_BUS_ADDRESS'] = f'unix:path=/run/user/{uid}/bus'
|
||||
|
||||
obj = dbus.SessionBus().get_object('org.freedesktop.Notifications', '/org/freedesktop/Notifications')
|
||||
obj = dbus.Interface(obj, 'org.freedesktop.Notifications')
|
||||
obj.Notify('', 0, '', 'simple_backup', text, [], {'urgency': 1}, 10000)
|
||||
|
||||
os.seteuid(int(euid))
|
||||
|
||||
|
||||
def simple_backup():
|
||||
args = _parse_arguments()
|
||||
inputs, output, exclude, keep = _read_config(args.config)
|
||||
@ -291,7 +332,7 @@ def simple_backup():
|
||||
else:
|
||||
backup_options = '-arvh -H -X'
|
||||
|
||||
backup = Backup(inputs, output, exclude, keep, backup_options)
|
||||
backup = Backup(inputs, output, exclude, keep, backup_options, remove_before=args.remove_before_backup)
|
||||
|
||||
if backup.check_params():
|
||||
backup.run()
|
||||
|
Reference in New Issue
Block a user