6 Commits
3.2.3 ... 3.2.6

Author SHA1 Message Date
06620a4dba Fix bug when exclude pattern is None 2023-05-31 19:07:50 +02:00
0dd7b887f7 Update PKGBUILD to use release version 2023-05-28 11:07:03 +02:00
7684bc43b0 Add desktop notifications 2023-05-25 23:44:59 +02:00
a07fce1b6c Version bump 2023-05-25 19:01:50 +02:00
11c1ab0952 Add options to remove old backups before copying files 2023-05-25 19:01:19 +02:00
a42ade4f2b Version bump 2023-05-25 17:27:04 +02:00
5 changed files with 68 additions and 45 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
.idea/ .idea/
__pycache__/
test/ test/

View File

@ -3,45 +3,39 @@
# Maintainer: Daniele Fucini <dfucini@gmail.com> # Maintainer: Daniele Fucini <dfucini@gmail.com>
pkgname=simple_backup pkgname=simple_backup
pkgver=3.1.2.r1.ga4c4b88
pkgrel=1
pkgdesc='Simple backup script that uses rsync to copy files' pkgdesc='Simple backup script that uses rsync to copy files'
pkgver=3.2.5
pkgrel=3
epoch=1
url="https://github.com/Fuxino/${pkgname}"
install=simple_backup.install
arch=('any') arch=('any')
url="https://github.com/Fuxino/simple_backup.git"
license=('GPL3') license=('GPL3')
makedepends=('git' makedepends=('python-setuptools'
'python-setuptools'
'python-build' 'python-build'
'python-installer' 'python-installer'
'python-wheel') 'python-wheel')
depends=('python' depends=('python'
'rsync' 'rsync'
'python-dotenv') 'python-dotenv')
optdepends=('python-systemd: use systemd log') optdepends=('python-systemd: use systemd log'
install=${pkgname}.install 'python-dbus: for desktop notifications')
source=(git+https://github.com/Fuxino/${pkgname}.git) conflicts=('simple_backup-git')
sha256sums=('SKIP') source=(${pkgname}-${pkgver}.tar.gz::https://github.com/Fuxino/${pkgname}/archive/${pkgver}.tar.gz
https://github.com/Fuxino/${pkgname}/releases/download/${pkgver}/${pkgname}-${pkgver}.tar.gz.sig)
pkgver() validpgpkeys=('7E12BC1FF3B6EDB2CD8053EB981A2B2A3BBF5514')
{ sha256sums=('fd418a9c641d604c64bd0a8487b5ab431ff71f192d6021e55b194085951cb40d'
cd ${pkgname} 'SKIP')
git describe --long --tags | sed 's/\([^-]*-g\)/r\1/;s/-/./g'
}
prepare()
{
git -C ${srcdir}/${pkgname} clean -dfx
}
build() build()
{ {
cd ${srcdir}/${pkgname} cd ${srcdir}/${pkgname}-${pkgver}
python -m build --wheel --no-isolation python -m build --wheel --no-isolation
} }
package() package()
{ {
cd ${srcdir}/${pkgname} cd ${srcdir}/${pkgname}-${pkgver}
python -m installer --destdir=${pkgdir} dist/*.whl python -m installer --destdir=${pkgdir} dist/*.whl
install -Dm644 ${srcdir}/${pkgname}/${pkgname}.conf ${pkgdir}/etc/${pkgname}/${pkgname}.conf install -Dm644 ${srcdir}/${pkgname}-${pkgver}/${pkgname}.conf ${pkgdir}/etc/${pkgname}/${pkgname}.conf
} }

View File

@ -1,3 +1,3 @@
"""Init.""" """Init."""
__version__ = '3.2.2' __version__ = '3.2.5'

View File

@ -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

View File

@ -19,12 +19,17 @@ try:
except ImportError: except ImportError:
journal = None journal = None
try:
import dbus
except ImportError:
pass
load_dotenv() load_dotenv()
euid = os.geteuid() euid = os.geteuid()
if euid == 0: if euid == 0:
user = os.getenv("SUDO_USER") user = os.getenv('SUDO_USER')
homedir = os.path.expanduser(f'~{user}') homedir = os.path.expanduser(f'~{user}')
else: else:
homedir = os.getenv('HOME') homedir = os.getenv('HOME')
@ -71,12 +76,13 @@ class MyFormatter(argparse.RawTextHelpFormatter, argparse.ArgumentDefaultsHelpFo
class Backup: 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.inputs = inputs
self.output = output self.output = output
self.exclude = exclude self.exclude = exclude
self.options = options self.options = options
self.keep = keep self.keep = keep
self.remove_before = remove_before
self._last_backup = '' self._last_backup = ''
self._output_dir = '' self._output_dir = ''
self._inputs_path = '' self._inputs_path = ''
@ -159,6 +165,11 @@ class Backup:
def run(self): def run(self):
logger.info('Starting backup...') logger.info('Starting backup...')
try:
notify('Starting backup...')
except NameError:
pass
self.create_backup_dir() self.create_backup_dir()
self.find_last_backup() self.find_last_backup()
@ -174,10 +185,14 @@ class Backup:
fp.write('\n') fp.write('\n')
with open(self._exclude_path, 'w') as fp: with open(self._exclude_path, 'w') as fp:
if self.exclude is not None:
for e in self.exclude: for e in self.exclude:
fp.write(e) fp.write(e)
fp.write('\n') 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...') logger.info('Copying files. This may take a long time...')
if self._last_backup == '': if self._last_backup == '':
@ -219,7 +234,7 @@ class Backup:
logger.error('Failed to create last_backup link. Permission denied') logger.error('Failed to create last_backup link. Permission denied')
self._err_flag = True self._err_flag = True
if self.keep != -1: if self.keep != -1 and not self.remove_before:
self.remove_old_backups() self.remove_old_backups()
os.remove(self._inputs_path) os.remove(self._inputs_path)
@ -230,6 +245,13 @@ class Backup:
if self._err_flag: if self._err_flag:
logger.warning('Some errors occurred (check log for details)') 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(): def _parse_arguments():
parser = argparse.ArgumentParser(prog='simple_backup', parser = argparse.ArgumentParser(prog='simple_backup',
@ -245,6 +267,8 @@ def _parse_arguments():
parser.add_argument('-k', '--keep', type=int, help='Number of old backups to keep') parser.add_argument('-k', '--keep', type=int, help='Number of old backups to keep')
parser.add_argument('-s', '--checksum', action='store_true', parser.add_argument('-s', '--checksum', action='store_true',
help='Use checksum rsync option to compare files (MUCH SLOWER)') 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() args = parser.parse_args()
@ -270,6 +294,24 @@ def _read_config(config_file):
return inputs, output, exclude, keep 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(): def simple_backup():
args = _parse_arguments() args = _parse_arguments()
inputs, output, exclude, keep = _read_config(args.config) inputs, output, exclude, keep = _read_config(args.config)
@ -291,7 +333,7 @@ def simple_backup():
else: else:
backup_options = '-arvh -H -X' 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(): if backup.check_params():
backup.run() backup.run()